Damm and Cyclops
Weekly challenge 177 — 8 August 2022
Week 177: 8 Aug 2022
Write a script to generate the 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.
Example 1: 101 Example 2: 16061 Example 3: 31013
Palindromic prime Cyclops numbers (PPCNs) are prime numbers with an odd count of digits with the only zero as the middle digit.
My first thought was to start at 100 and check every number for being a PPCN. This works, but isn't very efficient. My second thought was only to check numbers with an odd number of digits - so 100 to 999 and then 10000 to 99999 and so on. That was a little quicker.
However, my third and submitted algorithm looks like this:
Loop $j
up from 1 to something huge. $j
is going to be just the digits to the left of the central zero.
Discard any $j
that contains a zero.
Now create a candidate number which is the concatenation of $j
, 0 and $j
reversed. So that is palindromic and has a single 0 in the right place, and all that remains is to check that it's prime. I used Math::Prime::Util
to do that rather than write yet another sieve of Eratosthenes.
So far as I know, Perl has no intrinsic function to reverse a string, but this works:
join('', reverse(split('', $j)))
- that is, split the digits of the number into an array, reverse the array, and then join them together again. There may be faster methods, but using my (third) algorithm generates PPCNs up into the millions without noticeable delay.
#!/usr/bin/perl # Peter Campbell Smith - 2022-08-08 # PWC 177 task 2 use v5.28; use utf8; use warnings; use Math::Prime::Util 'is_prime'; my ($j, $count, $test, $string, $k); # loop from 1 upwards - $j is the digits before the middle 0 $k = 1; for $j (1 .. 2 ** 31) { # no good if $j has a zero in it next if $j =~ m|0|; # create $j . 0 . reverse of $j $test = $j . '0' . join('', reverse(split('', $j))); # but is it prime? next unless is_prime($test); # it is say qq[Output: PPCN $k = $test]; last if $k ++ == 20; }
Output: PPCN 1 = 101 Output: PPCN 2 = 16061 Output: PPCN 3 = 31013 Output: PPCN 4 = 35053 Output: PPCN 5 = 38083 Output: PPCN 6 = 73037 Output: PPCN 7 = 74047 Output: PPCN 8 = 91019 Output: PPCN 9 = 94049 Output: PPCN 10 = 1120211 Output: PPCN 11 = 1150511 Output: PPCN 12 = 1160611 Output: PPCN 13 = 1180811 Output: PPCN 14 = 1190911 Output: PPCN 15 = 1250521 Output: PPCN 16 = 1280821 Output: PPCN 17 = 1360631 Output: PPCN 18 = 1390931 Output: PPCN 19 = 1490941 Output: PPCN 20 = 1520251
Any content of this website which has been created by Peter Campbell Smith is in the public domain