[viv] add a --check/-c mode so viv can subsume tryfile, too
[pugs.git] / examples / euler / prob03.pl
blob35324ca0e16498cd956c1f266cd124ece8682393
1 #!/usr/bin/env pugs
3 # vim: filetype=perl6
5 =begin Problem
6 The prime factors of 13195 are 5, 7, 13 and 29.
8 What is the largest prime factor of the number 317584931803?
9 =end Problem
11 use v6;
12 use Benchmark <timeit>;
14 sub is_prime($n) {
15 my @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
16 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109,
17 113);
19 return True if $n == any(@primes);
20 return False if $n % any(@primes) == 0;
21 return False if $n % any(2..sqrt($n)) == 0;
22 return True;
25 sub divide($n) {
26 my $i = 2;
27 while $i <= $n {
28 return ($i, $n / $i) if $n % $i == 0;
29 $i += 1;
33 sub prime_factors($n) {
34 if is_prime($n) {
35 ($n);
37 else {
38 my ($x, $y) = divide($n);
39 (prime_factors($x), prime_factors($y));
43 my @t = timeit(1, -> { say max(prime_factors(317584931803)) });
44 say "execution time: @t[0]";