Problem 020

A place to air possible concerns or difficulties in understanding ProjectEuler problems. This forum is not meant to publish solutions. This forum is NOT meant to discuss solution methods or giving hints how a problem can be solved.
Forum rules
As your posts will be visible to the general public you are requested to be thoughtful in not posting anything that might explicitly give away how to solve a particular problem.

This forum is NOT meant to discuss solution methods for a problem.

In particular don't post any code fragments or results.

Don't start begging others to give partial answers to problems

Don't ask for hints how to solve a problem

Don't start a new topic for a problem if there already exists one


See also the topics:
Don't post any spoilers
Comments, questions and clarifications about PE problems.
themadman
Posts: 2
Joined: Fri Dec 12, 2008 11:22 am

Problem 20

Post by themadman »

Hey!
Uh, can sombody please explain problem 20 to me or send me to a site that can.
I have no clue what so ever to the theory, etc.
It would be great if it was in a way that a high school student could understand it.
Cheers. :)

PS I think I put this in the wrong place
User avatar
hk
Administrator
Posts: 12842
Joined: Sun Mar 26, 2006 10:34 am
Location: Haren, Netherlands

Re: Problem 20

Post by hk »

Are we talking about: Find the sum of the digits in the number 100!

Take a simpler example:
5!=5*4*3*2*1=120
The digits of 120 are 1,2 and 0.
Then 1+2+0=3.
The sum of the digits of 5! is 3.
Image
War ruins the life and health of untold numbers of innocent children.
themadman
Posts: 2
Joined: Fri Dec 12, 2008 11:22 am

Re: Problem 20

Post by themadman »

Oh now I see
easy :roll:
Thanks mate :D
Paul-Hadfield
Posts: 2
Joined: Mon Jan 19, 2009 3:02 pm

Re: Problem 020

Post by Paul-Hadfield »

I'm confused - I've run my code for this example and it works for the test answer of 5!, the answer being sum of 120 which equals 3. However I'm running into problems when I do it for 100. I'm pretty sure it's something to do with accuracy of scientific numbers as if I run my code in C#, using a double, it returns a value of 60. In desperation I've tried windows calculator, which appears to return a better populated scientific number , which results in a sum of 153. Any pointers would be gratefully appreciated.
User avatar
stijn263
Posts: 1505
Joined: Sat Sep 15, 2007 11:57 pm
Location: Netherlands

Re: Problem 020

Post by stijn263 »

100! [asymp] 9.33262154 × 10157

Which is more than 64 bit precision, so you'll need to do it in another way...

Since the answer is 157 digits long, perhaps you can represent it as a an array/vector of 157 digits.
Paul-Hadfield
Posts: 2
Joined: Mon Jan 19, 2009 3:02 pm

Re: Problem 020

Post by Paul-Hadfield »

Thanks for the pointer, just shows why I need to do these things!
Roxxor
Posts: 2
Joined: Sat Jul 03, 2010 1:17 pm

Re: Problem 020

Post by Roxxor »

stijn263 wrote:100! [asymp] 9.33262154 × 10157

Which is more than 64 bit precision, so you'll need to do it in another way...

Since the answer is 157 digits long, perhaps you can represent it as a an array/vector of 157 digits.
9.33262154 × 10157 is the answer I get. when I do 100!. But I can not understand why it ends as a decimal number when no decimals are involved in 100!. Can somebody explain?
harryh
Posts: 2091
Joined: Tue Aug 22, 2006 9:33 pm
Location: Thessaloniki, Greece

Re: Problem 020

Post by harryh »

Roxxor wrote:9.33262154 × 10157 is the answer I get. when I do 100!. But I can not understand why it ends as a decimal number when no decimals are involved in 100!. Can somebody explain?
100! is an integer; it has no decimals.
But it is a very large integer, containing 158 digits.
Most calculators, programs etc cannot display or print such very large numbers.
Instead, they print something like 9.33262154 × 10157 which is only an approximation to the actual value, showing in a compact form the first 9 significant digits of the number as well as the order of its magnitude (the number of its digits).

9.33262154 × 10157 = 933262154 × 10149 and now, there are no decimals!
Of course, it still only an approximate value for 100!.
What it means is that 100! is an integer which starts with 933262154... (followed by 149 more digits).

A bigger calculator, would tell you that 100! is approximately equal to 9.3326215443944152681699238856 × 10157
which is equal to 93326215443944152681699238856 × 10129,
So, 100! is an integer starting with 93326215443944152681699238856... (and followed by 129 more digits).

However, that's still an approximation for the value of 100!.
It is a better approximation (since you now have more significant digits than before), but you still don't have the remaining 129 digits.
LarryBlake
Posts: 100
Joined: Sat Aug 29, 2009 8:49 pm

Re: Problem 020

Post by LarryBlake »

If you're using C# 2010, you can use the BigInteger class (you'll need to import System.Numerics).

I should mention that while BigInteger will work fine for this problem, it's really slow compared to the normal numeric types. Don't overuse it.

EDIT: BTW, I solved this one by writing my own large number logic as stijn263 suggested. It's a fun exercise if you like that sort of thing.
Image
nvno
Posts: 1
Joined: Mon Jul 26, 2010 8:16 pm

Re: Problem 020

Post by nvno »

Hi everyone

The answer field on the problem page have a max length of 30. Its a page problem or mine?
User avatar
hk
Administrator
Posts: 12842
Joined: Sun Mar 26, 2006 10:34 am
Location: Haren, Netherlands

Re: Problem 020

Post by hk »

Yours.
Image
War ruins the life and health of untold numbers of innocent children.
User avatar
elendiastarman
Posts: 410
Joined: Sat Dec 22, 2007 8:15 pm

Re: Problem 020

Post by elendiastarman »

nvno wrote:Hi everyone

The answer field on the problem page have a max length of 30. Its a page problem or mine?
Problem 020 (View Problem)

You only need the last ten digits, not all of them.
Want some
3.14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679...?
Image
User avatar
hk
Administrator
Posts: 12842
Joined: Sun Mar 26, 2006 10:34 am
Location: Haren, Netherlands

Re: Problem 020

Post by hk »

elendiastarman wrote:
nvno wrote:Hi everyone

The answer field on the problem page have a max length of 30. Its a page problem or mine?
Problem 020 (View Problem)

You only need the last ten digits, not all of them.
Are you sure?
Image
War ruins the life and health of untold numbers of innocent children.
User avatar
elendiastarman
Posts: 410
Joined: Sat Dec 22, 2007 8:15 pm

Re: Problem 020

Post by elendiastarman »

Bah, I was thinking of a different problem. [smacks forehead] :oops:

Restatement: You only need the SUM of the digits...not all of them.
Want some
3.14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679...?
Image
Allasar
Posts: 4
Joined: Fri Jul 30, 2010 2:05 pm

Re: Problem 020

Post by Allasar »

As usual there is a very elegant way to solve this problem.

To be honest, I can't remember exactly how I did it but I solely program in C++ (it's the only language I know) and I remember that I found a very handy C++ solution in the forum once I solved it.

Not giving it away but think about school and how you learned to multiply in your head or by pen and paper. Now try to learn the computer to do it in the same way. For instance, when you were asked to do 28*56, you would do it step by step.

I didn't think of creating big numbers this way but it has helped me in alot of problems on this site. Until now I never used BigInt, not that I have anything against it, it's just fun that you can solve problems this way.
JMW1994
Posts: 43
Joined: Sat Apr 09, 2011 11:35 pm

Trying to grab a digit with a string

Post by JMW1994 »

I'm trying to solve Problem #20 and I already got a way to get the answer of n!. However, I can't figure out how to get the sum of digits within the n! answer. In the problem, the sample showed that 10! = 3628800 and adding the digit of those numbers equals to 27. Before I try to get the sum of digits of 100!, I need to figure out how to program such a thing.

I first thought of using strings to convert one digit of the result of n! at a time, revert back to an integer, and add the digit to another variable consisting the final answer. However, I have no clue on how to do this.

I'm using C++. I don't want the answer to Problem #20 though, just a way to get to the desired result with 10!.
Image
TripleM
Posts: 384
Joined: Fri Sep 12, 2008 3:31 am

Re: Trying to grab a digit with a string

Post by TripleM »

That really depends on how you're storing the answer of 100!. For example, if you wanted to sum the digits of an int N, consider N%10 and N/10 and how you could use that to find the sum you're looking for.

However, since 100! doesn't fit in an int, that doesn't apply. Since storing 100! is the main point of this problem, that's where you need to start.
User avatar
rayfil
Administrator
Posts: 1412
Joined: Sun Mar 26, 2006 5:30 am
Location: Quebec, Canada
Contact:

Re: Problem 020

Post by rayfil »

Your post was related to Problem 020 and was moved to its proper topic. The Suggestion & FAQ forum is not designed for this kind of post.
When you assume something, you risk being wrong half the time.
JMW1994
Posts: 43
Joined: Sat Apr 09, 2011 11:35 pm

Re: Problem 020

Post by JMW1994 »

100! will fit in a long long integer, but you will have x.xxxxxxe+xx as your answer
Image
TripleM
Posts: 384
Joined: Fri Sep 12, 2008 3:31 am

Re: Problem 020

Post by TripleM »

No, it doesn't - a long long can only store numbers up to 2^63, which won't fit 21! let alone 100!. If you meant a long double, that's even worse as it can only store integers up to about 2^53 before drastically rounding anything bigger.

Try calculating 21! in a long long and then print out the result - you'll get a negative number, meaning you've overflowed the maximum.

As mentioned earlier in this thread, in-built data types aren't going to solve this one for you - you'll need to write an algorithm to perform the factorial like you would do with pen and paper.
Post Reply