The main problem here is that we need some way to generate = palindromes. Since=20 there are only about 10,000 palindromes less than 100,000,000, we can = just test=20 each one to see if it is prime and in the range.=20 To generate a palindrome, we start with the first half and reverse = it. The=20 trick is that we can repeat the middle character or not repeat the = middle=20 character. I call a palindrome with a repeated middle character "even" = (because=20 it is of even length) and one without "odd". So from the string "123", = we can=20 generate the even palindrome "123321" or the odd palindrome "12321".=20 We can generate all palindromes by doing the following:=20 length 1: generate odd palindromes using 1..9=20 length 2: generate even palindromes using 1..9=20 length 3: generate odd palindromes using 10..99=20 length 4: generate even palindromes using 10..99=20 ... The "generate" function does exactly this, using "genoddeven" to = first=20 generate the odd palindromes for a range and then the even ones.=20 The "gen" function generates an even or odd palindrome for a number = by=20 converting it to a string, making a palindrome, and converting the = resulting=20 string back to a number. Then it tests to see if the number is in the = range and=20 prime. If so, it is printed.=20 We could speed this up in a number of ways: use a faster primality = test,=20 don't generate palindromes past "b", etc. But this is already plenty = fast, and=20 doing such things makes the program more complex and might introduce = bugs. #include #include #include #include FILE *fout; long a, b; int isprime(long n) { long i; if(n =3D=3D 2) return 1; if(n%2 =3D=3D 0) return 0; for(i=3D3; i*i <=3D n; i+=3D2) if(n%i =3D=3D 0) return 0; return 1; } void gen(int i, int isodd) { char buf[30]; char *p, *q; long n; sprintf(buf, "%d", i); p =3D buf+strlen(buf); q =3D p - isodd; while(q > buf) *p++ =3D *--q; *p =3D '\0'; n =3D atol(buf); if(a <=3D n && n <=3D b && isprime(n)) fprintf(fout, "%ld\n", n); } void genoddeven(int lo, int hi) { int i; for(i=3Dlo; i<=3Dhi; i++) gen(i, 1); for(i=3Dlo; i<=3Dhi; i++) gen(i, 0); } void generate(void) { genoddeven(1, 9); genoddeven(10, 99); genoddeven(100, 999); genoddeven(1000, 9999); } void main(void) { FILE *fin; fin =3D fopen("pprime.in", "r"); fout =3D fopen("pprime.out", "w"); assert(fin !=3D NULL && fout !=3D NULL); fscanf(fin, "%ld %ld", &a, &b); generate(); exit (0); } master_zed writes:=20 The problem can be simplified slightly by noticing that any even = palindrome=20 is divisible by 11. Therefore, 11 is the ONLY even prime palindrome. = This=20 eliminates the need to deal with 2 cases: #include #include #include #include FILE *fout; long a, b; int isprime(long n) { long i; if(n =3D=3D 2) return 1; if(n%2 =3D=3D 0) return 0; for(i=3D3; i*i <=3D n; i+=3D2) if(n%i =3D=3D 0) return 0; return 1; } void gen(int i) { char buf[30]; char *p, *q; long n; sprintf(buf, "%d", i); p =3D buf+strlen(buf); q =3D p - 1; while(q > buf) *p++ =3D *--q; *p =3D '\0'; n =3D atol(buf); if(a <=3D n && n <=3D b && isprime(n)) fprintf(fout, "%ld\n", n); } void generate(void) { int i; for (i =3D 1; i <=3D 9; i++) gen(i); if(a <=3D 11 && 11 <=3D b) fprintf(fout, "11\n"); for (i =3D 10; i <=3D 9999; i++) gen(i); } void main(void) { FILE *fin; fin =3D fopen("pprime.in", "r"); fout =3D fopen("pprime.out", "w"); assert(fin !=3D NULL && fout !=3D NULL); fscanf(fin, "%ld %ld", &a, &b); generate(); exit (0); } Coach Rob writes:=20 I guess I have a slightly different coding style than the previous = two=20 solutions. This is the same idea but coded a bit more tightly (thanks to = Michael=20 Coblenz for its kernel):=20 #include #include #include int primelist[100000]; int nprimes; int isPrime(int num); int reverse2(int i, int j); int compare(const void *p, const void *q) { return *(int *)p-*(int *)q; = } void main (void) { ifstream infile("pprime.in"); ofstream outfile("pprime.out");=20 int i, j, begin, end, num; infile>>begin>>end; if (begin <=3D 11 && 11 <=3Dend) primelist[nprimes++] =3D 11; for (j =3D 0; j <=3D 999; j++) for (i =3D 0; i <=3D 9; i++) { num =3D reverse2(j,i); if (num >=3D begin && num <=3Dend && = isPrime(num))=20 primelist[nprimes++] =3D num; } qsort(primelist, nprimes, sizeof(int), compare); for (i =3D 0; i < nprimes; i++) outfile << primelist[i] << "\n"; } int reverse2(int num, int middle) { int i, save=3Dnum, digit, combino =3D 1; for (i =3D 0; num; num /=3D 10) { digit =3D num % 10; i =3D 10 * i + digit; combino *=3D 10; } return i+10*combino*save+combino*middle; } =09 int isPrime(int num) { int i; if (num <=3D 3) return 1; if (num%2 =3D=3D 0 || num%3 =3D=3D0) return 0; for (i =3D 5; i*i <=3D num; i++) if (num %i =3D=3D0) return 0; return 1; } And here is another tightly coded solution from Anton = Titov:=20 I guess you may find intresting my solution to Prime Palindromes as I = use a=20 function to generate palindromes, that is purely arythmetical it does = not use=20 strings, sprintf, reversion or other things. It uses recursion, but my = guess is=20 that it will outpreform all other functions listed.=20 Function void genPalind(int num, int add, int mulleft, int mulright)=20 expects 4 parameters, first is the number (N) of digits you want for = your=20 palindromes, second should be 0 for direct calls, third should be = 10^(N-1) for=20 direct calls and last one should be 1 for direct calls. #include = #include #include #include FILE *f; int a, b; int isPrime(int num); void genPalind(int num, int add, int mulleft, int mulright); void tryPalind(int num); int main(){ int i; char first; f=3Dfopen("pprime.in", "r"); fscanf(f, "%d%d", &a, &b); fclose(f); f=3Dfopen("pprime.out", "w"); if (a<=3D5) fprintf(f, "%i\n", 5); if (a<=3D7 && b>=3D7) fprintf(f, "%i\n", 7); if (a<=3D11 && b>=3D11) fprintf(f, "%i\n", 11); genPalind(3, 0, 100, 1); genPalind(5, 0, 10000, 1); genPalind(7, 0, 1000000, 1); fclose(f); } void tryPalind(int num){ if (!(num&1)) return; if (numb) return; if (!(num%3) || !(num%5) || !(num%7)) return; if (!isPrime(num)) return; fprintf(f, "%d\n", num); } void genPalind(int num, int add, int mulleft, int mulright){ int i, nmulleft, nmulright; if (num=3D=3D2){ for (i=3D0; i<10; i++) tryPalind(add+mulleft*i+mulright*i); } else if (num=3D=3D1){ for (i=3D0; i<10; i++) tryPalind(add+mulright*i); } else { nmulleft=3Dmulleft/10; nmulright=3Dmulright*10; num-=3D2; for (i=3D0; i<10; i++) genPalind(num, add+i*mulleft+i*mulright, nmulleft, nmulright); } } int isPrime(int num){ int koren, i; koren=3D(int)sqrt(1.0*num); for (i=3D11; i<=3Dkoren; i+=3D2) if (!(num%i)) return 0; return 1; }