// Direct check of Legendre's conjecture: a prime in (n^2,(n+1)^2) for 1<=n<=N, deterministic Miller-Rabin (64-bit). #include #include #include typedef unsigned __int128 u128; typedef uint64_t u64; static u64 pw(u64 a,u64 e,u64 m){u64 r=1;a%=m;while(e){if(e&1)r=(u128)r*a%m;a=(u128)a*a%m;e>>=1;}return r;} static int isp(u64 n){ if(n<2)return 0; static const u64 B[]={2,3,5,7,11,13,17,19,23,29,31,37}; for(int i=0;i<12;i++){ if(n%B[i]==0) return n==B[i]; } u64 d=n-1; int s=0; while(!(d&1)){d>>=1;s++;} for(int i=0;i<12;i++){ u64 x=pw(B[i],d,n); if(x==1||x==n-1) continue; int ok=0; for(int r=1;r=b){printf("COUNTEREXAMPLE n=%llu\n",(unsigned long long)n);return 1;} } printf("Legendre holds for all 1<=n<=%llu (squares up to %.3e)\n",(unsigned long long)N,(double)(N+1)*(N+1)); }