以下是一些 的正整数解
利用多线程库在28核56线程CPU上跑大概几分钟就能出第一组解,后面的几组是在64核128线程CPU上跑了8小时找出来的。
#include<math.h> #include<stdio.h> #include<stdlib.h> #include<threads.h> const unsigned long NUM_THREADS = 1024; void solution(void*); int main(){ thrd_t threads[NUM_THREADS]; for(unsigned long t=4; t<NUM_THREADS; t++){ thrd_create(&threads[t], (thrd_start_t)solution, (void*)t); } thrd_exit(EXIT_SUCCESS); return EXIT_SUCCESS; } void solution(void* p){ long s = (long)p; printf("%ld: pthread ID - %lu
", s, thrd_current()); for(long x=1; x<s-3; ++x){ for(long y=x; x+y<s-2; ++y){ for(long z=y; x+y+z<=s-z; ++z){ long w = s-x-y-z; double d = pow(x,4)+pow(y,4)+pow(z,4)+pow(w,4); if(abs(pow(round(sqrt(sqrt(d))), 4)-d)<1e-3){ s = (long)round(sqrt(sqrt(d))); printf("%ld: %ld %ld %ld %ld
", s,x,y,z,w); } } } } }
编译命令(文件名为main.c)
cc main.c -lm -pthread -o main