The Weekly Challenge ‐ Perl and Raku

CY's Take on The Weekly Challenge #176 ‐ Magic of 142857

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://theweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

Do tell me, if I am wrong or you strongly oppose my statements!

It's time for challenges in Week #176 !


I have started to learn Raku.

Task 1: Permuted Multiples

The number 142857 makes me think of Merry-Go-Rounds.

But the criteria of the task is less restricted: any permutations, not only rotated permutations, can be considered as a permuted multiple.

To make the program run faster, I use the following mathematical argument: since 6x and x has the same number of digits, and 2×6=12 > 10, the leftmost digit of x must be 1 and the leftmost digit of 6x must be one of 6, 7, 8 or 9 ‐ then one of digits of x must be one of 6, 7, 8 or 9.

Since the task is quite straight-forward, I only post the Raku solution:

image info

# The Weekly Challenge 176
# Task 1 Permuted Multiples
use v6;

my $t = 0;
my $k = 1;
while ($t < 1) {
    $k++;
    if $k ~~ /<[6789]>/ {
        if (check_good("1"~$k, Array(2,3,4,5,6))) {
            $t++;
            say "1"~$k;
        }
    }
}


sub check_good ($i, @arr) {
    my $count = 0;
    my $dc = arrange($i);
    for (@arr) {
        if (arrange($_*$i) eq $dc) {
            $count++;
        }
        else {
            last;
        }
    }
    return $count == @arr.elems;
}

sub arrange ($i) {
    return $i.split("").sort.join("");
}

Time:
real 4.840s
user 5.036s
sys 0.020s

Since the phenomenon of permuted multiples is so interesting, I did a little more investigation. Can we relax the criterion of our the task statement then 142857 is obtained again? The answer appeared surprisingly earlier than I thought.

First lazily modify a few bytes of the program:

# The Weekly Challenge 176
# perm.raku
use v6;

my $t = 0;
my $k = 1;
my $a = Array( some_numbers );
while ($t < 1) {
    $k++;
    if 1 {
        if (check_good($k, $a)) {
            $t++;
            say $k;
            say $a;
        }
    }
}


sub check_good ($i, @arr) {
    my $count = 0;
    my $dc = arrange($i);
    for (@arr) {
        if (arrange($_*$i) eq $dc) {
            $count++;
        }
        else {
            last;
        }
    }
    return $count == @arr.elems;
}

sub arrange ($i) {
    return $i.split("").sort().join("");
}

some numbers Smallest Positive Integer Returned Time
2,3,4,5,6142857 N/A
2125874 real 13.686s
user 13.787s
sys 0.028s
31035 real 0.292s
user 0.403s
sys 0.017s
41782 real 0.423s
user 0.557s
sys 0.043s
5142857 real 15.646s
user 15.734s
sys 0.036s
61386 real 0.317s
user 0.418s
sys 0.024s

See? 5 alone gives us 142857. How about combinations of the remaining numbers? All combinations of cardinal 2 give us 142857, waaa!

some numbers Smallest Positive Integer Returned Time
2,3142857 real 15.960s
user 16.053s
sys 0.040s
2,4142857 real 16.209s
user 16.306s
sys 0.032s
2,6142857 real 13.082s
user 13.166s
sys 0.053s
3,4142857 real 12.989s
user 13.077s
sys 0.028s
3,6142857 real 13.392s
user 13.473s
sys 0.024s
4,6142857 real 13.278s
user 13.362s
sys 0.040s

Note that before writing this blogpost, I used arrange($_*$i) == $dc instead of arrange($_*$i) eq $dc, but it produced results give us 18 for multiplier 6 (18×6=108), which is not we desired.

For fun:

some numbers Smallest Positive Integer Returned
71359
8113967
91089

Task 2: Reversible Numbers

Raku solution:

for (1..99) {
    push @arr, $_ if ($_ + flip $_ ) ~~ /^<[13579]>+$/ ;
}

Perl solution:

for (1..99) {
    push @arr, $_ if ($_ + (scalar reverse $_) ) =~ /^[13579]+$/ ;
}

The parentheses bounded scalar reverse $_ in the Perl solution is not a must. I prefer they exist this time maybe because recently while I coded a program, I wrote something like index(scalar reverse $board, $player, $reversed_position+2 ) ‐ it is a reversi program, ‐ and you can guess perl has interpreted the line as index(scalar (reverse ($board, $player, $reversed_position+2 ))) and will throw a syntax error message "Not enough arguments for index ...".

Stay alert and healthy! □


The image of the Merry-Go-Round is from Wikimedia Commons and the original author is Shinjiman.

Except from images and codes from other personnels, the content of this blogpost is released under a copyleft spirit. One may share (full or partial) content of this blogpost on other platform if you share it under the free and open content spirit.

link for CY's full codes: ch-1.pl, ch-2.pl, ch-1.raku, ch-2.raku


Contact on twitter: @e7_87.

Discuss via GitHub issues: here.

Email: fungcheokyin at gmail.com

Created Date: 7th August, 2022.