aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-16 12:12:08 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-16 12:12:08 +0100
commit9f1d181d4dfd3b4190b71151ce1b94881d0eaf22 (patch)
tree994fef221dc171409a8dfb6f9955e0c1a166a03e
parentfab1f2a1445dc7f81a584b23c49e4c8b232cda57 (diff)
parent695df64155c21ca9e939d3f86766804f6c489e45 (diff)
downloadperlweeklychallenge-club-9f1d181d4dfd3b4190b71151ce1b94881d0eaf22.tar.gz
perlweeklychallenge-club-9f1d181d4dfd3b4190b71151ce1b94881d0eaf22.tar.bz2
perlweeklychallenge-club-9f1d181d4dfd3b4190b71151ce1b94881d0eaf22.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-173/bruce-gray/raku/ch-1.raku37
-rw-r--r--challenge-173/bruce-gray/raku/ch-2.raku19
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-173/bruce-gray/raku/ch-1.raku b/challenge-173/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..91f684c1d2
--- /dev/null
+++ b/challenge-173/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,37 @@
+sub is-esthetic ( UInt $n --> Bool ) {
+ so $n.comb.rotor(2 => -1).map({ abs [-] .list }).all == 1;
+}
+
+# Bonus: Efficent method to generate all esthetic numbers,
+# without grepping through ℕ .
+constant @all-esthetics = gather loop {
+ state @last_generation = 1..9;
+ .take for @last_generation;
+ my @next_generation = gather for @last_generation -> $i {
+ my $d = $i % 10;
+ my $n = $i * 10 + $d;
+
+ take $n - 1 if $d !== 0;
+ take $n + 1 if $d !== 9;
+ }
+ @last_generation = @next_generation;
+};
+
+
+multi sub MAIN ( UInt $n ) {
+ say is-esthetic $n;
+}
+multi sub MAIN ( 'test' ) {
+ constant @A033075 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78, 87, 89, 98, 101, 121, 123, 210, 212, 232, 234, 321, 323, 343, 345, 432, 434, 454, 456, 543, 545, 565, 567, 654, 656, 676, 678, 765, 767, 787, 789, 876;
+ constant @A090994 = 9, 17, 32, 61, 116, 222, 424, 813, 1556, 2986, 5721, 10982;
+
+ use Test;
+ plan 2;
+
+ is (1..Inf).grep(&is-esthetic).head(54), @A033075,
+ 'https://oeis.org/A033075 Esthetic numbers';
+
+ my $limit = @all-esthetics.first: :k, *.chars > @A090994.elems;
+ is @all-esthetics.head($limit)».chars.Bag.sort».value, @A090994,
+ 'https://oeis.org/A090994 Number of n-digit terms in A033075';
+}
diff --git a/challenge-173/bruce-gray/raku/ch-2.raku b/challenge-173/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..0a78060816
--- /dev/null
+++ b/challenge-173/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,19 @@
+# All of these produce the same sequence:
+constant @Sylvester's_sequence =
+ 2, 3, { 1 + [*] @_ } … *;
+ # 2, { $_² - $_ + 1 } … *;
+ # 2, -> \a { a² - a + 1 } … *;
+ # 2, -> \a { a * (a - 1) + 1 } … *;
+
+multi sub MAIN ( $count = 10 ) {
+ say @Sylvester's_sequence.head($count);
+}
+multi sub MAIN ( 'test' ) {
+ constant @A000058 = 2, 3, 7, 43, 1807, 3263443, 10650056950807, 113423713055421844361000443, 12864938683278671740537145998360961546653259485195807, 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443, 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920807, 750346333909286311464218348364293017384724140073732363176684391768374238237200233203724274839819736227493060107386942069521875902258281351952761393460726027774387698896086030486687796275661950199835484418384103096899499524666007073298797852932127876923983340497448231960048833094195425231846478785035602339261149953564729371337917773386670133413581537490788020231265093210310224397095644371148893261284201611453610443, 563019620811106188735345793029131127645126456201508328395934709948713369875017428398616526515350801978901280848429236023780360625686785326318172135941491985234945009397938528773976977887356630327917721419666751591559883455828027643329740930320548089836575156581070880066847294722353255119648341271402723167206816552066010592134725124376810874159292118224274440907862996344067325025151751506647339684959479438354192989470470488621172745980086435801519575581664038703641801974453354864238123148678312993828328158261237284571445163949954735321181426755485563860804935755099928085508785637606452207007984638610604113718738727804169240213041488645142155664706600019329809186121988322464207409749450811638629159492106903853533242008723387118397794980575878357156285111258733862431522178328441469980110808406224908967784943255608168545045807;
+
+ use Test;
+ plan 1;
+
+ is @Sylvester's_sequence.head(13), @A000058,
+ 'Exact match for OEIS A000058 extended `b-file`: https://oeis.org/A000058/b000058.txt';
+}