aboutsummaryrefslogtreecommitdiff
path: root/challenge-157
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-03-27 21:45:18 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-03-27 21:45:18 +0100
commit27c9c994dc0179606edc713b01cdff1d8fedd6cf (patch)
treeb2570b34199dc44d0ccda8354d94e4c2355c398b /challenge-157
parenta1e8d3643037dafb58e8ec8f140f177ce1e9a7f4 (diff)
downloadperlweeklychallenge-club-27c9c994dc0179606edc713b01cdff1d8fedd6cf.tar.gz
perlweeklychallenge-club-27c9c994dc0179606edc713b01cdff1d8fedd6cf.tar.bz2
perlweeklychallenge-club-27c9c994dc0179606edc713b01cdff1d8fedd6cf.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-157')
-rw-r--r--challenge-157/colin-crain/blog.txt1
-rw-r--r--challenge-157/colin-crain/blog1.txt1
-rwxr-xr-xchallenge-157/colin-crain/perl/ch-1.pl98
-rwxr-xr-xchallenge-157/colin-crain/perl/ch-2.pl128
-rwxr-xr-xchallenge-157/colin-crain/raku/ch-1.raku20
-rwxr-xr-xchallenge-157/colin-crain/raku/ch-2.raku40
6 files changed, 288 insertions, 0 deletions
diff --git a/challenge-157/colin-crain/blog.txt b/challenge-157/colin-crain/blog.txt
new file mode 100644
index 0000000000..d5ee9c0941
--- /dev/null
+++ b/challenge-157/colin-crain/blog.txt
@@ -0,0 +1 @@
+https://colincrain.com/2022/03/26/pythagoras-was-a-meanie/
diff --git a/challenge-157/colin-crain/blog1.txt b/challenge-157/colin-crain/blog1.txt
new file mode 100644
index 0000000000..b4278f7683
--- /dev/null
+++ b/challenge-157/colin-crain/blog1.txt
@@ -0,0 +1 @@
+https://colincrain.com/2022/03/27/waxing-numeric/
diff --git a/challenge-157/colin-crain/perl/ch-1.pl b/challenge-157/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..5922e4173c
--- /dev/null
+++ b/challenge-157/colin-crain/perl/ch-1.pl
@@ -0,0 +1,98 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# pythagoras-was-a-meanie.pl
+#
+# Pythagorean Means
+# Submitted by: Mohammad S Anwar
+# You are given a set of integers.
+#
+# Write a script to compute all three Pythagorean Means
+# i.e Arithmetic Mean,
+# Geometric Mean and
+# Harmonic Mean
+# of the given set of integers.
+# Please refer to wikipedia page for more informations.
+#
+# Example 1:
+# Input: @n = (1,3,5,6,9)
+# Output: AM = 4.8, GM = 3.8, HM = 2.8
+# CORRECTION [2022-03-21 16:35] GM = 3.9 (before)
+#
+# Example 2:
+# Input: @n = (2,4,6,8,10)
+# Output: AM = 6.0, GM = 5.2, HM = 4.4
+# Example 3:
+# Input: @n = (1,2,3,4,5)
+# Output: AM = 3.0, GM = 2.6, HM = 2.2
+
+# Pythagorean means in the manner of Pythagoras.
+# Which was apparently that he was mean — a real bastard.
+# I mean, that's a sort of middle-of-the-road opinion on the subject.
+# Whatever that means.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+use List::Util qw( sum product );
+
+
+
+sub am ( @list ) {
+ return sum( @list ) / @list
+}
+
+sub gm ( @list ) {
+ return product( @list ) ** (1 / @list)
+}
+
+sub hm ( @list ) {
+ return @list / ( sum map { 1/$_ } @list )
+}
+
+
+
+# my @list = (1,3,5,6,9);
+#
+# say sprintf "%.1f", gm( @list);
+#
+
+
+use Test::More;
+
+my @list;
+
+@list = (1,3,5,6,9);
+
+is ((sprintf "%.1f", am( @list)), 4.8, 'ex-1-am');
+is ((sprintf "%.1f", gm( @list)), 3.8, 'ex-1-gm');
+is ((sprintf "%.1f", hm( @list)), 2.8, 'ex-1-hm');
+
+
+@list = (2,4,6,8,10);
+
+is ((sprintf "%.1f", am( @list)), '6.0', 'ex-2-am');
+is ((sprintf "%.1f", gm( @list)), 5.2, 'ex-2-gm');
+is ((sprintf "%.1f", hm( @list)), 4.4, 'ex-2-hm');
+
+
+@list = (1,2,3,4,5);
+
+is ((sprintf "%.1f", am( @list)), '3.0', 'ex-3-am');
+is ((sprintf "%.1f", gm( @list)), 2.6, 'ex-3-gm');
+is ((sprintf "%.1f", hm( @list)), 2.2, 'ex-3-hm');
+
+@list = (1,9);
+
+is ((sprintf "%.1f", am( @list)), '5.0', 'blog-am');
+is ((sprintf "%.1f", gm( @list)), 3.0, 'blog-gm');
+is ((sprintf "%.1f", hm( @list)), 1.8, 'blog-hm');
+
+done_testing();
diff --git a/challenge-157/colin-crain/perl/ch-2.pl b/challenge-157/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..aa73efcc61
--- /dev/null
+++ b/challenge-157/colin-crain/perl/ch-2.pl
@@ -0,0 +1,128 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# waxing-numeric.pl
+#
+# Brazilian Number
+# Submitted by: Mohammad S Anwar
+# You are given a number $n > 3.
+#
+# Write a script to find out if the given number is a Brazilian
+# Number.
+#
+# A positive integer number N has at least one natural number B
+# where 1 < B < N-1 where the representation of N in base B has
+# same digits.
+#
+#
+# Example 1:
+# Input: $n = 7
+# Output: 1
+#
+# Since 7 in base 2 is 111.
+#
+# Example 2:
+# Input: $n = 6
+# Output: 0
+#
+# Since 6 in base 2 is 110,
+# 6 in base 3 is 20 and
+# 6 in base 4 is 12.
+#
+# Example 3:
+# Input: $n = 8
+# Output: 1
+#
+# Since 8 in base 3 is 22.
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+use constant { ANALYSE => 0 };
+use constant { COUNT => 2000 };
+
+
+if (ANALYSE) {
+ analyse( COUNT );
+}
+else {
+ my $input = shift @ARGV // 1993;
+ say "input: $input";
+ say "output: ", brazilian( $input ) ? 1 : 0;
+
+ say digit_test_36( 100, 99);
+}
+
+
+## ## ## the task
+
+sub brazilian ( $num ) {
+## up/down check if number is a Brazilian number
+ for ( 2..$num-2 ) {
+ return $_ if digit_test( $num, $_ );
+ }
+ return 0;
+}
+
+sub digit_test ( $num, $base ) {
+## up/down check for all digits are the same in a given base
+## converts from base-10 to any given base
+## does not internally store converted number other than
+## single digit as base-10 sequence index: conditional
+## 1 short-circuits returning 0 if non-repeated digit found,
+## otherwise returns 1
+ my $rem;
+ my $digit;
+ while ( $num > 0 ) {
+ ($num, $rem) = (int( $num/$base ), $num % $base);
+ $digit //= $rem;
+ return 0 if $digit != $rem;
+ }
+ return 1;
+}
+
+## ## ## /task
+
+
+## analysis section
+sub analyse ( $count ) {
+
+ my $match;
+ my @coef;
+ for (2..$count) {
+ my $b = brazilian( $_ );
+ next if $b == 0;
+ push @coef, $b;
+
+ my @pdiv = nd_brute( $_ );
+
+ for my $div ( 2..42 ) {
+ next unless $b == $_/$div - 1;
+ $match = "match 1/${div}-1";
+ last;
+ }
+
+ $match ||= (sprintf "\t%.2f ", $_/$b);
+ say sprintf "%3d %-3d %-20s %s", $_, $b, $match, "@pdiv";
+ $match = undef;
+ }
+
+ my @a = grep { brazilian($_) } (2..200);
+ say "@a";
+ say "@coef";
+
+ sub nd_brute ( $num, @div ) {
+ $num % $_ or push @div, $_ for 2..$num/2 ;
+ return @div;
+ }
+}
+## /analysis
+
diff --git a/challenge-157/colin-crain/raku/ch-1.raku b/challenge-157/colin-crain/raku/ch-1.raku
new file mode 100755
index 0000000000..21f5ad9df5
--- /dev/null
+++ b/challenge-157/colin-crain/raku/ch-1.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ( *@list ) ;
+
+.say for
+ @list.sum / @list.elems, ## arithmetic
+ @list.reduce({$^a * $^b}) ** (1/ @list.elems), ## geometric
+ @list.elems / ( sum map { 1/$_ }, @list ); ## harmonic
+
+
diff --git a/challenge-157/colin-crain/raku/ch-2.raku b/challenge-157/colin-crain/raku/ch-2.raku
new file mode 100755
index 0000000000..7df3d66d83
--- /dev/null
+++ b/challenge-157/colin-crain/raku/ch-2.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN ( $num = 1994 ) ;
+
+brazilian( $num ).say;
+
+sub brazilian ( $num ) {
+## up/down check if number is a Brazilian number
+ return 1 if digit_test( $num, $_ ) for 2..$num-2 ;
+ return 0;
+}
+
+sub digit_test ( $num is copy, $base ) {
+## up/down check for all digits are the same in a given base
+## converts from base-10 to any given base
+## does not internally store converted number other than
+## single digit as base-10 sequence index: 1
+## short-circuits 0 if non-repeated digit found, otherwise returns 1
+ my $rem;
+ my $digit;
+ while ( $num > 0 ) {
+ ($num, $rem) = $num div $base, $num % $base;
+ $digit //= $rem;
+ return 0 if $digit != $rem;
+ }
+ return 1;
+}
+
+
+