On Thu, Dec 12, 2013 at 02:05:35PM +1100, Jacinta Richardson wrote:
:sub MAIN($terms = 35, $maximum = 4_000_000) {
: my @sequence = gather for fibonacci($maximum) - $number {
: state $count = 0;
: take $number if $number $maximum $count++ $terms;
: };
:
: say @sequence;
:}
:
:sub fibonacci($maximum) {
:my @numbers := 0, 1, *+* ... * $maximum;
:return @numbers;
:}
Your main problem here is that your series is terminated backwards.
The ... runs until the right side matches, and and 0 $maximum,
so it terminates on 0. So you want * $maximum. You can also use a ^
to exclude the last value whwere it becomes true, so you can just say
0, 1, *+* ...^ * $maximum
and then you don't have to put in the extra check in your loop.
But you don't really need the loop either. A more succinct way to
pick the first N terms is to use slice subscripting to pick out the 0th
to the $term'th - 1 element (again, using ^ to exclude the final value):
say (0, 1, *+* ...^ * $maximum)[0 ..^ $terms];
We also often like to abbreviate 0 ..^ $terms down to ^$terms like this
say (0, 1, *+* ...^ * $maximum)[^$terms];
But usually we just limit it by number of terms, and don't care about the
maximum:
say (0, 1, *+* ... *)[^$terms];
Hope this helps...
Larry