forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,206 additions
and
893 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://raku-musings.com/linked-sum.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (Int $a is copy, $b is copy, :$verbose); | ||
|
||
($a, $b) = ($b, $a) if $b > $a; | ||
|
||
my $a2 = $a.base(2); | ||
|
||
my $length = $a2.chars; | ||
|
||
my $b2 = $b.fmt('%0' ~ $length ~ 'b'); | ||
|
||
my $c2 = ($a +^ $b).fmt('%0' ~ $length ~ 'b'); | ||
|
||
if $verbose | ||
{ | ||
say ": $a2 ($a)"; | ||
say ": $b2 ($b)"; | ||
say ": $c2 -> ", $c2.comb.sum; | ||
} | ||
|
||
say $c2.comb.sum; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (Int $a, $b, :$verbose); | ||
|
||
my $c = ($a +^ $b); | ||
|
||
my $sum = $c.comb.sum; | ||
|
||
if $verbose | ||
{ | ||
my $length = (max($a, $b)).base(2).chars; | ||
|
||
say ": { $a.fmt('%0' ~ $length ~ 'b') } ($a)"; | ||
say ": { $b.fmt('%0' ~ $length ~ 'b') } ($b)"; | ||
say ": { $c.fmt('%0' ~ $length ~ 'b') } -> $sum"; | ||
} | ||
|
||
say $sum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int, | ||
:$verbose); | ||
|
||
my $grand-total; | ||
|
||
for @numbers.combinations(2) -> $list | ||
{ | ||
my $sum = bit-diff(|$list); | ||
say ": $list -> $sum" if $verbose; | ||
$grand-total += $sum; | ||
} | ||
|
||
say $grand-total; | ||
|
||
sub bit-diff (Int $a, Int $b) | ||
{ | ||
return ($a +^ $b).base(2).comb.sum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (Int $limit where $limit > 1 = 100, :$verbose); | ||
|
||
my $prev-sum = 0; | ||
my $prev-inc = 0; | ||
my $prev-inx = 0; | ||
|
||
my @result; | ||
|
||
for 2 .. $limit -> $number | ||
{ | ||
my @list = 1 .. $number; | ||
|
||
my $sum = bit-sum(@list); | ||
my $inc = $sum - $prev-sum; | ||
my $inx = $inc - $prev-inc; | ||
|
||
say ": bit-sum 1..{ $number.fmt("%3d") } -> { $sum.fmt("%3d") } -> { $inc.fmt("%3d") } -> { $inx.fmt("%3d") }" if $verbose; | ||
|
||
@result.push: $inx; | ||
|
||
$prev-sum = $sum; | ||
$prev-inc = $inc; | ||
$prev-inx = $inx; | ||
} | ||
|
||
put @result; | ||
|
||
sub bit-sum (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int) | ||
{ | ||
my $grand-total; | ||
|
||
for @numbers.combinations(2) -> $list | ||
{ | ||
my $sum = bit-diff(|$list); | ||
$grand-total += $sum; | ||
} | ||
|
||
return $grand-total; | ||
|
||
sub bit-diff (Int $a, Int $b) | ||
{ | ||
return ($a +^ $b).base(2).comb.sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (Int $limit where $limit > 1 = 100, :$verbose, :$type = "int"); | ||
|
||
my $prev-sum = 0; | ||
my $prev-inc = 0; | ||
my $prev-inx = 0; | ||
|
||
my @result; | ||
|
||
for 2 .. $limit -> $number | ||
{ | ||
my @list; | ||
|
||
given $type | ||
{ | ||
when "int" { @list = (1 .. Inf)[^$number] } | ||
when "even" { @list = (2, 4 ... Inf)[^$number] } | ||
when "odd" { @list = (1, 3 ... Inf)[^$number] } | ||
when "prime" { @list = ((1 .. Inf).grep: *.is-prime)[^$number] } | ||
when "fib" { @list = (1, 1, * + * ... Inf)[^$number] } | ||
default { die "Unknown type $_" } | ||
} | ||
|
||
my $sum = bit-sum(@list); | ||
my $inc = $sum - $prev-sum; | ||
my $inx = $inc - $prev-inc; | ||
|
||
say ": bit-sum @list[] -> { $sum.fmt("%3d") } -> { $inc.fmt("%3d") } -> { $inx.fmt("%3d") }" if $verbose; | ||
|
||
@result.push: $inx; | ||
|
||
$prev-sum = $sum; | ||
$prev-inc = $inc; | ||
$prev-inx = $inx; | ||
} | ||
|
||
put @result; | ||
|
||
sub bit-sum (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int) | ||
{ | ||
my $grand-total; | ||
|
||
for @numbers.combinations(2) -> $list | ||
{ | ||
my $sum = bit-diff(|$list); | ||
$grand-total += $sum; | ||
} | ||
|
||
return $grand-total; | ||
|
||
sub bit-diff (Int $a, Int $b) | ||
{ | ||
return ($a +^ $b).base(2).comb.sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#! /usr/bin/env raku | ||
|
||
multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) | ||
{ | ||
MAIN($k, $list.words, :$verbose); | ||
} | ||
|
||
multi MAIN (Int $k = 3, *@list, :$verbose) | ||
{ | ||
my @lower; | ||
my @higher; | ||
|
||
for @list -> $elem | ||
{ | ||
$elem >= $k | ||
?? @higher.push: $elem | ||
!! @lower.push: $elem; | ||
} | ||
|
||
my @result = (@lower, @higher).flat; | ||
|
||
if $verbose | ||
{ | ||
say ": == : $k"; | ||
say ": < : @lower[]"; | ||
say ": >= : @higher[]"; | ||
} | ||
|
||
say @result.join(" → "); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#! /usr/bin/env raku | ||
|
||
unit sub MAIN (*@numbers where @numbers.elems > 1 && all(@numbers) ~~ Int, | ||
:$verbose); | ||
|
||
my $grand-total; | ||
|
||
for @numbers.combinations(2) -> $list | ||
{ | ||
my $sum = bit-diff(|$list); | ||
say ": $list -> $sum" if $verbose; | ||
$grand-total += $sum; | ||
} | ||
|
||
say $grand-total; | ||
|
||
sub bit-diff (Int $a, Int $b) | ||
{ | ||
return ($a +^ $b).base(2).comb.sum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#! /usr/bin/env raku | ||
|
||
multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) | ||
{ | ||
MAIN($k, $list.words, :$verbose); | ||
} | ||
|
||
multi MAIN (Int $k = 3, *@list, :$verbose) | ||
{ | ||
my @lower; | ||
my @higher; | ||
|
||
for @list -> $elem | ||
{ | ||
$elem >= $k | ||
?? @higher.push: $elem | ||
!! @lower.push: $elem; | ||
} | ||
|
||
my @result = (@lower, @higher).flat; | ||
|
||
if $verbose | ||
{ | ||
say ": == : $k"; | ||
say ": < : @lower[]"; | ||
say ": >= : @higher[]"; | ||
} | ||
|
||
say @result.join(" → "); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#! /usr/bin/env raku | ||
|
||
multi MAIN (Int $k = 3, Str $list = "1 4 3 2 5 2", :$verbose) | ||
{ | ||
MAIN($k, $list.words, :$verbose); | ||
} | ||
|
||
multi MAIN (Int $k = 3, *@list, :$verbose) | ||
{ | ||
my @lower; | ||
my @higher; | ||
|
||
for @list -> $elem | ||
{ | ||
$elem >= $k | ||
?? @higher.push: $elem | ||
!! @lower.push: $elem; | ||
} | ||
|
||
my @result = (@lower, @higher).flat; | ||
|
||
if $verbose | ||
{ | ||
say ": == : $k"; | ||
say ": < : @lower[]"; | ||
say ": >= : @higher[]"; | ||
} | ||
|
||
say @result.join(" → ") if $verbose; | ||
|
||
class ListElem | ||
{ | ||
has $.value; | ||
has $.next is rw; | ||
|
||
method display | ||
{ | ||
print $.value; | ||
if $.next { print " → "; $.next.display; } else { say ""; } | ||
} | ||
} | ||
|
||
my $head; | ||
|
||
$head = ListElem.new(value => $_, next => $head) for @result.reverse; | ||
|
||
$head.display; | ||
} |
Oops, something went wrong.