# RabbitFarm

### 2022-08-14

#### Cyclops Validation

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

## Part 1

You are given a positive number, \$n. Write a script to validate the given number against the included check digit.

### Solution

``````
use strict;
use warnings;
use boolean;

my @damm_matrix;
\$damm_matrix = [0, 7, 4, 1, 6, 3, 5, 8, 9, 2];
\$damm_matrix = [3, 0, 2, 7, 1, 6, 8, 9, 4, 5];
\$damm_matrix = [1, 9, 0, 5, 2, 7, 6, 4, 3, 8];
\$damm_matrix = [7, 2, 6, 0, 3, 4, 9, 5, 8, 1];
\$damm_matrix = [5, 1, 8, 9, 0, 2, 7, 3, 6, 4];
\$damm_matrix = [9, 5 ,7, 8, 4, 0, 2, 6, 1, 3];
\$damm_matrix = [8, 4, 1, 3, 5, 9, 0, 2, 7, 6];
\$damm_matrix = [6, 8, 3, 4, 9, 5, 1, 0, 2, 7];
\$damm_matrix = [4, 6, 5, 2, 7, 8, 3, 1, 0, 9];
\$damm_matrix = [2, 3, 9, 6, 8, 1, 4, 7, 5, 0];

sub damm_validation{
my(\$x) = @_;
my @digits = split(//, \$x);
my \$interim_digit = 0;
while(my \$d = shift @digits){
\$interim_digit = \$damm_matrix[\$d][\$interim_digit];
}
return boolean(\$interim_digit == 0);
}

MAIN:{
print damm_validation(5724) . "\n";
print damm_validation(5727) . "\n";
}
``````

### Sample Run

``````
\$ perl perl/ch-1.pl
1
0
``````

### Notes

Damm Validation really boils down to a series of table lookups. Once that is determined we need to encode the table and then perform the lookups in a loop.

## Part 2

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

### Solution

``````
use strict;
use warnings;
no warnings q/recursion/;
use Math::Primality qw/is_prime/;

sub n_cyclops_prime_r{
my(\$i, \$n, \$cyclops_primes) = @_;
return @{\$cyclops_primes} if @{\$cyclops_primes} == \$n;
push @{\$cyclops_primes}, \$i if is_prime(\$i) &&
length(\$i) % 2 == 1 &&
join("", reverse(split(//, \$i))) == \$i &&
(grep {\$_ == 0} split(//, \$i))   == 1 &&
do{my @a = split(//, \$i);
\$a[int(@a / 2)]
} == 0;
n_cyclops_prime_r(++\$i, \$n, \$cyclops_primes);
}

sub n_cyclops_primes{
my(\$n) = @_;
return n_cyclops_prime_r(1, \$n, []);
}

MAIN:{
print join(", ", n_cyclops_primes(20)) . "\n";
}
``````

### Sample Run

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

### Notes

I recently saw the word whipupitide used by Dave Jacoby and here is, I think, a good example of it. We need to determine if a number is prime, palindromic, and cyclops. In Perl we can determine all of these conditions very easily.

Just to add a bit of fun I decided to use a recursive loop. Out of necessity this will have a rather deep recursive depth, so we'll need to set `no warnings q/recursion/` or else perl will complain when we go deeper than 100 steps. We aren't using too much memory here, but if that were a concern we could do Perl style tail recursion with a `goto __SUB__` instead.

## References

Challenge 177

posted at: 17:59 by: Adam Russell | path: /perl | permanent link to this entry