Problem Description: Backwards Read Primes are primes that when read backwards in base 10 (from right to left) are a different prime. (This rules out primes which are palindromes.)
Examples:
13 17 31 37 71 73 are Backwards Read Primes
13 is such because it's prime and read from right to left writes 31 which is prime too. Same for the others.
Task
Find all Backwards Read Primes between two positive given numbers (both inclusive), the second one being greater than the first one. The resulting array or the resulting string will be ordered following the natural order of the prime numbers.
backwardsPrime(2, 100) => [13, 17, 31, 37, 71, 73, 79, 97]
backwardsPrime(9900, 10000) => [9923, 9931, 9941, 9967]
Level:6kyu
Link To Kata: Backwards Read Primes
In this kata we aim to print out such prime numbers in a given range where if the prime number is read backwards i.e. reversed then also it remains a prime number. Also we have to keep in mind that this list should exclude numbers which are palindrome i.e. the numbers which remain same when read backwards and forward. So we loop from starting point to the ending point and keep a number of checks at each point. In the starting we check that the number is not a single digit number. Then we check if the number is not a palindrome. If both the above conditions are satisfied then we check that the given number as well as the reversed form of given number are prime. If they both are prime then we push the number to an initialized answer array.
In the end we return the answer array as the solution to this kata.
Please go through the code for a better understanding.
Here is the link to code: Kata Day 50
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment