Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- https://twitter.com/pickover/status/585271919991095296
- $ gcc -O3 n_nth_powers.cpp -o n_nth_powers
- $ time ./n_nth_powers
- 27^5 + 84^5 + 110^5 + 133^5 = 144^5
- real 0m0.018s
- user 0m0.018s
- sys 0m0.000s
- */
- #include <stdio.h>
- #include <stdint.h>
- typedef uint64_t num;
- const int MAX=7131; // 5th root of 2^64
- int main()
- {
- num power5[MAX+1];
- for (num i=0; i<=MAX; ++i) power5[i] = i*i*i*i*i;
- for (int n4=1; n4<=MAX; ++n4){
- num tot4 = power5[n4];
- int guess = n4;
- num guessp5 = power5[guess];
- for (int n3=1; n3<=n4; ++n3){
- num tot3 = tot4 + power5[n3];
- for (int n2=1; n2<=n3; ++n2){
- num tot2 = tot3 + power5[n2];
- for (int n1=1; n1<=n2; ++n1){
- num tot = tot2 + power5[n1];
- if (guessp5 < tot){
- do {
- guessp5 = power5[++guess];
- } while (guessp5 < tot);
- } else if (guessp5 > tot){
- do {
- guessp5 = power5[--guess];
- } while (guessp5 > tot);
- }
- if (guessp5 == tot){
- printf("%d^5 + %d^5 + %d^5 + %d^5 = %d^5\n", n1, n2, n3, n4, guess);
- return 0;
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement