diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-25 22:24:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-25 22:24:37 +0100 |
| commit | ef45d40fdc89955f6b22a13d27429fa2c6b1da14 (patch) | |
| tree | 596eafaf5dda76cbc264b481a31ac317c88e3e13 /challenge-109 | |
| parent | f806354488aff214f3dd3a0419b94dd73bb8acab (diff) | |
| parent | 3ee248564437db1335e9d57384d083a03aea233a (diff) | |
| download | perlweeklychallenge-club-ef45d40fdc89955f6b22a13d27429fa2c6b1da14.tar.gz perlweeklychallenge-club-ef45d40fdc89955f6b22a13d27429fa2c6b1da14.tar.bz2 perlweeklychallenge-club-ef45d40fdc89955f6b22a13d27429fa2c6b1da14.zip | |
Merge pull request #3956 from dcw803/master
imported my solutions to this week's challenge..
Diffstat (limited to 'challenge-109')
| -rw-r--r-- | challenge-109/duncan-c-white/README | 85 | ||||
| -rwxr-xr-x | challenge-109/duncan-c-white/perl/ch-1.pl | 52 | ||||
| -rwxr-xr-x | challenge-109/duncan-c-white/perl/ch-2.pl | 101 |
3 files changed, 200 insertions, 38 deletions
diff --git a/challenge-109/duncan-c-white/README b/challenge-109/duncan-c-white/README index 5b35941c97..18bce46ea5 100644 --- a/challenge-109/duncan-c-white/README +++ b/challenge-109/duncan-c-white/README @@ -1,56 +1,65 @@ -Task 1: "Locate Memory +Task 1: "Chowla Numbers -Write a script to declare a variable or constant and print it's location in -the memory. -" +Write a script to generate first 20 Chowla Numbers, named after, +Sarvadaman D. S. Chowla, a London born Indian American mathematician. It +is defined as: -My notes: umm, does this just mean "use \ and %p", or something more subtle? +C(n) = sum of divisors of n except 1 and n +NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40]. -Task 2: "Bell Numbers +Output: -Write a script to display top 10 Bell Numbers. Please refer to +0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 +" -https://en.wikipedia.org/wiki/Bell_number +My notes: easy and fun. -for more informations. -Example: +Task 2: "Four Squares Puzzle -B0: 1 as you can only have one partition of zero element set +You are given four squares as below with numbers named a,b,c,d,e,f,g. -B1: 1 as you can only have one partition of one element set {a}. + (1) (3) + =============== =============== + | | | | + | a | | e | + | |(2) | | (4) + | --------------- -------------- + | | | | | | | | + | | b | | d | | f | | + | | | | | | | | + | | | | | | | | + ==========|==== ====|========== | + | c | | g | + | | | | + | | | | + --------------- -------------- -B2: 2 +Write a script to place the given unique numbers in the square box so +that sum of numbers in each box is the same. - {a}{b} - {a,b} +Example -B3: 5 +Input: 1,2,3,4,5,6,7 - {a}{b}{c} - {a,b}{c} - {a}{b,c} - {a,c}{b} - {a,b,c} +Output: -B4: 15 + a = 6 + b = 4 + c = 1 + d = 5 + e = 2 + f = 3 + g = 7 - {a}{b}{c}{d} - {a,b,c,d} - {a,b}{c,d} - {a,c}{b,d} - {a,d}{b,c} - {a,b}{c}{d} - {a,c}{b}{d} - {a,d}{b}{c} - {b,c}{a}{d} - {b,d}{a}{c} - {c,d}{a}{b} - {a}{b,c,d} - {b}{a,c,d} - {c}{a,b,d} - {d}{a,b,c} + Box 1: a + b = 6 + 4 = 10 + Box 2: b + c + d = 4 + 1 + 5 = 10 + Box 3: d + e + f = 5 + 2 + 3 = 10 + Box 4: f + g = 3 + 7 = 10 " -My notes: Bell's triangle has a simple algorithm; let's use that! +My notes: sounds simple enough. Find a,b,c,d,e,f st a+b = b+c+d = d+e+f = f+g +Of course, we'll need to try all permutations of the values given. There +are lots of CPAN modules (eg. Algorithm::Permute), but here I decided to +generate the permutations myself via Rosetta Stone code.. diff --git a/challenge-109/duncan-c-white/perl/ch-1.pl b/challenge-109/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..a0823e9de1 --- /dev/null +++ b/challenge-109/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +# +# Task 1: "Chowla Numbers +# +# Write a script to generate first 20 Chowla Numbers, named after, +# Sarvadaman D. S. Chowla, a London born Indian American mathematician. It +# is defined as: +# +# C(n) = sum of divisors of n except 1 and n +# +# NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40]. +# +# Output: +# +# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 +# " +# +# My notes: easy and fun. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +#use Data::Dumper; + + +# +# my $x = chowla($n); +# Compute and return the $n'th Chowla number: +# the sum of the divisors of $n except 1 and $n. +# +fun chowla( $n ) +{ + my $sum = 0; + foreach my $div (2..$n-1) + { + $sum += $div if $n%$div==0; + } + return $sum; +} + + +die "Usage: chowla N\n" unless @ARGV==1; +my $n = shift; + +foreach my $i (1..$n) +{ + my $x = chowla($i); + say "c($i) = $x"; +} + diff --git a/challenge-109/duncan-c-white/perl/ch-2.pl b/challenge-109/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..1fec2892e6 --- /dev/null +++ b/challenge-109/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl +# +# Task 2: "Four Squares Puzzle +# +# You are given four squares as below with numbers named a,b,c,d,e,f,g. +# +# (1) (3) +# =============== =============== +# | | | | +# | a | | e | +# | |(2) | | (4) +# | --------------- -------------- +# | | | | | | | | +# | | b | | d | | f | | +# | | | | | | | | +# | | | | | | | | +# ==========|==== ====|========== | +# | c | | g | +# | | | | +# | | | | +# --------------- -------------- +# +# Write a script to place the given unique numbers in the square box so +# that sum of numbers in each box is the same. +# +# Example +# +# Input: 1,2,3,4,5,6,7 +# +# Output: +# +# a = 6 +# b = 4 +# c = 1 +# d = 5 +# e = 2 +# f = 3 +# g = 7 +# +# Box 1: a + b = 6 + 4 = 10 +# Box 2: b + c + d = 4 + 1 + 5 = 10 +# Box 3: d + e + f = 5 + 2 + 3 = 10 +# Box 4: f + g = 3 + 7 = 10 +# " +# +# My notes: sounds simple enough. Find a,b,c,d,e,f st a+b = b+c+d = d+e+f = f+g +# Of course, we'll need to try all permutations of the values given. There +# are lots of CPAN modules (eg. Algorithm::Permute), but here I generate the +# permutations myself via Rosetta Stone code.. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +#use Data::Dumper; + +my $debug = 0; +die "Usage: four-squares [--debug] 7 values\n" + unless GetOptions("debug"=>\$debug) && @ARGV==7; +my @v = @ARGV; + + +# +# allpermutations( \&eachpermcallback, $perm, @v ); +# compute all permutations of @v with $perm on the front, +# calling eachpermcallback for each permutation found. +# Adapted from Rosetta Stone.. +# +fun allpermutations( $cb, $perm, @v ) +{ + if( @v == 0 ) + { + $cb->( @$perm ); + return; + } + foreach my $i (0..$#v) + { + # remove $v[$i] from the values, append it to the @$perm + allpermutations( $cb, + [ @$perm, $v[$i] ], + @v[0..$i-1], @v[$i+1..$#v] ); + } +} + +# +# printifsolution( @perm ); +# check whether @perm (7 values) is a four-square solution; +# print it if so. +# +fun printifsolution( @perm ) +{ + die unless @perm==7; + my( $a, $b, $c, $d, $e, $f, $g ) = @perm; + my $sum = $a+$b; + return unless $sum == $b+$c+$d && $sum == $d+$e+$f && $sum == $f+$g; + say "solution: a=$a, b=$b, c=$c, d=$d, e=$e, f=$f, g=$g"; +} + +allpermutations( \&printifsolution, [], ,@v ); |
