Skip to content

Instantly share code, notes, and snippets.

@SqrtNegInf
Created October 27, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SqrtNegInf/f3e9ef2807f225946871b385aeae6826 to your computer and use it in GitHub Desktop.
Save SqrtNegInf/f3e9ef2807f225946871b385aeae6826 to your computer and use it in GitHub Desktop.
Perl - Rosettacode Prime_conspiracy task
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
}
@danaj
Copy link

danaj commented Oct 29, 2018

Would you recommend replacing the code on RC or adding it as an alternative?

@SqrtNegInf
Copy link
Author

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