The Bear's Den

Enter at your own risk

Valid Cyclops

Task 1: Damm Algorithm

Submitted by: Mohammad S Anwar


You are given a positive number, $n.

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

Please checkout the wikipedia page for information.

Example 1

Input: $n = 5724
Output: 1 as it is valid number

Example 2

Input: $n = 5727
Output: 0 as it is invalid number

Solution

Despite the heavy mathematics behind Damm’s algorithm, its implementation is simple. All we need is the matrix representing the quasigroup.

Perl

use strict;
use warnings;
use experimental 'signatures';

sub damm ($n) {
    state $m = [
        [0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
        [7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
        [4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
        [1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
        [6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
        [3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
        [5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
        [8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
        [9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
        [2, 5, 8, 1, 4, 3, 6, 7, 9, 0],
    ];
    my $x = 0;
    for my $y (split //, $n) {
        $x = $m->[$x][$y];
    }
    0 + !$x;
}

See the full solution to task 1.

J

This is a perfect use-case for J’s “Fold Single Forward” conjunction F...

This conjunction generates a loop that returns a single value. Consider the J sentence:

\[x\ u\ \operatorname{F}..\ v\ y_0\,y_1 \ldots y_{k-1}\]

This calculates:

\[\begin{align*} v_0 &= x\ v\ y_0 \\ v_i &= v_{i-1}\ v\ y_i \text{ for } 0 < i < k \end{align*}\]

and provides the final result:

\[u\ v_{k-1}\]

Using these settings

\(x\) \(0\)
\(y_0\,y_1 \ldots y_{k-1}\) \(n\)’s digits
\(x\ v\ y\) \(x \ast y\), i.e. \(m_{xy}\)
\(u\ y\) \(1 - \operatorname{sign}(y)\)

yields:

damm =: _(adverb define)
  mx =. 0 ". S:0 cutopen {{)n
    0 3 1 7 5 9 8 6 4 2
    7 0 9 2 1 5 4 8 6 3
    4 2 0 6 8 7 1 3 5 9
    1 7 5 0 9 8 3 4 2 6
    6 1 2 3 0 4 5 9 7 8
    3 6 7 4 2 0 9 5 8 1
    5 8 6 9 7 2 0 1 3 4
    8 9 4 5 3 6 2 0 1 7
    9 4 3 8 6 1 7 2 0 5
    2 5 8 1 4 3 6 7 9 0
}}
  0&$: : ([ -.@* F.. (mx {::~  ,~ ) 10&#.inv@])
)
   damm 5724
1
   damm 5727
0

See the full solution.

Task 2: Palindromic Prime Cyclops

Submitted by: Mohammad S Anwar


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.

Output

101, 16061, 31013, 35053, 38083, 73037, 74047, 91019, 94049,
1120211, 1150511, 1160611, 1180811, 1190911, 1250521, 1280821,
1360631, 1390931, 1490941, 1520251

Solution

This is a reproduction of my solution dated 8 Aug 2022.

Note that List::Gen has been modified to use Math::Prime::Util’s is_prime function if it is available.


Build a generator for palindromic prime cyclops in base 10.

Loop over palindromic cyclops starting with and ending in 1, 3, 7 or 9 and filter prime numbers. The approach seems to be useful as this sequence can easily be constructed and has large steps.

use strict;
use warnings;
use Math::Prime::Util 'is_prime';
use List::Gen ':iterate';

sub gen_palindromic_prime_cyclops {
    # Map digits to be skipped onto their successor.
    my %next = (0 => 1, 2 => 3, 4 => 7, 8 => 9);

    # Iterator for the head of palindromic cyclops that are prime
    # candidates, i.e. do not end in 2, 4, 5, 6 or 8.
    iterate_stream {
        # Increment the head.  If the result starts with one of the
        # excluded leading digits or contains a zero, map the found
        # digit to the next possible and replace the subsequent digits
        # with ones.  This gives the head of the next possibly prime
        # palindromic cyclops number.
        ($_ + 1) =~ s/(^[248]|0)(.*)/$next{$1} . '1' x length($2)/er;
    }
    ->from(1)
    ->map(sub {
            # Construct the full palindrome from the head.
            $_ . '0' . reverse;
        })
    # ->grep('prime') is *way* to slow.
    ->filter(sub {is_prime $_});
}

See the full solution to task 2.