Created
October 27, 2018 14:41
-
-
Save SqrtNegInf/f3e9ef2807f225946871b385aeae6826 to your computer and use it in GitHub Desktop.
Perl - Rosettacode Prime_conspiracy task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use ntheory qw/forprimes nth_prime/; | |
my $upto = 100_000_000; | |
my @freq; | |
my($this_digit,$last_digit)=(2,0); | |
forprimes { | |
($last_digit,$this_digit) = ($this_digit, $_ % 10); | |
$freq[$last_digit . $this_digit]++; | |
} 3,nth_prime($upto); | |
print "$upto first primes. Transitions prime % 10 → next-prime % 10.\n"; | |
for my $i (0..$#freq) { | |
next unless $freq[$i]; | |
printf "%s → %s count:\t%7d\tfrequency: %4.2f %%\n", | |
substr($i,0,1), substr($i,1,1), $freq[$i], 100*$freq[$i]/$upto | |
} |
Would you recommend replacing the code on RC or adding it as an alternative?
TIL... Yup, it all works as advertised. And I'm always in favor of keeping it simple, and putting just the best version on RC, so a vote to 'replace'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this:
Either version of the print is fine -- it doesn't matter which is used. By doing the map/grep, only the actual work portion is changed, leaving the setup and print function unchanged from the existing code (technically not true as I renamed last_digit to last_tens).
The two performance changes from your gist are:
Timing, perl 5.27.10:
The performance changes seem to be a mix of things. The fastest one leaves everything in integer form throughout the loop.