|
22 | 22 | # count duplicates
|
23 | 23 | for %powers.values -> \p {
|
24 | 24 | for 2..B -> \e {
|
25 |
| - # raise to power e |
| 25 | + # raise to power \e |
26 | 26 | # classify by root and relative exponent
|
27 | 27 | ++%count{p.key => p.value * e}
|
28 | 28 | }
|
|
32 | 32 | return (A - 1) * (B - 1) + %count - [+] %count.values;
|
33 | 33 | }
|
34 | 34 |
|
| 35 | +sub cross(@a, @b) { @a X @b } |
| 36 | +sub dups(@a) { @a - @a.uniq } |
| 37 | +sub count-feedly(Int \A, Int \B --> Int) { |
| 38 | + 2..Int(sqrt A) |
| 39 | + ==> map -> \a { (a, a**2, a**3 ...^ * > A) Z=> (a X 1..*).tree } |
| 40 | + ==> reverse() |
| 41 | + ==> hash() |
| 42 | + ==> values() |
| 43 | + ==> cross(2..B) |
| 44 | + ==> map -> \n, [\r, \e] { (r) => e * n } |
| 45 | + ==> dups() |
| 46 | + ==> ((A - 1) * (B - 1) - *)() |
| 47 | +} |
| 48 | + |
35 | 49 | sub bench(|) {
|
36 | 50 | my \start = now;
|
37 | 51 | my \result = callsame;
|
38 | 52 | my \end = now;
|
39 | 53 | return result, round (end - start) * 1000;
|
40 | 54 | }
|
41 | 55 |
|
42 |
| -multi MAIN(Int $N, Bool :$verify) { |
43 |
| - nextwith($N, $N, :$verify) |
| 56 | +multi MAIN(Int $N, Bool :$verify, Bool :$feeds) { |
| 57 | + nextwith($N, $N, :$verify, :$feeds) |
44 | 58 | }
|
45 | 59 |
|
46 |
| -multi MAIN(Int $A = 100, Int $B = 100, Bool :$verify) { |
47 |
| - &count-smartly.wrap(&bench); |
| 60 | +multi MAIN(Int $A = 100, Int $B = 100, Bool :$verify, Bool :$feeds) { |
48 | 61 | &count-naively.wrap(&bench);
|
| 62 | + &count-smartly.wrap(&bench); |
| 63 | + &count-feedly.wrap(&bench); |
| 64 | + |
| 65 | + printf "got %u [%ums]\n", |
| 66 | + ($feeds ?? &count-feedly !! &count-smartly)($A, $B); |
49 | 67 |
|
50 |
| - printf "got %u [%ums]\n", count-smartly $A, $B; |
51 |
| - printf "expected %u [%ums]\n", count-naively $A, $B if $verify; |
| 68 | + printf "expected %u [%ums]\n", |
| 69 | + count-naively $A, $B if $verify; |
52 | 70 | }
|
0 commit comments