Project Euler Homework

016.cpp

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