0

A palindromic prime is a prime number that can reads the same even if you reverse the digits, e.g. 11, 101, 121, 383, 3443. I want to write a program that asks the user for two numbers, a and b, the program must recursively find all the palindromic primes between a and b and print them out.

I have a function, palindrome, that can recursively reverse the prime after you turn it into a string. I am struggling to create a recursive function that checks if a number is a prime and palindromic as well, it must also save these values and print them out.

def palindrome(seq):
    if seq == '':
        return seq
    else:
        return palindrome_reverse(seq[1:]) + seq[0]

This is the expected output of the program:

Enter first number: 50
Enter second number: 150
The palindromic primes between 50 and 150 are:
101
121
131
Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 4
    See if you can update your question with the parts of the program you have written even if they don't work. – quamrana Apr 24 '19 at 09:56
  • 2
    you need a method to check for primalty (or one that lists primes). many options! e.g. here: https://codegolf.stackexchange.com/a/74370 or here https://stackoverflow.com/questions/30553925/adding-wheel-factorization-to-an-indefinite-sieve/30563958#30563958 or here https://stackoverflow.com/questions/31120986/optimize-sieve-of-eratosthenes-further/31122596#31122596 – hiro protagonist Apr 24 '19 at 09:58

1 Answers1

1

I'm here for the recursion part :-)

I don't know python, so I will share a solution in java. And as primalty is not a recursion problem, I let you introduce it in the algorithm ;-)

Here is my solution :

package palyndromic;
public class Palindrome {   
    static boolean isPalindrome(String number)
    {
        if(number.length() < 2)
        {   // Your string is either empty or one character long. ==> Palindrome
            return true;
        }
        else if(number.length() == 2)
        {   // if your number is 2 char only, it is a palindrome when both characters are the same
            return number.charAt(0) == number.charAt(1);
        }
        else
        {
            // If your number is more than 2 chars, the bounds must be identical and the inner number must be a palindrome too.
            boolean boundAreSame = number.charAt(0) == number.charAt(number.length()-1);
            boolean innerStringIsPalindrome = isPalindrome(number.substring(1, number.length()-1)); 
            return boundAreSame && innerStringIsPalindrome;
        }
    }
    static boolean isPrime(int number)
    {
        return true; // Implement it as you wish
    }

    public static void main(String[] args) {

            for (int i = 50; i < 150; i++) {
                if(isPalindrome(Integer.toString(i)) && isPrime(i))
                    System.out.println(i + " is a prime palindrome");
            }           
    }

}

outputs :

55 is a prime palindrome
66 is a prime palindrome
77 is a prime palindrome
88 is a prime palindrome
99 is a prime palindrome
101 is a prime palindrome
111 is a prime palindrome
121 is a prime palindrome
131 is a prime palindrome
141 is a prime palindrome

Welcome on StackOverflow =)

A. Ocannaille
  • 306
  • 4
  • 14