aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-01-14 17:03:31 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-01-14 17:03:31 +0000
commitb38db58f0c4edc7ac9407f5bc03402dbdb2e530e (patch)
treee92b07cb066fa48e63d2a399707ff77f1ea101cc
parent53bacd059cb8446ddc447d9d1172468b3fa50f68 (diff)
parent921eb4d2a09b0a9837cb53c209fb3c17e961e10c (diff)
downloadperlweeklychallenge-club-b38db58f0c4edc7ac9407f5bc03402dbdb2e530e.tar.gz
perlweeklychallenge-club-b38db58f0c4edc7ac9407f5bc03402dbdb2e530e.tar.bz2
perlweeklychallenge-club-b38db58f0c4edc7ac9407f5bc03402dbdb2e530e.zip
Merge branch 'master' of git://github.com/holli-holzer/perlweeklychallenge-club into holli-holzer-043
-rw-r--r--challenge-043/markus-holzer/perl6/lib/Math/SelfDescriptiveNumbers.pm633
-rw-r--r--challenge-043/markus-holzer/raku/ch-2.p664
2 files changed, 51 insertions, 46 deletions
diff --git a/challenge-043/markus-holzer/perl6/lib/Math/SelfDescriptiveNumbers.pm6 b/challenge-043/markus-holzer/perl6/lib/Math/SelfDescriptiveNumbers.pm6
new file mode 100644
index 0000000000..974f0c532a
--- /dev/null
+++ b/challenge-043/markus-holzer/perl6/lib/Math/SelfDescriptiveNumbers.pm6
@@ -0,0 +1,33 @@
+unit module Math::SelfDescriptiveNumbers;
+
+multi sub is-self-descriptive( Int $number, Int $base ) is export {
+ is-self-descriptive( :$number, :$base );
+}
+
+multi sub is-self-descriptive( Int :$number, Int :$base ) is export {
+ $number.base( $base ) ∈ self-descriptive-numbers-of( $base );
+}
+
+multi sub is-self-descriptive( Str $number, Int $base ) is export {
+ is-self-descriptive( :$number, :$base );
+}
+
+multi sub is-self-descriptive( Str :$number, Int :$base ) is export {
+ $number ∈ self-descriptive-numbers-of( $base );
+}
+
+sub self-descriptive-numbers is export {
+ ( 1 .. 36 ).map( -> $base { ( $base, self-descriptive-numbers-of( $base ) ) });
+}
+
+sub self-descriptive-numbers-dec is export {
+ ( 1 .. 36 ).map( -> $base { ( $base, self-descriptive-numbers-of( $base ).map({ parse-base($_, $base ) }) ) });
+}
+
+multi sub self-descriptive-numbers-of( Int $base where $_ ~~ 1|2|3|6 ) is export { () }
+multi sub self-descriptive-numbers-of( Int $base where $_ == 4 ) is export { ('1210', '2020') }
+multi sub self-descriptive-numbers-of( Int $base where $_ == 5 ) is export { ('21200') }
+multi sub self-descriptive-numbers-of( Int $base ) is export {
+ ( ($base - 4).base( $base ) ~ "21" ~ ( '0' x ($base - 7) ) ~ '1000' )
+}
+
diff --git a/challenge-043/markus-holzer/raku/ch-2.p6 b/challenge-043/markus-holzer/raku/ch-2.p6
index 11b4b2a19c..1a6ea4389a 100644
--- a/challenge-043/markus-holzer/raku/ch-2.p6
+++ b/challenge-043/markus-holzer/raku/ch-2.p6
@@ -1,61 +1,33 @@
use Test;
+use Math::SelfDescriptiveNumbers;
-# all self-descriptive numbers
-# - must be at least base digits long
-# - have digit sums equal to their base,
-# - are multiples of that base
-# - each digit d at position n counts how many instances of digit n are in m
-
-multi sub MAIN( Int $base where $_ < 37 )
+multi sub MAIN()
{
- .base( $base ).say for
- self-descriptive-candidates( $base )
- .grep({ is-self-descriptive( $_, $base ) });
+ for self-descriptive-numbers() -> ($base, $numbers)
+ {
+ say output( $base, $numbers );
+ }
}
+multi sub MAIN( Int $base where 1 < $base < 37 )
+{
+ say output( $base, self-descriptive-numbers-of( $base ) );
+}
multi sub MAIN( "test" )
{
- ok base-start( 2 ) == 2;
- ok base-start( 10 ) == 1000000000;
- ok base-start( 16 ) == 0x1000000000000000;
-
# test values from Wikipedia
- ok is-self-descriptive( parse-base('21200',5), 5 );
+ ok is-self-descriptive( :number(parse-base('21200',5)), :base(5) );
+ ok is-self-descriptive( :number('21200'), :base(5) );
+ ok is-self-descriptive( 'C210000000001000', 16 );
ok is-self-descriptive( 0xC210000000001000, 16 );
- ok is-self-descriptive( 6210001000, 10 );
ok !is-self-descriptive( 3210001000, 10 );
- ok self-descriptive-candidates(4).first({ is-self-descriptive( $_, 4) }).base(4) eq "1210";
- ok self-descriptive-candidates(5).first({ is-self-descriptive($_, 5) }).base(5) eq "21200";
- ok self-descriptive-candidates(7).first({ is-self-descriptive($_, 7) }).base(7) eq "3211000";
-}
-
-
-sub is-self-descriptive( $number, $base )
-{
- state @digits = (0 .. 9).Array.append( ('A' .. 'Z').Array );
-
- my $base-str = $number.base( $base );
-
- !so $base-str.comb.pairs.first( -> $p
- {
- my $digit = @digits[ $p.key ];
- my $count-is = ( $base-str ~~ m:g/ ($digit) / ).elems;
- $count-is != parse-base( $p.value.Str, $base );
- });
-}
-
-
-sub self-descriptive-candidates( $base )
-{
- my $base-start = base-start($base);
- return $base-start, $base-start + $base, { $_ + $base } ...^ $base-start * $base;
}
-sub base-start( $base )
+sub output( $base, $numbers )
{
- my $zeroes = $base - 1;
- my $n = "1" ~ ( "0" x $zeroes );
- parse-base( $n, $base );
-}
+ my $num-base = $numbers.join(',');
+ my $num-dec = $numbers.map({ parse-base( $_, $base ) }).join(',');
+ "Base $base, " ~ ( $numbers.elems ?? "$num-base; $num-dec (decimal)" !! '---' );
+} \ No newline at end of file