1

I am trying to make a program in which a number is taken as input and the program displays the smallest number bigger than the input which is both prime and palindrome. But this program does not give any output. Can you please give me the reason? I am very new to programming, and it would be great if you could explain me the reason:

The program is as follows:

#include <stdio.h>
#include <math.h>

int checkp(long long int a);

int main() {
    long long int n, i;

    scanf("%lld", &n);

    for (i = (n + 1);; i++) {
        long long int reverse = 0, rem, temp;
        int check;
        temp = n;
        while (temp != 0) {
            rem = temp % 10;
            reverse = reverse * 10 + rem;
            temp /= 10;
        }

        if (reverse == i) {
            check = checkp(i);
            if (check == 1) {
                return 0;
            }
        }
    }
}

int checkp(long long int a) {
    long long int b, i;
    b = sqrt(a);
    int flag = 0;
    for (i = 2; i <= b; i++) {
        if (a % i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 0) {
        printf("%lld", a);
        return 1;
    } else
        return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
P parker
  • 81
  • 1
  • 4
  • 2
    If you paste the whole code and ask what is wrong I would suggest debugging step by step and see what is happening right way to learn – Gopi Feb 01 '15 at 14:47
  • I agree with @Gopi: this does sound like a perfect opportunity to learn to use a debugger. That'll save you *a lot* of time in the long run. – NPE Feb 01 '15 at 14:52
  • For long running programs I like to include some sort of feedback to know it is working. Somthing like keeping a counter and ... `if (counter++ % 1000 == 0) putchar('.');` – pmg Feb 01 '15 at 15:04

2 Answers2

1

Just change temp=n; to temp=i; you should check whether i is a palindrome and not n.

0

As correctly diagnosed by srivatsan ramesh, you should initialize temp to i instead of n in the core loop.

Note that palindromes in base 10 with an even number of digits are multiples of 11, therefore the only palindrome with an even number of digits is 11 itself.

You can greatly reduce the lag by counting the number of digits and adjusting i when it becomes even:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int checkp(long long int a);

int main(int argc, char *argv[]) {
    long long int n, i;

    if (argc > 1) {
        n = strtoll(argv[1], NULL, 0);
    } else {
        scanf("%lld", &n);
    }

    for (i = n + 1;; i++) {
        long long int reverse = 0, rem, temp;
        int digits = 0;
        temp = i;
        while (temp != 0) {
            rem = temp % 10;
            reverse = reverse * 10 + rem;
            temp /= 10;
            digits++;
        }
        if (digits % 2 == 0 && digits > 2) {
            for (i = 1; digits-- > 0; i *= 10)
                continue;
            i -= 1;
            continue;
        }
        if (reverse == i && checkp(i)) {
            printf("%lld\n", i);
            if (n > 0)
                return 0;
        }
    }
}

int checkp(long long int a) {
    long long int i;

    if ((a % 2) == 0)
        return a == 2;

    for (i = 3; i * i <= a; i += 2) {
        if (a % i == 0) {
            return 0;
        }
    }
    return 1;
}

It would also be must more efficient to generate the palindromes instead of enumerating all numbers and testing for palindromes. This would reduce the complexity by an order of magnitude, O(sqrt(N)) instead of O(N).

chqrlie
  • 131,814
  • 10
  • 121
  • 189