This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Sep 21, 2011
You just need to enclose each line in double quotes and put plus signs in between them.
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.)
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.
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.
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
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 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.)
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.
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.
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).