# Euler problem 8

posted 4 years ago

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!

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

Posts: 3742

16

posted 4 years ago

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

i have no idea how to solve this though. and this is only the setup for the actual problem

SCJP

Visit my download page

posted 4 years ago

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

SCJP

Visit my download page

Tim Moores

Bartender

Posts: 2946

46

posted 4 years ago

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

the current problem is that i get errors because there are newline characters in the string i copy and pasted

SCJP

Visit my download page

Joanne Neal

Rancher

Posts: 3742

16

posted 4 years ago

well that was no help i get errors saying

unclosed string literal

integer number too large

etc.

unclosed string literal

integer number too large

etc.

SCJP

Visit my download page

posted 4 years ago

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

i will try one more time to do it using the backspace key

SCJP

Visit my download page

Tim Moores

Bartender

Posts: 2946

46

posted 4 years ago

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

oh... i misunderstood...now i see what you mean...i think

oh... i misunderstood...now i see what you mean...i think

SCJP

Visit my download page

posted 4 years ago

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

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

SCJP

Visit my download page

posted 4 years ago

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

Also note that your

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.

posted 4 years ago

yeah i figured out what you meant.

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.

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.

SCJP

Visit my download page

posted 4 years ago

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.

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

posted 4 years ago

Even if you separate the declaration from the initialization of the variable, there's not need for the

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

Posts: 524

posted 4 years ago

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

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

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?

posted 4 years ago

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.

posted 4 years ago

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

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.

posted 4 years ago

Erm? Are you perhaps confusing this with some other Euler problem that RT is working on? Or am I missing something?

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?

posted 4 years ago

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.

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.

posted 4 years ago

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

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

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

SCJP

Visit my download page

posted 4 years ago

I believe there is another solution. Whether such solution - if it exists - is actually

(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 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.)

posted 4 years ago

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.

posted 4 years ago

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

Posts: 4568

9

posted 4 years ago

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).

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).