DEV Community

Simon Green
Simon Green

Posted on

The Palindromic Prime Cyclops Algorithm

Weekly Challenge 177

Challenge, My solution

Task 1: Damm Algorithm

Task

You are given a positive number, $n.

Write a script to validate the given number against the included check digit.

My solution

Thanks to the Wikipedia link provided, this proved a pretty straight forward task. Starting with 0, change the row that corresponds with the value of that digit in the current row. Repeat until we have processed all digits. If the last row is 0, it's validated. If it is something else, you've got dodgy data.

Examples

$ ./ch-1.py 5724
1

$ ./ch-1.py 5727
0
Enter fullscreen mode Exit fullscreen mode

Task 2: Palindromic Prime Cyclops

Task

Write a script to generate first 20 Palindromic Prime Cyclops Numbers.

A cyclops number is a number with an odd number of digits that has a zero in the center only.

My solution

Rather than checking if number is a palindrome with only a 0 in the center, I generate the left side of the number, and then add a zero and the number reversed. This is easier and quicker IMO. YMMV

Then I need to check that the number does not contain any other zeros, and if it is prime.

I then wrap this in a loop that increments the number by one, and continues until we find 20 solution. Finally, I print out the results.

Examples

$ ./ch-2.py 
101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, 1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821, 1360631, 1390931, 1490941, 1520251

$ ./ch-2.pl 
101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049, 1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821, 1360631, 1390931, 1490941, 1520251
Enter fullscreen mode Exit fullscreen mode

Top comments (0)