• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Euler problem 8

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i probably should have posted this in beginners. i am trying to declare a String. the compiler insists it is an integer

i get 4 errors all based on the compiler thinking i am talking integer when i mean String!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:i get 4 errors all based on the compiler thinking i am talking integer when i mean String!


I don't know what errors you're getting, but I get an 'unclosed string literal' error. String literals can't contain newlines.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh..that must be it. i copy and pasted it.

i have no idea how to solve this though. and this is only the setup for the actual problem
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i will try using the backspace key to get rid of the newlines
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what the actual problem is, but since it mentions the sum of the digits, are you familiar with the String.charAt method?
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have, and i haven't got to that yet because of these compiler errors
the current problem is that i get errors because there are newline characters in the string i copy and pasted
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use + to concatenate multiple string literals together the compiler will treat them as a single string literal.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks again joann! i know how to do that!
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well that was no help i get errors saying
unclosed string literal
integer number too large
etc.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i need to use a program where i can see the newline characters and delete them. I dont have microsoft Word anymore but i have the open office version. it can probably do it.
i will try one more time to do it using the backspace key
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to enclose each line in double quotes and put plus signs in between them.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh really! like "" "" instead of " "?

oh... i misunderstood...now i see what you mean...i think
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, it compiles now thanks
you might have noticed i had an import statement in the wrong place also. i also had the name of the class wrong. but it compiles now
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:well that was no help i get errors saying
unclosed string literal
integer number too large
etc.



Er, no. What was meant was that you can concatenate multiple lines of literals like this:



Also note that your new String() is completely pointless. In fact, there's probably never a good reason to use String's no-arg constructor.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i figured out what you meant.

Also note that your new String() is completely pointless. In fact, there's probably never a good reason to use String's no-arg constructor.


yeah i only separated the declaration from the instantiation in desperation. i should change it back. anyway it works fine now and solves the problem.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just curious as to what you are doing here. 2^(any integer power) cannot end it a 0.

And IMHO, the point of the exercise is not to add up a bunch of digits, but to come up with a clever way of calculating 2^1000.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:yeah i figured out what you meant.

Also note that your new String() is completely pointless. In fact, there's probably never a good reason to use String's no-arg constructor.


yeah i only separated the declaration from the instantiation in desperation. i should change it back. anyway it works fine now and solves the problem.




Even if you separate the declaration from the initialization of the variable, there's not need for the new String() part. Just String string; is sufficient. In fact it's more correct, since you're not actually using the empty String value. (And if you were, you should just have used "" instead.)
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I'm just curious as to what you are doing here. 2^(any integer power) cannot end it a 0.

And IMHO, the point of the exercise is not to add up a bunch of digits, but to come up with a clever way of calculating 2^1000.



Project Euler - Problem 8 http://projecteuler.net/problem=8



Can somebody tell me what does "Greatest Product" mean? Is it Greatest Common denominator?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a lot of different ways to choose 5 consecutive digits from a big number. If it's a 1000-digit number then there are 996 ways. So if you look at those 996 sets of 5 digits and take the product of each set, you get 996 products. Then the "greatest product" is the one which is the greatest.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh Pillai wrote:

Can somebody tell me what does "Greatest Product" mean? Is it Greatest Common denominator?



No. It means, for any 5 consecutive digits in that number, which product of those 5 digits is greater than all the other products of 5 consecutive digits.

For instance, the number given starts with 7316717653. So is 7 * 3 * 1 * 6 * 7 (digits 1-5) greater than 3 * 1 * 6 * 7 * 1 (digits 2-6)? What about 1 * 6 * 7 * 1 * 7 (digits 3-7)? etc.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I'm just curious as to what you are doing here. 2^(any integer power) cannot end it a 0.

And IMHO, the point of the exercise is not to add up a bunch of digits, but to come up with a clever way of calculating 2^1000.



Erm? Are you perhaps confusing this with some other Euler problem that RT is working on? Or am I missing something?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the sum of the digits of 2^1000 is Euler 16. However, the OP had the class name Euler16 and a getMessage() method that returned the question asked by Euler 16, so I can see where fred could get confused. I did too.

BTW, is there a clever way to sum up the digits of 2^1000? When I ran through the problems, I just computed the giant number, converted it to a decimal string, and added up the numbers. I'd think anything "more clever" would essentially just be doing the same thing the binary to decimal conversion does.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the confusion. i hadn't changed the message from when i solved # 16(if you cant tell i kind of like copy/paste/edit)
this one was quite easy actually, except for the problem i had creating the string
thanks jeff

BTW, is there a clever way to sum up the digits of 2^1000?


i solved that one the same way you suggest i think. once i got the BigInteger i changed it to a string and parsed that to create the sum. don't want to give exact solutions
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cut-and-paste is a proven time-saving technique, but it has its hazards!

That's how I solved Euler 16 too. Fred implied there's a better way, but I don't think so ... at least not much better.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:That's how I solved Euler 16 too. Fred implied there's a better way, but I don't think so ... at least not much better.


I believe there is another solution. Whether such solution - if it exists - is actually better is probably in the eye of the beholder. I have always thought that computing the power using BigInteger (even with optimization to avoid thousand of multiplications) is actually a brute-force approach and that there is a more elegant, mathematical solution. I'd have to spend a considerable amount of time to even see the glimpse of the solution, so I didn't try.

(There is a branch in mathematics which concerns divisibility and digit sums. One of the simplest and most known rules in this field is that a digit sum of a number is divisible by three if and only if the original number is divisible by three. An interesting page regarding these aspects is here, though it has to be noted that the DigitSum function as used there is actually a digital root.)
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure about that "eye of the beholder". I don't believe there's an algorithm significantly better than computing the digits one by one, and summing them, and I trust that BigInteger converts an integer to decimal digits in the most efficient possible algorithm. You might be able to save some time in the conversion to String and back to digits, but that's peanuts. The only significantly different way to solve the problem would be to compute the sum without ever needing to compute the individual digits, and even keeping those number theory rules in mind, I just don't think that's possible.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible that there is a mathematical theory of the sum of the digits of 2^n. However I doubt very much that such a theory is going to produce a closed-form expression for the function sumofthedigitsof2tothe(n); it's more likely to say something like "the limit as n -> infinity of (sumofthedigitsof2tothe(n) / n) is 5". So I'm also of the opinion that evaluating the digits of 2^1000 is the way to go.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The early Euler problems tend to be amenable to the "obvious" approach - it's the later ones where you have to get clever.

For instance, problems 18 and 67 are exactly the same problem. Except you can solve 18 with brute force, but that's just not realistic for 67, where you have to find a more efficient algorithm (of course, if you find that straight away you can solve both problems at the same time).
reply
    Bookmark Topic Watch Topic
  • New Topic