Advertisement
brucehoult

Euler conjecture

Apr 7th, 2015
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. /*
  2. https://twitter.com/pickover/status/585271919991095296
  3.  
  4. $ gcc -O3 n_nth_powers.cpp -o n_nth_powers
  5. $ time ./n_nth_powers
  6. 27^5 + 84^5 + 110^5 + 133^5 = 144^5
  7.  
  8. real    0m0.018s
  9. user    0m0.018s
  10. sys 0m0.000s
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdint.h>
  15.  
  16. typedef uint64_t num;
  17.  
  18. const int MAX=7131; // 5th root of 2^64
  19.  
  20. int main()
  21. {
  22.   num power5[MAX+1];
  23.   for (num i=0; i<=MAX; ++i) power5[i] = i*i*i*i*i;
  24.  
  25.   for (int n4=1; n4<=MAX; ++n4){
  26.     num tot4 = power5[n4];
  27.     int guess = n4;
  28.     num guessp5 = power5[guess];
  29.     for (int n3=1; n3<=n4; ++n3){
  30.       num tot3 = tot4 + power5[n3];
  31.       for (int n2=1; n2<=n3; ++n2){
  32.         num tot2 = tot3 + power5[n2];
  33.         for (int n1=1; n1<=n2; ++n1){
  34.           num tot = tot2 + power5[n1];
  35.  
  36.           if (guessp5 < tot){
  37.             do {
  38.               guessp5 = power5[++guess];
  39.             } while (guessp5 < tot);
  40.           } else if (guessp5 > tot){
  41.             do {
  42.               guessp5 = power5[--guess];
  43.             } while (guessp5 > tot);
  44.           }
  45.           if (guessp5 == tot){
  46.             printf("%d^5 + %d^5 + %d^5 + %d^5 = %d^5\n", n1, n2, n3, n4, guess);
  47.             return 0;
  48.           }
  49.  
  50.         }
  51.       }
  52.     }
  53.   }
  54.  
  55.   return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement