Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Euler problem 8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Euler problem 8" Watch "Euler problem 8" New topic
Author

Euler problem 8

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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!


SCJP
Visit my download page
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
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.


Joanne
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

i will try using the backspace key to get rid of the newlines
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Aug 05, 2005
Posts: 3432
    
  12
If you use + to concatenate multiple string literals together the compiler will treat them as a single string literal.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

ok, thanks again joann! i know how to do that!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
You just need to enclose each line in double quotes and put plus signs in between them.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

oh really! like "" "" instead of " "?

oh... i misunderstood...now i see what you mean...i think
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.)
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 506
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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

Joined: Oct 01, 2001
Posts: 2841
    
  11

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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Oct 01, 2001
Posts: 2841
    
  11

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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).
 
wood burning stoves
 
subject: Euler problem 8
 
Similar Threads
Unable to understand
Generics question
Simple ArrayList conundrum?
generics code mixed with legacy collection
Problem with parseInt and entering in array