// Search for numbers N <= X palindromic (with >= 2 digits) in k consecutive bases b..b+k-1. // Enumerates palindromes in the smallest base b with mind..maxd digits; tests b+1, b+2, b+3. // usage: pal X mind bmin bmax -> prints "3 N b" for 3-palindromes, "4 N b" for 4-palindromes #include #include #include typedef unsigned __int128 u128; typedef uint64_t u64; static int ispal(u64 n, u64 B){ if(nX) break;} if(p>X) break; int h=(d+1)/2; u64 dig[64]; // half digits // iterate over half-digit vectors: first digit 1..b-1, others 0..b-1 for(int i=0;iX){ /* increase leading digit makes larger; skip to next leading digit */ if(dig[0]+1>=b) break; dig[0]++; for(int i=1;iX) break; continue; } tested++; u64 n=(u64)N; if(ispal(n,b+1)&&ispal(n,b+2)){ n3++; printf("3 %llu %llu %d\n",(unsigned long long)n,(unsigned long long)b,d); if(ispal(n,b+3)){ n4++; printf("4 %llu %llu %d\n",(unsigned long long)n,(unsigned long long)b,d);} } int i=h-1; while(i>=0){ dig[i]++; if(dig[i]