Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <time.h>
- #include <windows.h>
- typedef time_t prime_t;
- prime_t reverseDigits(prime_t num)
- {
- prime_t rev_num = 0;
- while(num > 0)
- {
- rev_num = rev_num*10 + num%10;
- num = num/10;
- }
- return rev_num;
- }
- prime_t nextOddPalindrome(prime_t n) {
- do {
- n += 2;
- } while(n != reverseDigits(n));
- return n;
- }
- int isPrime(prime_t n) {
- int i;
- if(n < 2)
- return 0;
- else if(n == 2)
- return 1;
- for(i = 3; i < sqrt(n) + 1; i+=2)
- if(n % i == 0)
- return 0;
- return 1;
- }
- int main(int argc, char **argv)
- {
- time_t t = time(NULL);
- printf("next prime palindrome of %I64u is ", t);
- unsigned int s = GetTickCount();
- if(t % 2 == 0)
- t--;
- do {
- t = nextOddPalindrome(t);
- } while(!isPrime(t));
- printf("%I64u\n", t);
- printf("elapsed %lu milliseconds", GetTickCount() - s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement