Project::Euler::Lib::Utils - Collection of helper utilities for project euler problems
use Project::Euler::Lib::Utils qw/ :all /;
This returns a clojure that returns the next successive fib number with each call
my $fib = fib_generator; # Manually create the first 4 fibs my @fibs; push @fibs, $fib->() for 1..4;
The returns either the first 'n' fibs or the nth fib if called in scalar context. If only the nth fib is used, then no memory is used to store the previous fibs and it should run very fast. For now this does some very primitive caching but will have to be improved in the future.
This also does not currently use Math::BigInt so if a large # is requested it may not be 100% accurate. This will be fixed once I decide upon a caching solution.
# Get the first 4 fib numbers my @fibs = n_fibs( 4 ); # Just get the last one my $fourth_fib = n_fibs( 4 ); $fibs[-1] == $fourth_fib;
Check to see if a number is evenly divisible by one or all of a range of numbers.
my $is_divisible = multiple_check(15, [2, 3, 5], 0); my $is_divisible2 = multiple_check(15, [2, 3, 5]); my $is_not_divisible = multiple_check(10, [3, 6, 7]); my $is_all_divisible = multiple_check(30, [2, 3, 5], 1); my $is_not_all_divisible = multiple_check(15, [2, 3, 5], 1); my @div_by = multiple_check(15, [2, 3, 5]); @div_by ~~ (3, 5) == 1; my $num = 3; my $is_prime = !multiple_check($num, [2..sqrt($num)]);
Adam Lesperance <email@example.com>
This software is copyright (c) 2010 by Adam Lesperance.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.