Palindromic primes: Difference between revisions
Content deleted Content added
Added PL/I-80 example |
Added C solution |
||
Line 160: | Line 160: | ||
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 |
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 |
||
Palindromic primes 1-999: 20 |
Palindromic primes 1-999: 20 |
||
</pre> |
|||
=={{header|C}}== |
|||
<syntaxhighlight lang="C"> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <math.h> |
|||
#include <stdbool.h> |
|||
bool ispalindromic(int n) { |
|||
int i, j; |
|||
char s[20]; |
|||
itoa(n, s, 10); |
|||
i = 0; |
|||
j = strlen(s) - 1; |
|||
while (i < j) { |
|||
if (s[i] != s[j]) return false; |
|||
i++; |
|||
j--; |
|||
} |
|||
return true; |
|||
} |
|||
bool isprime(int n) { |
|||
int i, lim, count; |
|||
if (n < 2) return false; |
|||
if (n % 2 == 0) return (n == 2); |
|||
lim = (int) floor(sqrt((float) n)); |
|||
for (i = 3; i <= lim; i += 2) { |
|||
if (n % i == 0) return false; |
|||
} |
|||
return true; |
|||
} |
|||
int main(void) { |
|||
int i, count; |
|||
count = 0; |
|||
for (i = 2; i <= 1000; i++) { |
|||
if (isprime(i) && ispalindromic(i)) { |
|||
printf("%d ", i); |
|||
count++; |
|||
} |
|||
} |
|||
printf("\nFound %d palindromic primes\n", count); |
|||
return EXIT_SUCCESS; |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 |
|||
Found 20 palindromic primes |
|||
</pre> |
</pre> |
||