[Purdue-pm] Perl Weekly Challenge
Mark Senn
mark at purdue.edu
Sun Sep 1 17:19:42 PDT 2019
This week's Perl Weekly Challenge problem was at
https://perlweeklychallenge.org/blog/perl-weekly-challenge-023/
It includes recaps of some previous problems by several people.
My blog for this week's forward difference task is at
https://engineering.purdue.edu/~mark/pwc-023-1.pdf
The Perl 6 code:
#
# Perl Weekly Challenge - 023
# Task #1
#
# See
# engineering.purdue.edu/~mark/blog/pwc-023-1.pdf
# for more information.
#
# Run using Perl v6.d.
use v6.d;
# Get command line arguments.
my $order = shift @*ARGS;
my @x = @*ARGS;
my $i = 1;
while @x.elems > 1 && $i <= $order {
# <<->> is a hyperoperator
# if @x = (10,20,30,40), then
@x = @x[1..*] <<->> @x[0..^*-1];
# gives (20-10,30-20,40-30) = (10,20,30)
say "order {$i++}: {@x}";
}
My blog for this week's factoring numbers task is at
https://engineering.purdue.edu/~mark/pwc-023-2.pdf
The Perl 6 code:
#
# Perl Weekly Challenge - 023
# Task #2
#
# See
# engineering.purdue.edu/~mark/blog/pwc-023-2.pdf
# for more information.
#
# Run using Perl v6.d.
use v6.d;
my $n = 228; # number to factor
# for 2, 3, 5, 7, ..., if the number is prime enter the loop
for (2, 3, *+2 ... *).grep({.is-prime}) -> $p {
while $n %% $p { # $n evenly divisible by the current prime?
say $p; # print the prime
$n div= $p; # divide $n by the prime
}
# Is $p > $n.sqrt?
# But, I suspect $p ** 2 > $n (= $p * $p > $n) , is faster to compute.
if ($p ** 2 > $n) {
$n.say;
last;
}
}
-mark
More information about the Purdue-pm
mailing list