Project Euler Homework

028.cpp

Go to the documentation of this file.
00001 
00013 #include <iostream>
00014 using namespace std;
00015 
00016 const unsigned length = 1001;
00017 class spiral
00018 {
00019   unsigned x;
00020 public:
00021     spiral ()
00022   {
00023     x = 1;
00024   }
00025   bool end ()
00026   {
00027     return (x > length * length + 1);
00028   }
00029   void next ()
00030   {
00031     static unsigned short count = 1;
00032     static unsigned term = 2;
00033 
00034     x += term;
00035     count++;
00036     if (count == 5)
00037       {
00038         term += 2;
00039         count = 1;
00040       }
00041   }
00042   unsigned value ()
00043   {
00044     return x;
00045   }
00046 };
00047 
00048 int
00049 main ()
00050 {
00051 
00052   unsigned sum = 0;
00053   for (spiral a; !a.end (); a.next ())
00054   {  sum += a.value ();
00055   //cout << a.value()<<endl;
00056   }
00057 
00058   cout << sum << endl;
00059 
00060   return 0;
00061 }
00062 
 All Classes Files Functions Variables