d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Using Large Numbers Without A Library
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 28 2013 09:15pm
Is it possible to solve this without using a library like GMP?

Quote
n! means n  (n  1)  ...  3  2  1

For example, 10! = 10  9  ...  3  2  1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2013 09:22pm
Simple answer is yes. Since all you want is the sum of the digits, you probably don't actually have to calculate 100! Personally I'd look into some number theory to see if it can help simplify the problem.

But if you don't know much number theory, you can try the java library that comes standard. I think it's called BigInteger or something like that. you can also represent the number differently and do your own math on it. one example is creating a byte array with the correct size to fit the number. a 32 bit integer can be represented by byte[4] for example. so create a method that will treat a byte array as a single number with lots of digits and another method that will multiply two of them together.

This post was edited by carteblanche on Mar 28 2013 09:44pm
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 28 2013 09:47pm
Thanks and you're right, I see that there's a pattern. I guess I'll just try to figure it out

6! = 9
7! = 9
8! = 9
9! = 27
10! = 27
11! = 36
12! = 27
13! = 27
14! = 45
15! = 45
16! = 63
17! = 63
18! = 54
19! = 45
20! = 54

This post was edited by Foxic on Mar 28 2013 10:08pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2013 09:57pm
Quote (Foxic @ Mar 28 2013 11:47pm)
Thanks and you're right, I see that there's a pattern. I guess I'll just try to figure it out

6! = 9
7! = 9
8! = 9
9! = 25
10! = 27
11! = 36
12! = 27
13! = 27
14! = 45
15! = 45
16! = 63
17! = 63
18! = 54
19! = 45
20! = 54


i dont think that plan is going to work, but i havent tested it out

btw 9! => 25 doesn't look right to me. the sum has to be divisible by 3. not to mention the sum of 9! has to be the same as 10! since multiplying by 10 just adds a 0 to the end and doesn't change the sum

This post was edited by carteblanche on Mar 28 2013 10:00pm
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Mar 28 2013 10:08pm
Woops, I meant 27 thanks
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll