Advertisement
Guest User

Challenge 28(int)

a guest
Mar 21st, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include "../TTMATH/ttmath.h" // for 128 uints
  3.  
  4. #define uint128 ttmath::UInt<2> // define a 128 bit unsigned
  5.  
  6. int main()
  7. {
  8.     std::cout.sync_with_stdio(false);
  9.  
  10.     const uint128 wanted_result = 169179692512835000; // 128 - bit unsigned for the value
  11.     uint128 balls = 0;
  12.     unsigned long long base = 0;
  13.     unsigned inc = 1000; // initial value of the increaser/decreaser
  14.  
  15.     while (true) // loop forever
  16.     {
  17.         balls = base*(base+1)*(base+2) / 6; // the formula
  18.         std::cout << '\r' << base; // print the base every loop
  19.  
  20.         if (balls > wanted_result) // if result is larger than wanted result
  21.         {
  22.             if (inc != 1) inc /= 10; // decrease the incrementer if it's not 1
  23.             base -= inc; // decrease base with incrementer
  24.         }
  25.         else if (balls < wanted_result) // result is smaller that w.result
  26.             base += inc; // increase base
  27.         else // if it is equal
  28.             break;
  29.     }
  30.  
  31.     std::cout << "\rThe base for a pyramid with 169179692512835000 balls is...\n" << base << "!\n";
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement