Prime conspiracy: Difference between revisions

→‎{{header|REXX}}: rewritten to be readable
(→‎{{header|REXX}}: rewritten to be readable)
 
(15 intermediate revisions by 13 users not shown)
Line 58:
</pre>
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V limit = 1000000
V k = limit
V n = k * 17
V primes = [1B] * n
primes[0] = primes[1] = 0B
 
L(i) 2..Int(sqrt(n))
I !primes[i]
L.continue
L(j) (i * i .< n).step(i)
primes[j] = 0B
 
DefaultDict[(Int, Int), Int] trans_map
V prev = -1
 
L(i) 0 .< n
I primes[i]
I prev != -1
trans_map[(prev, i % 10)]++
prev = i % 10
 
I --k == 0
L.break
 
print(‘First #. primes. Transitions prime % 10 > next-prime % 10.’.format(limit))
L(trans) sorted(trans_map.keys())
print(‘#. -> #. count #5 frequency: #.4%’.format(trans[0], trans[1], trans_map[trans], 100.0 * trans_map[trans] / limit))</syntaxhighlight>
 
{{out}}
<pre>
First 1000000 primes. Transitions prime % 10 > next-prime % 10.
1 -> 1 count 42853 frequency: 4.2853%
1 -> 3 count 77475 frequency: 7.7475%
1 -> 7 count 79453 frequency: 7.9453%
1 -> 9 count 50153 frequency: 5.0153%
2 -> 3 count 1 frequency: 0.0001%
3 -> 1 count 58255 frequency: 5.8255%
3 -> 3 count 39668 frequency: 3.9668%
3 -> 5 count 1 frequency: 0.0001%
3 -> 7 count 72827 frequency: 7.2827%
3 -> 9 count 79358 frequency: 7.9358%
5 -> 7 count 1 frequency: 0.0001%
7 -> 1 count 64230 frequency: 6.4230%
7 -> 3 count 68595 frequency: 6.8595%
7 -> 7 count 39603 frequency: 3.9603%
7 -> 9 count 77586 frequency: 7.7586%
9 -> 1 count 84596 frequency: 8.4596%
9 -> 3 count 64371 frequency: 6.4371%
9 -> 7 count 58130 frequency: 5.8130%
9 -> 9 count 42843 frequency: 4.2843%
</pre>
 
=={{header|ALGOL 68}}==
Solves the basic task (1 000 000 primes) using the standard sieve of Eratosthanes.
The sieve is represented by an array of BITS (32-bit items in Algol 68G).
<langsyntaxhighlight lang="algol68"># extend SET, CLEAR and ELEM to operate on rows of BITS #
OP SET = ( INT n, REF[]BITS b )REF[]BITS:
BEGIN
Line 140 ⟶ 195:
FI
OD
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 164 ⟶ 219:
9->9 42843 4.28
</pre>
 
=={{header|AppleScript}}==
<big><code>&nbsp;i </code></big>'s "preference" for any <big><code>&nbsp;j </code></big> is a percentage of the transitions from <big><code>&nbsp;i </code></big>, not of all the transitions in the collection!
 
This takes three and a half minutes on my machine. But it's not a script you'd need to use every day.
 
<syntaxhighlight lang="applescript">on isPrime(n)
if ((n < 4) or (n is 5)) then return (n > 1)
if ((n mod 2 = 0) or (n mod 3 = 0) or (n mod 5 = 0)) then return false
repeat with i from 7 to (n ^ 0.5) div 1 by 30
if ((n mod i = 0) or (n mod (i + 4) = 0) or (n mod (i + 6) = 0) or ¬
(n mod (i + 10) = 0) or (n mod (i + 12) = 0) or (n mod (i + 16) = 0) or ¬
(n mod (i + 22) = 0) or (n mod (i + 24) = 0)) then return false
end repeat
return true
end isPrime
 
on conspiracy(limit)
script o
property counters : {{0, 0, 0, 0, 0, 0, 0, 0, 0}}
end script
repeat 8 times
copy beginning of o's counters to end of o's counters
end repeat
if (limit > 1) then
set primeCount to 1
set i to 2 -- First prime.
set n to 3 -- First number to test for primality.
repeat until (primeCount = limit)
if (isPrime(n)) then
set primeCount to primeCount + 1
set j to n mod 10
set item j of item i of o's counters to (item j of item i of o's counters) + 1
set i to j
end if
set n to n + 2
end repeat
end if
set output to {"First " & limit & " primes: transitions between end digits of consecutive primes."}
set totalTransitions to limit - 1
repeat with i in {1, 2, 3, 5, 7, 9}
set iTransitions to 0
repeat with j from 1 to 9 by 2
set iTransitions to iTransitions + (item j of item i of o's counters)
end repeat
repeat with j from 1 to 9 by 2
set ijCount to item j of item i of o's counters
if (ijCount > 0) then ¬
set end of output to ¬
(i as text) & " → " & j & ¬
(" count: " & ijCount) & ¬
(" preference for " & j & ": " & (ijCount * 10000 / iTransitions as integer) / 100) & ¬
("% overall occurrence: " & (ijCount * 10000 / totalTransitions as integer) / 100 & "%")
end repeat
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output
end conspiracy
 
conspiracy(1000000)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"First 1000000 primes: transitions between end digits of consecutive primes.
1 → 1 count: 42853 preference for 1: 17.15% overall occurrence: 4.29%
1 → 3 count: 77475 preference for 3: 31.0% overall occurrence: 7.75%
1 → 7 count: 79453 preference for 7: 31.79% overall occurrence: 7.95%
1 → 9 count: 50153 preference for 9: 20.07% overall occurrence: 5.02%
2 → 3 count: 1 preference for 3: 100.0% overall occurrence: 0.0%
3 → 1 count: 58255 preference for 1: 23.29% overall occurrence: 5.83%
3 → 3 count: 39668 preference for 3: 15.86% overall occurrence: 3.97%
3 → 5 count: 1 preference for 5: 0.0% overall occurrence: 0.0%
3 → 7 count: 72827 preference for 7: 29.12% overall occurrence: 7.28%
3 → 9 count: 79358 preference for 9: 31.73% overall occurrence: 7.94%
5 → 7 count: 1 preference for 7: 100.0% overall occurrence: 0.0%
7 → 1 count: 64230 preference for 1: 25.69% overall occurrence: 6.42%
7 → 3 count: 68595 preference for 3: 27.44% overall occurrence: 6.86%
7 → 7 count: 39603 preference for 7: 15.84% overall occurrence: 3.96%
7 → 9 count: 77586 preference for 9: 31.03% overall occurrence: 7.76%
9 → 1 count: 84596 preference for 1: 33.85% overall occurrence: 8.46%
9 → 3 count: 64371 preference for 3: 25.75% overall occurrence: 6.44%
9 → 7 count: 58130 preference for 7: 23.26% overall occurrence: 5.81%
9 → 9 count: 42843 preference for 9: 17.14% overall occurrence: 4.28%"</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
Line 287 ⟶ 430:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1000000 primes, last prime considered: 15485863
Line 312 ⟶ 455:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace PrimeConspiracy {
Line 358 ⟶ 501:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 -> 1 count: 42853 frequency : 4.29%
Line 381 ⟶ 524:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <vector>
#include <iostream>
#include <cmath>
Line 435 ⟶ 578:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.29 %
Line 460 ⟶ 603:
===Alternative using primesieve library===
{{libheader|Primesieve}}
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 490 ⟶ 633:
compute_transitions(100000000);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 538 ⟶ 681:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.range;
import std.stdio;
Line 590 ⟶ 733:
writefln(" frequency: %4.2f%%", transMap[trans] / 10_000.0);
}
}</langsyntaxhighlight>
{{out}}
<pre>First 1,000,000 primes. Transitions prime % 10 -> next-prime % 10.
Line 612 ⟶ 755:
9 -> 7 count: 58130 frequency: 5.81%
9 -> 9 count: 42843 frequency: 4.28%</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
# test only odd numbers
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
func nextprim num .
repeat
num += 2
until isprim num = 1
.
return num
.
len d[][] 9
for i to 9
len d[i][] 9
.
d[2][3] = 1
p = 3
for i to 1000000
pp = p
p = nextprim p
d[pp mod 10][p mod 10] += 1
.
for i to 9
for j to 9
if d[i][j] > 0
print i & " -> " & j & ": " & d[i][j] & " = " & d[i][j] / 10000 & "%"
.
.
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'math) ;; (in-primes n) stream
(decimals 4)
Line 634 ⟶ 817:
(vector+= trans (+ (* (% p1 m) m) (% p2 m)) 1))
(print-trans trans m N))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 657 ⟶ 840:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Prime do
def conspiracy(m) do
IO.puts "#{m} first primes. Transitions prime % 10 → next-prime % 10."
Line 683 ⟶ 866:
end
 
Prime.conspiracy(1000000)</langsyntaxhighlight>
 
{{out}}
Line 711 ⟶ 894:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Prime Conspiracy. Nigel Galloway: March 27th., 2018
primes|>Seq.take 10000|>Seq.map(fun n->n%10)|>Seq.pairwise|>Seq.countBy id|>Seq.groupBy(fun((n,_),_)->n)|>Seq.sortBy(fst)
|>Seq.iter(fun(_,n)->Seq.sortBy(fun((_,n),_)->n) n|>Seq.iter(fun((n,g),z)->printfn "%d -> %d ocurred %3d times" n g z))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 740 ⟶ 923:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs formatting grouping kernel math math.primes math.statistics
sequences sorting ;
IN: rosetta-code.prime-conspiracy
Line 759 ⟶ 942:
1,000,000 dup header transitions [ print-trans ] each ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 785 ⟶ 968:
 
=={{header|Fortran}}==
Avoiding base ten chauvinism, here are results for bases two to thirteen. The source file relies on the [[Extensible_prime_generator]] project for its collection of primes. The bitbag file being in place, execution takes about two minutes for a hundred million primes, approaching the thirty-two bit limit. <langsyntaxhighlight Fortranlang="fortran"> PROGRAM INHERIT !Last digit persistence in successive prime numbers.
USE PRIMEBAG !Inherit this also.
INTEGER MBASE,P0,NHIC !Problem bounds.
Line 828 ⟶ 1,011:
END DO !On to the next successor digit.
END DO !On to the next base.
END !That was easy.</langsyntaxhighlight>
 
Results: just the counts - with the total number being a power of ten, percentages are deducible by eye. Though one could add row and column percentages as a further feature.
Line 1,049 ⟶ 1,232:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 13-04-2017
' updated 09-08-2018 Using bit-sieve of odd numbers
' compile with: fbc -s console
Line 1,106 ⟶ 1,289:
Sleep
End
</syntaxhighlight>
</lang>
{{out}}
Output is shown side by side
Line 1,135 ⟶ 1,318:
 
Expect a run time of ~20−60 seconds to generate and process the full 100 million primes.
<langsyntaxhighlight Golang="go">package main
 
import (
Line 1,206 ⟶ 1,389:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,275 ⟶ 1,458:
=={{header|Haskell}}==
Uses Primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html
<langsyntaxhighlight lang="haskell">import Data.List (group, sort)
import Text.Printf (printf)
import Data.Numbers.Primes (primes)
Line 1,287 ⟶ 1,470:
main :: IO ()
main = mapM_ line $ groups primes
where groups = tail . group . sort . (\n -> zip (0: n) n) . fmap (`mod` 10) . take 10000</langsyntaxhighlight>
{{out}}
<pre>
Line 1,315 ⟶ 1,498:
This gets the job done:
 
<langsyntaxhighlight Jlang="j"> /:~ (~.,. ' ',. ":@(%/&1 999999)@(#/.~)) 2 (,'->',])&":/\ 10|p:i.1e6
1->1 42853 0.042853
1->3 77475 0.0774751
Line 1,334 ⟶ 1,517:
9->3 64371 0.0643711
9->7 58130 0.0581301
9->9 42843 0.042843</langsyntaxhighlight>
 
Note that the [[Sieve of Eratosthenes]] task has some important implications for how often we will see the various transitions here.
Line 1,358 ⟶ 1,541:
Or, if you prefer the ratios formatted as percents, you could do this:
 
<langsyntaxhighlight Jlang="j"> /:~ (~.,. ' ',. '%',.~ ":@(%/&1 9999.99)@(#/.~)) 2 (,'->',])&":/\ 10|p:i.1e6
1->1 42853 4.2853%
1->3 77475 7.74751%
Line 1,377 ⟶ 1,560:
9->3 64371 6.43711%
9->7 58130 5.81301%
9->9 42843 4.2843%</langsyntaxhighlight>
 
'''Extra Credit:'''
Line 1,387 ⟶ 1,570:
In other words:
 
<langsyntaxhighlight Jlang="j"> dgpairs=: 2 (,'->',])&":/\ 10 | p:
combine=: ~.@[ ,. ' ',. ":@(%/&1 99999999)@(+//.)
/:~ combine&;/|: (~.;#/.~)@dgpairs@((+ i.)/)"1 (1e6*i.100),.1e6+99>i.100
Line 1,408 ⟶ 1,591:
9->3 6.37294e6 0.0637294
9->7 6.01274e6 0.0601274
9->9 4.62292e6 0.0462292</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class PrimeConspiracy {
 
public static void main(String[] args) {
Line 1,455 ⟶ 1,638:
return composite;
}
}</langsyntaxhighlight>
 
<pre>1 -> 1 : 4,285300
Line 1,476 ⟶ 1,659:
9 -> 7 : 5,813000
9 -> 9 : 4,284300</pre>
 
=={{header|jq}}==
<syntaxhighlight lang=jq>
# Input should be an integer
def isPrime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
else 5
| until( . <= 0;
if .*. > $n then -1
elif ($n % . == 0) then 0
else . + 2
| if ($n % . == 0) then 0
else . + 4
end
end)
| . == -1
end;
 
# The first $n primes
def sieved($n):
[limit($n; range(2;infinite) | select(isPrime)) ];
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# right-pad with 0
def rpad($len): tostring | ($len - length) as $l | ("0" * $l)[:$l] + .;
 
# Input: a string of digits with up to one "."
# Output: the corresponding string representation with exactly $n decimal digits
def align_decimal($n):
tostring
| (capture("(?<i>[0-9]*[.])(?<j>[0-9]{0," + ($n|tostring) + "})") as $ix
| $ix.i + ($ix.j|rpad($n)) )
// . + "." + ($n*"0") ;
 
# Report the noteworthy transitions recorded in the input object
def reportTransitions:
([.[]] | add) as $num
| keys as $keys
| "For the first \($num + 1) primes, the noteworthy transitions of the last digit from prime to next-prime are:",
($keys[] as $key
| .[$key] as $count
| select($key | IN("2 => 3", "3 => 5", "5 => 7") | not)
| ($count / $num * 100) as $freq
| "\($key) count: \($count|lpad(6)) frequency: \($freq | align_decimal(4))%" ) ;
 
def tasks:
1E6 as $n
| sieved($n) as $sieved
| (1e4, 1e6) as $num
| reduce range(1; $num) as $i ({};
($sieved[$i] % 10) as $p
| ($sieved[$i-1] % 10) as $q
| "\($q) => \($p)" as $key
| .[$key] += 1)
| reportTransitions, "";
 
tasks
</syntaxhighlight>
'''Invocation''': jq -nr -f prime-conspiracy.jq
{{output}}
<pre>
For the first 10000 primes, the noteworthy transitions of the last digit from prime to next-prime are:
1 => 1 count: 365 frequency: 3.6503%
1 => 3 count: 833 frequency: 8.3308%
1 => 7 count: 889 frequency: 8.8908%
1 => 9 count: 397 frequency: 3.9703%
3 => 1 count: 529 frequency: 5.2905%
3 => 3 count: 324 frequency: 3.2403%
3 => 7 count: 754 frequency: 7.5407%
3 => 9 count: 907 frequency: 9.0709%
7 => 1 count: 655 frequency: 6.5506%
7 => 3 count: 722 frequency: 7.2207%
7 => 7 count: 323 frequency: 3.2303%
7 => 9 count: 808 frequency: 8.0808%
9 => 1 count: 935 frequency: 9.3509%
9 => 3 count: 635 frequency: 6.3506%
9 => 7 count: 541 frequency: 5.4105%
9 => 9 count: 379 frequency: 3.7903%
 
For the first 1000000 primes, the noteworthy transitions of the last digit from prime to next-prime are:
1 => 1 count: 42853 frequency: 4.2853%
1 => 3 count: 77475 frequency: 7.7475%
1 => 7 count: 79453 frequency: 7.9453%
1 => 9 count: 50153 frequency: 5.0153%
3 => 1 count: 58255 frequency: 5.8255%
3 => 3 count: 39668 frequency: 3.9668%
3 => 7 count: 72827 frequency: 7.2827%
3 => 9 count: 79358 frequency: 7.9357%
7 => 1 count: 64230 frequency: 6.4229%
7 => 3 count: 68595 frequency: 6.8595%
7 => 7 count: 39603 frequency: 3.9603%
7 => 9 count: 77586 frequency: 7.7586%
9 => 1 count: 84596 frequency: 8.4596%
9 => 3 count: 64371 frequency: 6.4371%
9 => 7 count: 58130 frequency: 5.0813%
9 => 9 count: 42843 frequency: 4.2843%
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf, Primes
using DataStructures
 
Line 1,499 ⟶ 1,784:
for ((i, j), fr) in trans
@printf("%i → %i: freq. %3.4f%%\n", i, j, 100fr / tot)
end</langsyntaxhighlight>
 
{{out}}
Line 1,524 ⟶ 1,809:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning
 
Line 1,571 ⟶ 1,856:
println(" frequency: ${"%4.2f".format(transMap[trans]!! / 10000.0)}%")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,599 ⟶ 1,884:
=={{header|Lua}}==
Takes about eight seconds with a limit of 10^6. It could of course be changed to 10^8 for the extra credit but the execution time is longer than my patience lasted.
<langsyntaxhighlight Lualang="lua">-- Return boolean indicating whether or not n is prime
function isPrime (n)
if n <= 1 then return false end
Line 1,649 ⟶ 1,934:
end
end
end</langsyntaxhighlight>
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.2853 %
Line 1,670 ⟶ 1,955:
9 -> 7 count: 58130 frequency: 5.813 %
9 -> 9 count: 42843 frequency: 4.2843 %</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We do just the challenge with 10^8 primes, 10^6 primes is an easy modification by changing the 10^8 to 10^6. The first line is just the string formatting, the actual calculation is the smaller second line.
<syntaxhighlight lang="mathematica">
StringForm["`` count: `` frequency: ``", Rule@@ #[[1]], StringPadLeft[ToString@ #[[2]], 8], PercentForm[N@ #[[2]]/(10^8 -1)]]& /@
Sort[Tally[Partition[Mod[Prime[Range[10^8]], 10], 2, 1]]] // Column
</syntaxhighlight>
 
 
{{out}}
<pre>
1->1 count: 4623041 frequency: 4.623%
1->3 count: 7429438 frequency: 7.429%
1->7 count: 7504612 frequency: 7.505%
1->9 count: 5442344 frequency: 5.442%
2->3 count: 1 frequency: 0.000001%
3->1 count: 6010981 frequency: 6.011%
3->3 count: 4442561 frequency: 4.443%
3->5 count: 1 frequency: 0.000001%
3->7 count: 7043695 frequency: 7.044%
3->9 count: 7502896 frequency: 7.503%
5->7 count: 1 frequency: 0.000001%
7->1 count: 6373982 frequency: 6.374%
7->3 count: 6755195 frequency: 6.755%
7->7 count: 4439355 frequency: 4.439%
7->9 count: 7431870 frequency: 7.432%
9->1 count: 7991431 frequency: 7.991%
9->3 count: 6372940 frequency: 6.373%
9->7 count: 6012739 frequency: 6.013%
9->9 count: 4622916 frequency: 4.623%
</pre>
 
=={{header|Nim}}==
We use a sieve of Erathostenes for odd values only. This allows to find the result for 10 000, 1 000 000 and 100 000 000 primes in about 16 seconds.
 
<syntaxhighlight lang="nim"># Prime conspiracy.
<lang Nim>
# Prime conspiracy.
 
fromimport std/[algorithm, importmath, sequtils, strformat, sortedtables]
from math import sqrt
from sequtils import toSeq
from strformat import fmt
import tables
 
const N = 1_020_000_000.int # Size of sieve of Eratosthenes.
 
proc newSieve(): seq[bool] =
Line 1,721 ⟶ 2,032:
# Check if sieve was big enough.
if count < nprimes:
echo fmt&"Found only {count} primes; expected {nprimes} primes. Increase value of N."
quit(QuitFailure)
 
# Print result.
echo fmt&"{nprimes} first primes. Transitions prime %(mod 10) → next-prime %(mod 10)."
for key in sorted(toSeq(counts.keys).toSeq):
let count = counts[key]
let freq = count.toFloat * 100 / nprimes.toFloat
echo fmt&"{key[0]} -> {key[1]}: count Count: {count:7d} frequency Frequency: {freq:4.2f}%"
echo ""
 
Line 1,735 ⟶ 2,046:
isPrime.countTransitions(1_000_000)
isPrime.countTransitions(100_000_000)
</syntaxhighlight>
</lang>
{{out}}
<pre>
10000 first primes. Transitions prime %(mod 10) → next-prime %(mod 10).
1 → 1: count Count: 365 frequency Frequency: 3.65%
1 → 3: count Count: 833 frequency Frequency: 8.33%
1 → 7: count Count: 889 frequency Frequency: 8.89%
1 → 9: count Count: 397 frequency Frequency: 3.97%
2 → 3: count Count: 1 frequency Frequency: 0.01%
3 → 1: count Count: 529 frequency Frequency: 5.29%
3 → 3: count Count: 324 frequency Frequency: 3.24%
3 → 5: count Count: 1 frequency Frequency: 0.01%
3 → 7: count Count: 754 frequency Frequency: 7.54%
3 → 9: count Count: 907 frequency Frequency: 9.07%
5 → 7: count Count: 1 frequency Frequency: 0.01%
7 → 1: count Count: 655 frequency Frequency: 6.55%
7 → 3: count Count: 722 frequency Frequency: 7.22%
7 → 7: count Count: 323 frequency Frequency: 3.23%
7 → 9: count Count: 808 frequency Frequency: 8.08%
9 → 1: count Count: 935 frequency Frequency: 9.35%
9 → 3: count Count: 635 frequency Frequency: 6.35%
9 → 7: count Count: 541 frequency Frequency: 5.41%
9 → 9: count Count: 379 frequency Frequency: 3.79%
 
1000000 first primes. Transitions prime %(mod 10) → next-prime %(mod 10).
1 → 1: count Count: 42853 frequency Frequency: 4.29%
1 → 3: count Count: 77475 frequency Frequency: 7.75%
1 → 7: count Count: 79453 frequency Frequency: 7.95%
1 → 9: count Count: 50153 frequency Frequency: 5.02%
2 → 3: count Count: 1 frequency Frequency: 0.00%
3 → 1: count Count: 58255 frequency Frequency: 5.83%
3 → 3: count Count: 39668 frequency Frequency: 3.97%
3 → 5: count Count: 1 frequency Frequency: 0.00%
3 → 7: count Count: 72827 frequency Frequency: 7.28%
3 → 9: count Count: 79358 frequency Frequency: 7.94%
5 → 7: count Count: 1 frequency Frequency: 0.00%
7 → 1: count Count: 64230 frequency Frequency: 6.42%
7 → 3: count Count: 68595 frequency Frequency: 6.86%
7 → 7: count Count: 39603 frequency Frequency: 3.96%
7 → 9: count Count: 77586 frequency Frequency: 7.76%
9 → 1: count Count: 84596 frequency Frequency: 8.46%
9 → 3: count Count: 64371 frequency Frequency: 6.44%
9 → 7: count Count: 58130 frequency Frequency: 5.81%
9 → 9: count Count: 42843 frequency Frequency: 4.28%
 
100000000 first primes. Transitions prime %(mod 10) → next-prime %(mod 10).
1 → 1: count Count: 4623041 frequency Frequency: 4.62%
1 → 3: count Count: 7429438 frequency Frequency: 7.43%
1 → 7: count Count: 7504612 frequency Frequency: 7.50%
1 → 9: count Count: 5442344 frequency Frequency: 5.44%
2 → 3: count Count: 1 frequency Frequency: 0.00%
3 → 1: count Count: 6010981 frequency Frequency: 6.01%
3 → 3: count Count: 4442561 frequency Frequency: 4.44%
3 → 5: count Count: 1 frequency Frequency: 0.00%
3 → 7: count Count: 7043695 frequency Frequency: 7.04%
3 → 9: count Count: 7502896 frequency Frequency: 7.50%
5 → 7: count Count: 1 frequency Frequency: 0.00%
7 → 1: count Count: 6373982 frequency Frequency: 6.37%
7 → 3: count Count: 6755195 frequency Frequency: 6.76%
7 → 7: count Count: 4439355 frequency Frequency: 4.44%
7 → 9: count Count: 7431870 frequency Frequency: 7.43%
9 → 1: count Count: 7991431 frequency Frequency: 7.99%
9 → 3: count Count: 6372940 frequency Frequency: 6.37%
9 → 7: count Count: 6012739 frequency Frequency: 6.01%
9 → 9: count Count: 4622916 frequency Frequency: 4.62%
</pre>
 
=={{header|PARI/GP}}==
 
<langsyntaxhighlight lang="parigp">
conspiracy(maxx) = {
print("primes considered= ", maxx);
x = matrix(9, 9);cnt=0;p=2;q=2%10;
while( cnt< =maxx, 0;
p = 2;
cnt+=1;
q = 2 % 10;
m=q;
 
p=nextprime(p+1);
while (cnt <= maxx,
q= p%10;
cnt += 1;
x[m,q]+=1);
m = q;
print (2," to ",3, " count: ",x[2,3]," freq ", 100./cnt," %" );
p = nextprime(p + 1);
forstep(i=1,9,2,
q = p % 10;
forstep(j=1,9,2,
x[m, q] += 1
if( x[i,j]<1,continue);
);
print (i," to ",j, " count: ",x[i,j]," freq ", 100.* x[i,j]/cnt," %" )));
 
print ("total transitions= ",cnt);
printf("2 to 3 count: %d freq %.6f %s\n", x[2, 3], 100. *x[2,3]/cnt, "%");
print(p);
 
forstep(i = 1, 9, 2,
forstep(j = 1, 9, 2,
if (x[i, j] < 1, continue);
printf("%d to %d count: %d freq %.6f %s\n", i, j, x[i, j], 100. *x[i,j]/cnt, "%");
)
);
 
print("total transitions= ", cnt);
print(p);
}
 
</lang>
conspiracy(1000000);
</syntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="parigp">
primes considered= 1000000
2 to 3 count: 1 freq 0.000100 %
1 to 1 count: 42853 freq 4.29 285296 %
1 to 3 count: 77475 freq 7.75 747492 %
1 to 5 count: 0 freq 0 .000000 %
1 to 7 count: 79453 freq 7.95 945292 %
1 to 9 count: 50153 freq 5.02 015295 %
3 to 1 count: 58255 freq 5.83 825494 %
3 to 3 count: 39668 freq 3.97 966796 %
3 to 5 count: 1 freq 0.000100 %
3 to 7 count: 72828 freq 7.28 282793 %
3 to 9 count: 79358 freq 7.94 935792 %
5 to 1 count: 0 freq 0 .000000 %
5 to 3 count: 0 freq 0 .000000 %
5 to 5 count: 0 freq 0 .000000 %
5 to 7 count: 1 freq 0.000100 %
5 to 9 count: 0 freq 0 .000000 %
7 to 1 count: 64230 freq 6.42 422994 %
7 to 3 count: 68595 freq 6.86 859493 %
7 to 5 count: 0 freq 0 .000000 %
7 to 7 count: 39604 freq 3.96 960396 %
7 to 9 count: 77586 freq 7.76 758592 %
9 to 1 count: 84596 freq 8.46 459592 %
9 to 3 count: 64371 freq 6.44 437094 %
9 to 5 count: 0 freq 0 .000000 %
9 to 7 count: 58130 freq 5.81 812994 %
9 to 9 count: 42843 freq 4.28 284296 %
total transitions= 1000001
15485917
 
time = 5,016 ms.
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
Line 1,864 ⟶ 2,187:
 
'''Extra credit:''' is included PrimeLimit = 2038074743-> 100'000'000 Primes
<langsyntaxhighlight lang="pascal">
program primCons;
{$IFNDEF FPC}
Line 1,992 ⟶ 2,315:
OutputTransitions(CntTransitions);
end.
</syntaxhighlight>
</lang>
{{Out}}
<pre>PrimCnt 10000 100000 1000000 10000000 100000000
Line 2,026 ⟶ 2,349:
{{libheader|ntheory}}
 
<langsyntaxhighlight lang="perl">use ntheory qw/forprimes nth_prime/;
 
my $upto = 1_000_000;
Line 2,040 ⟶ 2,363:
printf "%s → %s count:\t%7d\tfrequency: %4.2f %%\n",
substr($_,0,1), substr($_,1,1), $freq{$_}, 100*$freq{$_}/$upto
for sort keys %freq;</langsyntaxhighlight>
{{out}}
<pre>1000000 first primes. Transitions prime % 10 → next-prime % 10.
Line 2,086 ⟶ 2,409:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence p10k = get_primes(-10000)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence transitions = repeat(repeat(0,9),9)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p10k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">10_000</span><span style="color: #0000FF;">),</span>
integer last = p10k[1], this
<span style="color: #000000;">transitions</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #000000;">9</span><span style="color: #0000FF;">)</span>
for i=2 to length(p10k) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p10k</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p10k</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">curr</span>
this = remainder(p10k[i],10)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
transitions[last][this] += 1
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p10k</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
last = this
<span style="color: #000000;">transitions</span><span style="color: #0000FF;">[</span><span style="color: #000000;">last</span><span style="color: #0000FF;">][</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
for i=1 to 9 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for j=1 to 9 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
if transitions[i][j]!=0 then
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
printf(1,"%d->%d:%3.2f%%\n",{i,j,transitions[i][j]*100/length(p10k)})
<span style="color: #004080;">atom</span> <span style="color: #000000;">tij</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">transitions</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">tij</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end for
<span style="color: #004080;">atom</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tij</span><span style="color: #0000FF;">*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">/</span><span style="color: #000000;">l</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d-&gt;%d:%3.2f%%\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,123 ⟶ 2,451:
9->9:3.79%
</pre>
Of course it is very silly to include 2 and 5 in the analysis since they're only going to appear once, and in fact if you remove them and sort by difference, with 0 here meaning +10 and -ve diffs being a "roll over", the only real outlier is 1->9, being about half what you might expect, though there does seem to be a bit of a clear bias between rolled-over and not-rolled over, namely the 6%s vs. the 7%s:
Unfortunately, that prime number generator uses a table: while 1 million primes needs ~16MB*4|8, 10 million needs ~180MB*4|8, (both easily done on either 32 or 64 bit) 100 million would need ~2GB*8, (clearly 64 bit only) which is more than this 4GB box can allocate, it seems.
<!--<syntaxhighlight lang="phix">(phixonline)-->
But it might work on a machine with lots more memory.
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p1m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">transitions</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1m</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p1m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">transitions</span><span style="color: #0000FF;">[</span><span style="color: #000000;">last</span><span style="color: #0000FF;">][</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">tij</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">transitions</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">tij</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tij</span><span style="color: #0000FF;">*</span><span style="color: #000000;">100</span><span style="color: #0000FF;">/</span><span style="color: #000000;">l</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%2d, %d-&gt;%d:%3.2f%%\n"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">results</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
<pre>
-8, 9->1:8.46%
-6, 7->1:6.42%
-6, 9->3:6.44%
-4, 7->3:6.86%
-2, 3->1:5.83%
-2, 9->7:5.81%
0, 1->1:4.29%
0, 3->3:3.97%
0, 7->7:3.96%
0, 9->9:4.28%
2, 1->3:7.75%
2, 7->9:7.76%
4, 3->7:7.28%
6, 1->7:7.95%
6, 3->9:7.94%
8, 1->9:5.02%
</pre>
 
=={{header|Picat}}==
(Note: Adjustment for 1-based indices.)
<syntaxhighlight lang="picat">go =>
N = 15_485_863, % 1_000_000 primes
Primes = {P mod 10 : P in primes(N)},
Len = Primes.len,
A = new_array(10,10), bind_vars(A,0),
foreach(I in 2..Len)
P1 = 1 + Primes[I-1], % adjust for 1-based
P2 = 1 + Primes[I],
A[P1,P2] := A[P1,P2] + 1
end,
foreach(I in 0..9, J in 0..9, V = A[I+1,J+1], V > 0)
printf("%d -> %d count: %5d frequency: %0.4f%%\n", I,J,V,100*V/Len)
end,
nl.</syntaxhighlight>
 
{{out}}
<pre>num_primes = 1000000
1 -> 1 count: 42853 frequency: 4.2853%
1 -> 3 count: 77475 frequency: 7.7475%
1 -> 7 count: 79453 frequency: 7.9453%
1 -> 9 count: 50153 frequency: 5.0153%
2 -> 3 count: 1 frequency: 0.0001%
3 -> 1 count: 58255 frequency: 5.8255%
3 -> 3 count: 39668 frequency: 3.9668%
3 -> 5 count: 1 frequency: 0.0001%
3 -> 7 count: 72827 frequency: 7.2827%
3 -> 9 count: 79358 frequency: 7.9358%
5 -> 7 count: 1 frequency: 0.0001%
7 -> 1 count: 64230 frequency: 6.4230%
7 -> 3 count: 68595 frequency: 6.8595%
7 -> 7 count: 39603 frequency: 3.9603%
7 -> 9 count: 77586 frequency: 7.7586%
9 -> 1 count: 84596 frequency: 8.4596%
9 -> 3 count: 64371 frequency: 6.4371%
9 -> 7 count: 58130 frequency: 5.8130%
9 -> 9 count: 42843 frequency: 4.2843%</pre>
 
 
=={{header|PicoLisp}}==
I'm using the fast version from the Sieve of Eratosthanes task to create the list of primes.
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
(load "pluser/sieve.l") # See the task "Sieve of Eratosthanes"
 
Line 2,175 ⟶ 2,583:
(T
(cons (car Tally) (bump-trans Trans (cdr Tally))))))
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,203 ⟶ 2,611:
=={{header|Prolog}}==
While the program can handle a million primes, it's rather slow, so I've capped it to accept up to 100,000 primes.
<langsyntaxhighlight lang="prolog">
% table of nth prime values (up to 100,000)
 
Line 2,263 ⟶ 2,671:
plus(M, N, M2),
remove_multiples(N, M2, L, R).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,291 ⟶ 2,699:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">def isPrime(n):
if n < 2:
return False
Line 2,339 ⟶ 2,747:
print "First {:,} primes. Transitions prime % 10 > next-prime % 10.".format(limit)
for trans in sorted(transMap):
print "{0} -> {1} count {2:5} frequency: {3}%".format(trans[0], trans[1], transMap[trans], 100.0 * transMap[trans] / limit)</langsyntaxhighlight>
{{out}}
<pre>First 1,000,000 primes. Transitions prime % 10 > next-prime % 10.
Line 2,363 ⟶ 2,771:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
suppressMessages(library(gmp))
 
Line 2,389 ⟶ 2,797:
cat(sprintf("%d",limit),"first primes. Transitions prime % 10 -> next-prime % 10\n")
invisible(sapply(1:99,getOutput))
</syntaxhighlight>
</lang>
<pre>
1000000 first primes. Transitions prime % 10 -> next-prime % 10
Line 2,415 ⟶ 2,823:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 2,433 ⟶ 2,841:
(match-define (cons (cons x y) freq) item)
(printf "~a → ~a count: ~a frequency: ~a %\n"
x y (~a freq #:min-width 8 #:align 'right) (~r (* 100 freq (/ 1 limit)) #:precision '(= 2))))</langsyntaxhighlight>
 
{{out}}
Line 2,463 ⟶ 2,871:
{{works with|Rakudo|2018.9}}
Using module <code>Math::Primesieve</code> to generate primes, as much faster than the built-in (but extra credit still very slow).
<syntaxhighlight lang="raku" perl6line>use Math::Primesieve;
 
my %conspiracy;
Line 2,476 ⟶ 2,884:
}
 
say "$_ \tfrequency: {($_.value/$upto*100).round(.01)} %" for %conspiracy.sort;</langsyntaxhighlight>
{{out}}
<pre>1 → 1 count: 42853 frequency: 4.29 %
Line 2,500 ⟶ 2,908:
=={{header|REXX}}==
The first &nbsp; '''do''' &nbsp; loop is a modified ''Sieve of Eratosthenes'' &nbsp; &nbsp; (just for odd numbers).
<langsyntaxhighlight lang="rexx">/*REXX pgm shows a table of whatwhich last digit follows the previous last digit for N primes*/
parse/* argfor N .primes /*N: the number of primes to be genned*/
Call time 'R'
if N=='' | N=="," then N= 1000000 /*Not specified? Then use the default.*/
Numeric Digits 12
Np= N+1; w= length(N-1) /*W: width used for formatting output.*/
H=Parse N*Arg (2**max(4,n (w%2+1). ) ) /*used asN: a roughthe limitnumber forof theprimes sieve.to be looked at */
@.If n==''|n=="," .Then /* Not specified? /*assume all numbers are prime (so far)*/
# n=1000000 1 /* Use the default /*primes found so far {assume prime 2}.*/
w=length(n-1) do j=3 by 2; if @.j=='' then iterate /*Is composite?W: width Thenused skipfor thisformatting number.o*/
 
#= #+1 /*bump the prime number counter. */
h=n*(2**max(4,(w%2+1))) do m=j/*j used toas Ha rough bylimit j+j;for the sieve @.m= /*strike odd multiples as composite. */
h=h*1.2 end /*m make sure it is large enough */
prime.=1 if #==Np then leave /* assume all numbers are prime /*Enough primes? Then done with gen. */
nn=1 end /*j*/ /* primes found so far {2 is the firt /* [↑] gen using Eratosthenes' sieve. prime)*/
Do j=3 By 2 while nn<n
!.= 0 /*initialize all the frequency counters*/
If prime.j Then Do
say 'For ' N " primes used in this study:" /*show hdr information about this run. */
r= 2 nn=nn+1 /* bump the prime number counter. /*the last digit of the very 1st prime.*/
Do m=j*j To h By j+j
#= 1 /*the number of primes looked at so far*/
do iprime.m=30 by 2; if @.i=='' then iterate /*This numberstrike odd multiples as composite? Then ignore it */
End
#= # + 1; parse var i '' -1 x /*bump prime counter; get its last dig.*/
End
!.r.x= !.r.x +1; r= x /*bump the last digit counter for prev.*/
End
if #==Np then leave /*Done? Then leave this DO loop. */
Say 'Sieve of Eratosthenes finished' time('E') 'seconds'
end /*i*/ /* [↑] examine almost all odd numbers.*/
Call time 'R'
say /* [↓] display the results to the term*/
frequency.=0 do d=1 for 9; if d//2 | d==2 then say /*display ainitialize all the frequency counts blank line (if appropriate)*/
Say 'For' n 'primes used in this study:'
do f=1 for 9; if !.d.f==0 then iterate /*don't show if the count is zero. */
/*show hdr information about this run. */
say 'digit ' d "──►" f ' has a count of: ',
r=2 /* the last digit of the very 1st prime (2) */
right(!.d.f, w)", frequency of:" right(format(!.d.f / N*100, , 4)'%.', 10)
nn=1 /* the number of primes looked at */
end /*f*/
cnt.=0
end /*d*/ /*stick a fork in it, we're all done. */</lang>
cnt.2=1
Do i=3 By 2 While nn<n+1 /* Inspect all odd numbers */
If prime.i Then Do /* it is a prime number */
nn=nn+1
Parse Var i ''-1 x /* get last digit of current prime */
cnt.x+=1 /* bump last digit counter */
frequency.r.x=frequency.r.x+1 /* bump the frequency counter */
r=x /* current becomes previous */
End
End
Say 'i='i 'largest prime'
Say 'h='h
Say /* display the results */
Do d=1 For 9
If d//2|d==2 Then
Say '' /* display a blank line (if appropriate) */
Do f=1 For 9
If frequency.d.f>0 Then
Say 'digit ' d '-->' f ' has a count of: ' right(frequency.d.f,w)||,
', frequency of:' right(format(frequency.d.f/n*100,,4)'%.',10)
End
End
Say 'Frequency analysis:' time('E') 'seconds'
sum=0
Say 'last digit Number of occurrences'
Do i=1 To 9
If cnt.i>0 Then
Say ' 'i format(cnt.i,8)
sum+=cnt.i
End
Say ' 'format(sum,10)</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>Sieve of Eratosthenes finished 23.526000 seconds
<pre>
For 1000000 primes used in this study:
i=15485869 largest prime
h=19200000.0
 
digit 1 --> 1 has a count of: 42853, frequency of: 4.2853%.
digit 1 --> 3 has a count of: 77475, frequency of: 7.7475%.
digit 1 --> 7 has a count of: 79453, frequency of: 7.9453%.
digit 1 --> 9 has a count of: 50153, frequency of: 5.0153%.
 
digit 12 ──►--> 13 has a count of: 42853 1, frequency of: 40.28530001%.
digit 1 ──► 3 has a count of: 77475, frequency of: 7.7475%.
digit 1 ──► 7 has a count of: 79453, frequency of: 7.9453%.
digit 1 ──► 9 has a count of: 50153, frequency of: 5.0153%.
 
digit 23 ──►--> 31 has a count of: 158255, frequency of: 05.00018255%.
digit 3 --> 3 has a count of: 39668, frequency of: 3.9668%.
digit 3 --> 5 has a count of: 1, frequency of: 0.0001%.
digit 3 --> 7 has a count of: 72828, frequency of: 7.2828%.
digit 3 --> 9 has a count of: 79358, frequency of: 7.9358%.
 
digit 35 ──►--> 17 has a count of: 58255 1, frequency of: 50.82550001%.
digit 3 ──► 3 has a count of: 39668, frequency of: 3.9668%.
digit 3 ──► 5 has a count of: 1, frequency of: 0.0001%.
digit 3 ──► 7 has a count of: 72828, frequency of: 7.2828%.
digit 3 ──► 9 has a count of: 79358, frequency of: 7.9358%.
 
digit 57 ──►--> 71 has a count of: 164230, frequency of: 06.00014230%.
digit 7 --> 3 has a count of: 68595, frequency of: 6.8595%.
digit 7 --> 7 has a count of: 39603, frequency of: 3.9603%.
digit 7 --> 9 has a count of: 77586, frequency of: 7.7586%.
 
digit 79 ──►--> 1 has a count of: 6423084596, frequency of: 68.42304596%.
digit 79 ──►--> 3 has a count of: 6859564371, frequency of: 6.85954371%.
digit 79 ──►--> 7 has a count of: 3960358130, frequency of: 35.96038130%.
digit 79 ──►--> 9 has a count of: 7758642843, frequency of: 74.75862843%.
Frequency analysis: 5.640000 seconds
 
last digit Number of occurrences
digit 9 ──► 1 has a count of: 84596, frequency of: 8.4596%.
1 249934
digit 9 ──► 3 has a count of: 64371, frequency of: 6.4371%.
digit 9 ──► 7 has a count of:2 58130, frequency of: 5.8130%.1
3 250110
digit 9 ──► 9 has a count of: 42843, frequency of: 4.2843%.
5 1
</pre>
7 250015
9 249940
1000001</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
 
def prime_conspiracy(m)
Line 2,572 ⟶ 3,020:
end
 
prime_conspiracy(1_000_000)</langsyntaxhighlight>
{{out}}
<pre>1000000 first primes. Transitions prime % 10 → next-prime % 10.
Line 2,597 ⟶ 3,045:
=={{header|Rust}}==
Execution time is about 13 seconds on my system (macOS 10.15.4, 3.2GHz Quad-Core Intel Core i5).
<langsyntaxhighlight lang="rust">// main.rs
mod bit_array;
mod prime_sieve;
Line 2,644 ⟶ 3,092:
println!();
compute_transitions(100000000);
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="rust">// prime_sieve.rs
use crate::bit_array;
 
Line 2,681 ⟶ 3,129:
!self.composite.get(n / 2 - 1)
}
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="rust">// bit_array.rs
pub struct BitArray {
array: Vec<u32>,
Line 2,706 ⟶ 3,154:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,756 ⟶ 3,204:
Execution time is about 3 seconds on my system (macOS 10.15.4, 3.2GHz Quad-Core Intel Core i5).
Same output as above.
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.2"
 
Line 2,789 ⟶ 3,237:
println!();
compute_transitions(100000000);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
===Imperative version (Ugly, side effects)===
Con: Has to unfair assume the one millionth prime.
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
import scala.collection.mutable
 
Line 2,846 ⟶ 3,294:
 
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</langsyntaxhighlight>
===Functional version, memoizatized===
<langsyntaxhighlight Scalalang="scala">object PrimeConspiracy1 extends App {
private val oddPrimes: Stream[Int] =
3 #:: Stream.from(5, 2)
Line 2,870 ⟶ 3,318:
 
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 2,881 ⟶ 3,329:
Executing the [http://seed7.sourceforge.net/faq.htm#compile compiled] Seed7 program takes only 0.08 seconds.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 2,930 ⟶ 3,378:
flt(count * 100)/flt(total) digits 2 lpad 4 <& " %");
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,957 ⟶ 3,405:
=={{header|Sidef}}==
{{trans|zkl}}
<langsyntaxhighlight lang="ruby">var primes = (^Inf -> lazy.grep{.is_prime})
 
var upto = 1e6
Line 2,970 ⟶ 3,418:
for k,v in (conspiracy.sort_by{|k,_v| k }) {
printf("%s count: %6s\tfrequency: %2.2f %\n", k, v.commify, v / upto * 100)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,995 ⟶ 3,443:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 3,077 ⟶ 3,525:
Debug.Print K & " " & Right(" " & Dict(K), 6) & " " & Dict(K) / Nb * 100 & "%"
Next
End Sub</langsyntaxhighlight>
{{out}}
<pre>1000000 primes, last prime considered: 15485867
Line 3,134 ⟶ 3,582:
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
Limited to the first 10 million primes in order to finish in a reasonable time (around 2811.4 seconds on my system).
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
import "./sort" for Sort
 
var reportTransitions = Fn.new { |transMap, num|
Line 3,175 ⟶ 3,623:
reportTransitions.call(transMap, n)
}
System.print("Took %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 3,219 ⟶ 3,667:
9 -> 3 count: 6,513 frequency: 6.51%
9 -> 7 count: 5,671 frequency: 5.67%
9 -> 9 count: 3,995 frequency: 34.1000%
 
First 1,000,000 primes. Transitions prime % 10 -> next-prime % 10.
Line 3,263 ⟶ 3,711:
9 -> 9 count: 446,032 frequency: 4.46%
 
Took 2711.90441407733 seconds.
</pre>
 
Line 3,269 ⟶ 3,717:
{{trans|Raku}}
Using [[Extensible prime generator#zkl]].
<langsyntaxhighlight lang="zkl">const CNT =0d1_000_000;
sieve :=Import("sieve.zkl",False,False,False).postponed_sieve;
conspiracy:=Dictionary();
Line 3,279 ⟶ 3,727:
foreach key in (conspiracy.keys.sort()){ v:=conspiracy[key].toFloat();
println("%s%,6d\tfrequency: %2.2F%".fmt(key,v,v/CNT *100))
}</langsyntaxhighlight>
{{out}}
<pre>
2,294

edits