4 ms·
Perl 6 version: my $primes := (1..*).grep(*.is-prime); my %count; for ^100000 -> $i { %count{($primes[$i] % 10) ~ '-' ~ ($primes[$i+1] % 1
by colomon 11y ago
Perl 6 version:
my $primes := (1..*).grep(*.is-prime);
my %count;
for ^100000 -> $i {
%count{($primes[$i] % 10) ~ '-' ~ ($primes[$i+1] % 10)}++;
}
for %count.keys.sort -> $key {
say "$key = { %count{$key} }";
}
(I didn't skip the outliers, but I did explicitly write out the count so that it was in order.)
Results from the first 100,000 primes:
1-1 = 4104
1-3 = 7961
1-7 = 8297
1-9 = 4605
2-3 = 1
3-1 = 5596
3-3 = 3604
3-5 = 1
3-7 = 7419
3-9 = 8387
5-7 = 1
7-1 = 6438
7-3 = 6928
7-7 = 3627
7-9 = 8022
9-1 = 8830
9-3 = 6513
9-7 = 5671
9-9 = 3995
- colomon 11y agoAnd because I couldn't resist, here's a one-liner (for calculations, anyway) version: my %count := (1..*).grep(*.is-prime).map(* % 10).rotor(2 => -1).map(~*)[^100000].Bag; I think it's a bit slower than my previous version. (And much slower than the Rust version posted elsewhere here!)