-
-
Save RayMPerry/e38337cc48e389fe15796d5642871a19 to your computer and use it in GitHub Desktop.
Sums
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
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