Skip to content

Instantly share code, notes, and snippets.

@RayMPerry
Last active September 3, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RayMPerry/e38337cc48e389fe15796d5642871a19 to your computer and use it in GitHub Desktop.
Save RayMPerry/e38337cc48e389fe15796d5642871a19 to your computer and use it in GitHub Desktop.
Sums
my @numbers = (1..25);
my @otherNumbers = (0, 1);
my $forSum = 0;
my $whileSum = 0;
my $recurseSum = 0;
sub addTogether(@numbers) {
$recurseSum += @numbers.head();
return @numbers.tail(*-1) ~~ () ?? $recurseSum !! addTogether(@numbers.tail(*-1));
}
sub MAIN() {
# Get the sum three ways.
for @numbers { $forSum += $_ }
while $whileSum < $forSum { $whileSum += @numbers.reduce(&[+]) }
($forSum, $whileSum, addTogether(@numbers)).say;
# Get the zip of these two lists.
say (<a b c> Z (1, 2, 3)).flat;
# Fibonacci.
for 0..99 { @otherNumbers.push(@otherNumbers[$_] + @otherNumbers[$_ + 1]); }
say @otherNumbers;
}
# Local Variables:
# compile-command: "perl6 ./sum-of-numbers.p6"
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment