Project Euler Homework

026.cpp

Go to the documentation of this file.
00001 
00018 using namespace std;
00019 #include <iostream>
00020 #include <vector>
00021 #include "026_prime_table.h"
00022 
00023 typedef unsigned long long verylong;
00024 
00025 const verylong segment_max = 1e13;
00026 
00027 const unsigned digits_per_segments = 13;
00028 
00029 
00030 class bigNum
00031 {
00032         vector<verylong> segments;
00033 public:
00034         bigNum(unsigned);
00035         void times10plus9();
00036         unsigned mod(unsigned);
00037         unsigned digits();
00038         void print_all();
00039 };
00040 
00041 bigNum::bigNum(unsigned x)
00042 {
00043         //assume x<max
00044         segments.push_back(x);
00045 }
00046 
00047 void bigNum::times10plus9()
00048 {
00049         for (vector<verylong>::iterator p=segments.begin();p!=segments.end();p++)
00050                 *p *= 10;
00051         segments.front() +=9;
00052 
00053         for (vector<verylong>::iterator p=segments.begin();p!=segments.end();p++) {
00054                 unsigned count_out = *p / segment_max;
00055 
00056                 if (count_out == 0) break;
00057                 *p -= count_out * segment_max;
00058 
00059                 if (p+1 == segments.end())
00060                         segments.push_back(count_out);
00061                 else
00062                         *(p+1) += count_out;
00063         }
00064 }
00065 
00066 unsigned bigNum::mod(unsigned y)
00067 {
00068         vector<verylong> seg2=segments;
00069 
00070         for (vector<verylong>::reverse_iterator p = seg2.rbegin()+1; p!=seg2.rend();p++) {
00071                 unsigned count_out = *(p-1) % y;
00072                 *p += count_out * segment_max;
00073                 //print_all();
00074         }
00075         return *(seg2.begin()) % y;
00076 }
00077 
00078 unsigned bigNum::digits()
00079 {
00080         unsigned no_of_segments = segments.size();
00081         unsigned digits_at_back = 0;
00082         for (unsigned k = segments.back();k>0;k/=10)
00083                 digits_at_back++;
00084 
00085         return digits_at_back + digits_per_segments * (no_of_segments - 1);
00086 }
00087 
00088 void bigNum::print_all()
00089 {
00090         for (vector<verylong>::reverse_iterator p=segments.rbegin();p!=segments.rend();p++)
00091                 cout << *p;
00092         cout << endl;
00093 }
00094 
00095 
00096 using namespace std;
00097 unsigned period(unsigned d)
00098 {
00099         bigNum nines(9);
00100 
00101         while (nines.mod(d) != 0) {
00102                 nines.times10plus9();
00103         }
00104         return nines.digits();
00105 }
00106 
00107 int main()
00108 {
00109         prime_table primes;
00110         unsigned max_period = 0;
00111         unsigned max_d=0;
00112 
00113 for (unsigned d = 7; d < 1000; d = primes.next(d)) 
00114 {
00115                 unsigned temp = period(d);
00116                 if (temp > max_period) {
00117                         max_period = temp;
00118                         max_d = d;
00119                 }
00120                 
00121                 cout << d << '\t' << temp << endl;
00122         }
00123         cout << max_d << '\t' << max_period << endl;
00124         return 0;
00125 }
 All Classes Files Functions Variables