• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Project Euler 159

mscha Mar 11th, 2017 (edited) 160 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/env perl6
2.
3. # Solution for Project Euler problem 159
4. # https://projecteuler.net/problem=159
5.
6. use v6.c;
7.
8. sub MAIN(Int \$limit = 10, Bool :v(:\$verbose) = False)
9. {
10.     my \$max = \$limit - 1;
11.
13.     # The digital root of n > 0 is just (n mod 9) || 9, so generate a list
14.     # of repeated 1..9s of exactly the right length
15.     my @mdsr = 0, |(|(1..9) xx (\$max div 9)), |(1..(\$max % 9));
16.     say @mdsr if \$verbose;
17.
18.     # mdsr(n) = max(dr(n), mdsr(i) + mdsr(j)) for all i > j > 1 with n = i×j
19.     # so loop over all i and j and update mdsr(i×j) where necessary
20.     #for 2 .. (\$max div 2) -> \$i {
21.     loop (my \$i = 2, my \$max-i = \$max div 2; \$i <= \$max-i; \$i++) {
22.         #for 2 .. (\$max div \$i) -> \$j {
23.         loop (my \$j = 2, my \$max-j = \$max div \$i; \$j <= \$max-j; \$j++) {
24.             @mdsr[\$i*\$j] max= @mdsr[\$i] + @mdsr[\$j];
25.         }
26.         say "\$i: ", @mdsr if \$verbose;
27.     }
28.
29.     # We need the sum from 2..limit, so subtract @mdsr[0]=0 and @mdsr[1]=1
30.     say “∑mdrs(n) for 1 < n < \$limit is:, @mdsr.sum - 1;
31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top