n! means n × (n − 1) × ... × 3 × 2 × 1
Find the sum of the digits in the number 100!
Since it had been a little while since my last Project Euler post, I decided I would do an easy problem. Even though it was further down the list (there were other easier ones than this), I thought I could quickly come up with a solution in Java. That's where I was wrong.
Using the primitive data types in Java, the precision needed to compute the values for 100! did not exist. I experimented with using the BigInteger class, but the code was clunky. I have been considering doing these with a more mathematically based language, and made the leap to Mathematica tonight. At $2500 per license, it wouldn't be possible for me to use it, but fortunately I downloaded a version using a student license before I graduated from CU.
I am completely unfamiliar with using this software, and since I couldn't find a good function for summing the digits of a number, I did it manually. I'm sure there are more efficient ways of calculating the answer, but since this was my first Mathematica program, I'm ok with getting the answer, which was right on the first try.
In[1]:= 100!
Out[1]= 93326215443944152681699238856266700490715968264381621468592963\
8952175999932299156089414639761565182862536979208272237582511852109168\
64000000000000000000000000
In[2]:=
9 + 3 + 3 + 2 + 6 + 2 + 1 + 5 + 4 + 4 + 3 + 9 + 4 + 4 + 1 + 5 + 2 + 6 \
+ 8 + 1 + 6 + 9 + 9 + 2 + 3 + 8 + 8 + 5 + 6 + 2 + 6 + 6 + 7 + 4 + 9 + \
7 + 1 + 5 + 9 + 6 + 8 + 2 + 6 + 4 + 3 + 8 + 1 + 6 + 2 + 1 + 4 + 6 + 8 \
+ 5 + 9 + 2 + 9 + 6 + 3 + 8 + 9 + 5 + 2 + 1 + 7 + 5 + 9 + 9 + 9 + 9 + \
3 + 2 + 2 + 9 + 9 + 1 + 5 + 6 + 8 + 9 + 4 + 1 + 4 + 6 + 3 + 9 + 7 + 6 \
+ 1 + 5 + 6 + 5 + 1 + 8 + 2 + 8 + 6 + 2 + 5 + 3 + 6 + 9 + 7 + 9 + 2 + \
8 + 2 + 7 + 2 + 2 + 3 + 7 + 5 + 8 + 2 + 5 + 1 + 1 + 8 + 5 + 2 + 1 + 9 \
+ 1 + 6 + 8 + 6 + 4
Out[2]= 648
No comments:
Post a Comment