[Solved] Finding the n-th prime number that is palindrome in base K


Solution: change palindrome to

int palindrome ( int n,int base)
    {
        int isTrue = 1;
        int digitCount = digitCountBase(n,base);
        int power = intPow(base,digitCount-1);

        int original = n;
        while (n>0&& digitCount >0)
        {

            if (n%base != (original/power) % base &&digitCount!=1)
            {
                isTrue =0;
                return 0;

            }
            n=n/base;
            power = power /base;
            digitCount=digitCount-2;
        }

        return isTrue;

    }

How did I find the error:

  • You are doing only 2 things, primality testing and palindrome testing, so makes sense to check if these are working fine.
  • Primality testing is easy, count primes from 1 to 10^7 and compare to known values on google. In this case, this works
  • To test palindrome, pick a working solution from the internet (even if you canĀ“t submit their solution that uses arrays/strings, you can test with them!). Then iterate from 1 to 10^7 in a certain base and check that both functions return the same.
  • Testing with base 3, quickly saw that 56 was not giving same output. And the incorrect one was yours.
  • Then its a matter of fixing your function, which you now know which one is the problem and even have an example of where its not working

1

solved Finding the n-th prime number that is palindrome in base K