Project Euler Homework

020.cpp

Go to the documentation of this file.
00001 
00007 #include <iostream>
00008 using namespace std;
00009 
00010 const unsigned long long digits = 1e17;
00011 const int gps = 10;
00012 class bigN
00013 {
00014 //spilt into 16 groups, each with 19 digits
00015 //smallest place unit exists in x[0]
00016   unsigned long long x[gps];
00017 public:
00018     bigN (int init)
00019   {
00020     x[0] = init;
00021     for (int k = 1; k < gps; k++)
00022       x[k] = 0;
00023   }
00024   void times (int y)
00025   {
00026     for (int k = 0; k < gps; k++)
00027       x[k] *= y;
00028     for (int k = 0; k < gps - 1; k++)
00029       if (x[k] > digits)
00030         {
00031           x[k + 1] += x[k] / digits;
00032           x[k] %= digits;
00033         }
00034   }
00035   unsigned sum_of_digits ()
00036   {
00037     unsigned long long y;
00038     unsigned sum = 0;
00039     for (int k = 0; k < gps; k++)
00040       {
00041         y = x[k];
00042         while (y != 0)
00043           {
00044             sum += y % 10;
00045             y /= 10;
00046           }
00047       }
00048      return sum;
00049   }
00050 };
00051 
00073 int
00074 main ()
00075 {
00076   bigN N (1);
00077 
00078 //evaluate 100!
00079   for (int k = 1; k <= 100; k++)
00080     N.times (k);
00081 
00082   cout << N.sum_of_digits() << endl;
00083   return 0;
00084 }
00085 
 All Classes Files Functions Variables