Skip to content

Commit 1e2d228

Browse files
committed
[euler/prob029] add version using feeds
1 parent 57bdd7d commit 1e2d228

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

euler/prob029-gerdr.pl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# count duplicates
2323
for %powers.values -> \p {
2424
for 2..B -> \e {
25-
# raise to power e
25+
# raise to power \e
2626
# classify by root and relative exponent
2727
++%count{p.key => p.value * e}
2828
}
@@ -32,21 +32,39 @@
3232
return (A - 1) * (B - 1) + %count - [+] %count.values;
3333
}
3434

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+
3549
sub bench(|) {
3650
my \start = now;
3751
my \result = callsame;
3852
my \end = now;
3953
return result, round (end - start) * 1000;
4054
}
4155

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)
4458
}
4559

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) {
4861
&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);
4967

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;
5270
}

0 commit comments

Comments
 (0)