Prime conspiracy: Difference between revisions
→{{header|REXX}}: rewritten to be readable
Walterpachl (talk | contribs) (→{{header|REXX}}: rewritten to be readable) |
|||
(10 intermediate revisions by 8 users not shown) | |||
Line 62:
{{trans|Python}}
<
V k = limit
V n = k * 17
Line 88:
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))</
{{out}}
Line 117:
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).
<
OP SET = ( INT n, REF[]BITS b )REF[]BITS:
BEGIN
Line 195:
FI
OD
OD</
{{out}}
<pre>
Line 225:
This takes three and a half minutes on my machine. But it's not a script you'd need to use every day.
<
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
Line 284:
end conspiracy
conspiracy(1000000)</
{{output}}
<
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%
Line 306:
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%"</
=={{header|C}}==
{{trans|C++}}
<
#include <stdbool.h>
#include <stdio.h>
Line 430:
return 0;
}</
{{out}}
<pre>1000000 primes, last prime considered: 15485863
Line 455:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace PrimeConspiracy {
Line 501:
}
}
}</
{{out}}
<pre>1 -> 1 count: 42853 frequency : 4.29%
Line 524:
=={{header|C++}}==
<
#include <iostream>
#include <cmath>
Line 578:
}
return 0 ;
}</
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.29 %
Line 603:
===Alternative using primesieve library===
{{libheader|Primesieve}}
<
#include <iomanip>
#include <iostream>
Line 633:
compute_transitions(100000000);
return 0;
}</
{{out}}
Line 681:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.range;
import std.stdio;
Line 733:
writefln(" frequency: %4.2f%%", transMap[trans] / 10_000.0);
}
}</
{{out}}
<pre>First 1,000,000 primes. Transitions prime % 10 -> next-prime % 10.
Line 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}}==
<
(lib 'math) ;; (in-primes n) stream
(decimals 4)
Line 777 ⟶ 817:
(vector+= trans (+ (* (% p1 m) m) (% p2 m)) 1))
(print-trans trans m N))
</syntaxhighlight>
{{out}}
<pre>
Line 800 ⟶ 840:
=={{header|Elixir}}==
<
def conspiracy(m) do
IO.puts "#{m} first primes. Transitions prime % 10 → next-prime % 10."
Line 826 ⟶ 866:
end
Prime.conspiracy(1000000)</
{{out}}
Line 854 ⟶ 894:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// 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>
{{out}}
<pre>
Line 883 ⟶ 923:
=={{header|Factor}}==
<
sequences sorting ;
IN: rosetta-code.prime-conspiracy
Line 902 ⟶ 942:
1,000,000 dup header transitions [ print-trans ] each ;
MAIN: main</
{{out}}
<pre>
Line 928 ⟶ 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. <
USE PRIMEBAG !Inherit this also.
INTEGER MBASE,P0,NHIC !Problem bounds.
Line 971 ⟶ 1,011:
END DO !On to the next successor digit.
END DO !On to the next base.
END !That was easy.</
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,192 ⟶ 1,232:
=={{header|FreeBASIC}}==
<
' updated 09-08-2018 Using bit-sieve of odd numbers
' compile with: fbc -s console
Line 1,249 ⟶ 1,289:
Sleep
End
</syntaxhighlight>
{{out}}
Output is shown side by side
Line 1,278 ⟶ 1,318:
Expect a run time of ~20−60 seconds to generate and process the full 100 million primes.
<
import (
Line 1,349 ⟶ 1,389:
}
fmt.Println()
}</
{{out}}
Line 1,418 ⟶ 1,458:
=={{header|Haskell}}==
Uses Primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html
<
import Text.Printf (printf)
import Data.Numbers.Primes (primes)
Line 1,430 ⟶ 1,470:
main :: IO ()
main = mapM_ line $ groups primes
where groups = tail . group . sort . (\n -> zip (0: n) n) . fmap (`mod` 10) . take 10000</
{{out}}
<pre>
Line 1,458 ⟶ 1,498:
This gets the job done:
<
1->1 42853 0.042853
1->3 77475 0.0774751
Line 1,477 ⟶ 1,517:
9->3 64371 0.0643711
9->7 58130 0.0581301
9->9 42843 0.042843</
Note that the [[Sieve of Eratosthenes]] task has some important implications for how often we will see the various transitions here.
Line 1,501 ⟶ 1,541:
Or, if you prefer the ratios formatted as percents, you could do this:
<
1->1 42853 4.2853%
1->3 77475 7.74751%
Line 1,520 ⟶ 1,560:
9->3 64371 6.43711%
9->7 58130 5.81301%
9->9 42843 4.2843%</
'''Extra Credit:'''
Line 1,530 ⟶ 1,570:
In other words:
<
combine=: ~.@[ ,. ' ',. ":@(%/&1 99999999)@(+//.)
/:~ combine&;/|: (~.;#/.~)@dgpairs@((+ i.)/)"1 (1e6*i.100),.1e6+99>i.100
Line 1,551 ⟶ 1,591:
9->3 6.37294e6 0.0637294
9->7 6.01274e6 0.0601274
9->9 4.62292e6 0.0462292</
=={{header|Java}}==
<
public static void main(String[] args) {
Line 1,598 ⟶ 1,638:
return composite;
}
}</
<pre>1 -> 1 : 4,285300
Line 1,619 ⟶ 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}}==
<
using DataStructures
Line 1,642 ⟶ 1,784:
for ((i, j), fr) in trans
@printf("%i → %i: freq. %3.4f%%\n", i, j, 100fr / tot)
end</
{{out}}
Line 1,667 ⟶ 1,809:
=={{header|Kotlin}}==
<
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning
Line 1,714 ⟶ 1,856:
println(" frequency: ${"%4.2f".format(transMap[trans]!! / 10000.0)}%")
}
}</
{{out}}
Line 1,742 ⟶ 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.
<
function isPrime (n)
if n <= 1 then return false end
Line 1,792 ⟶ 1,934:
end
end
end</
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.2853 %
Line 1,816 ⟶ 1,958:
=={{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>
Line 1,848 ⟶ 1,990:
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.
const N = 1_020_000_000
proc newSieve(): seq[bool] =
Line 1,895 ⟶ 2,032:
# Check if sieve was big enough.
if count < nprimes:
echo
quit(QuitFailure)
# Print result.
echo
for key in sorted
let count = counts[key]
let freq = count.toFloat * 100 / nprimes.toFloat
echo
echo ""
Line 1,909 ⟶ 2,046:
isPrime.countTransitions(1_000_000)
isPrime.countTransitions(100_000_000)
</syntaxhighlight>
{{out}}
<pre>
Line 1,978 ⟶ 2,115:
=={{header|PARI/GP}}==
<
conspiracy(maxx) = {
print("primes considered= ", maxx);
x = matrix(9, 9)
p = 2;
q = 2 % 10;
while (cnt <= maxx,
cnt += 1;
m = q;
p = nextprime(p + 1);
q = p % 10;
x[m, q] += 1
);
printf("2 to 3 count: %d freq %.6f %s\n", x[2, 3], 100. *x[2,3]/cnt, "%");
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);
}
conspiracy(1000000);
</syntaxhighlight>
{{Out}}
<
primes considered= 1000000
2 to 3 count: 1
1 to 1 count: 42853
1 to 3 count: 77475
1 to 5 count: 0
1 to 7 count: 79453
1 to 9 count: 50153
3 to 1 count: 58255
3 to 3 count: 39668
3 to 5 count: 1
3 to 7 count: 72828
3 to 9 count: 79358
5 to 1 count: 0
5 to 3 count: 0
5 to 5 count: 0
5 to 7 count: 1
5 to 9 count: 0
7 to 1 count: 64230
7 to 3 count: 68595
7 to 5 count: 0
7 to 7 count: 39604
7 to 9 count: 77586
9 to 1 count: 84596
9 to 3 count: 64371
9 to 5 count: 0
9 to 7 count: 58130
9 to 9 count: 42843
total transitions= 1000001
15485917
</syntaxhighlight>
=={{header|Pascal}}==
Line 2,038 ⟶ 2,187:
'''Extra credit:''' is included PrimeLimit = 2038074743-> 100'000'000 Primes
<
program primCons;
{$IFNDEF FPC}
Line 2,166 ⟶ 2,315:
OutputTransitions(CntTransitions);
end.
</syntaxhighlight>
{{Out}}
<pre>PrimCnt 10000 100000 1000000 10000000 100000000
Line 2,200 ⟶ 2,349:
{{libheader|ntheory}}
<
my $upto = 1_000_000;
Line 2,214 ⟶ 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;</
{{out}}
<pre>1000000 first primes. Transitions prime % 10 → next-prime % 10.
Line 2,260 ⟶ 2,409:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: #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>
<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>
<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>
<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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d->%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,297 ⟶ 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:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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->%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">
(load "pluser/sieve.l") # See the task "Sieve of Eratosthanes"
Line 2,349 ⟶ 2,583:
(T
(cons (car Tally) (bump-trans Trans (cdr Tally))))))
</syntaxhighlight>
{{Out}}
<pre>
Line 2,377 ⟶ 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.
<
% table of nth prime values (up to 100,000)
Line 2,437 ⟶ 2,671:
plus(M, N, M2),
remove_multiples(N, M2, L, R).
</syntaxhighlight>
{{Out}}
<pre>
Line 2,465 ⟶ 2,699:
=={{header|Python}}==
{{trans|D}}
<
if n < 2:
return False
Line 2,513 ⟶ 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)</
{{out}}
<pre>First 1,000,000 primes. Transitions prime % 10 > next-prime % 10.
Line 2,537 ⟶ 2,771:
=={{header|R}}==
<
suppressMessages(library(gmp))
Line 2,563 ⟶ 2,797:
cat(sprintf("%d",limit),"first primes. Transitions prime % 10 -> next-prime % 10\n")
invisible(sapply(1:99,getOutput))
</syntaxhighlight>
<pre>
1000000 first primes. Transitions prime % 10 -> next-prime % 10
Line 2,589 ⟶ 2,823:
=={{header|Racket}}==
<
(require math/number-theory)
Line 2,607 ⟶ 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))))</
{{out}}
Line 2,637 ⟶ 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"
my %conspiracy;
Line 2,650 ⟶ 2,884:
}
say "$_ \tfrequency: {($_.value/$upto*100).round(.01)} %" for %conspiracy.sort;</
{{out}}
<pre>1 → 1 count: 42853 frequency: 4.29 %
Line 2,674 ⟶ 2,908:
=={{header|REXX}}==
The first '''do''' loop is a modified ''Sieve of Eratosthenes'' (just for odd numbers).
<
Call time 'R'
Numeric Digits 12
w=length(n-1)
h=n*(2**max(4,(w%2+1)))
h=h*1.2
prime.=1
nn=1
Do j=3 By 2 while nn<n
If prime.j Then Do
Do m=j*j To h By j+j
End
End
End
Say 'Sieve of Eratosthenes finished' time('E') 'seconds'
Call time 'R'
frequency.=0
Say 'For' n 'primes used in this study:'
/*show hdr information about this run. */
r=2 /* the last digit of the very 1st prime (2) */
nn=1 /* the number of primes looked at */
cnt.=0
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= when using the default input:}}
<pre>Sieve of Eratosthenes finished 23.526000 seconds
For
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
digit
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
digit
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
digit
digit
digit
Frequency analysis: 5.640000 seconds
last digit Number of occurrences
1 249934
3 250110
5 1
7 250015
9 249940
1000001</pre>
=={{header|Ruby}}==
<
def prime_conspiracy(m)
Line 2,746 ⟶ 3,020:
end
prime_conspiracy(1_000_000)</
{{out}}
<pre>1000000 first primes. Transitions prime % 10 → next-prime % 10.
Line 2,771 ⟶ 3,045:
=={{header|Rust}}==
Execution time is about 13 seconds on my system (macOS 10.15.4, 3.2GHz Quad-Core Intel Core i5).
<
mod bit_array;
mod prime_sieve;
Line 2,818 ⟶ 3,092:
println!();
compute_transitions(100000000);
}</
<
use crate::bit_array;
Line 2,855 ⟶ 3,129:
!self.composite.get(n / 2 - 1)
}
}</
<
pub struct BitArray {
array: Vec<u32>,
Line 2,880 ⟶ 3,154:
}
}
}</
{{out}}
Line 2,930 ⟶ 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.
<
// primal = "0.2"
Line 2,963 ⟶ 3,237:
println!();
compute_transitions(100000000);
}</
=={{header|Scala}}==
===Imperative version (Ugly, side effects)===
Con: Has to unfair assume the one millionth prime.
<
import scala.collection.mutable
Line 3,020 ⟶ 3,294:
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</
===Functional version, memoizatized===
<
private val oddPrimes: Stream[Int] =
3 #:: Stream.from(5, 2)
Line 3,044 ⟶ 3,318:
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</
=={{header|Seed7}}==
Line 3,055 ⟶ 3,329:
Executing the [http://seed7.sourceforge.net/faq.htm#compile compiled] Seed7 program takes only 0.08 seconds.
<
include "float.s7i";
Line 3,104 ⟶ 3,378:
flt(count * 100)/flt(total) digits 2 lpad 4 <& " %");
end for;
end func;</
{{out}}
Line 3,131 ⟶ 3,405:
=={{header|Sidef}}==
{{trans|zkl}}
<
var upto = 1e6
Line 3,144 ⟶ 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)
}</
{{out}}
<pre>
Line 3,169 ⟶ 3,443:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 3,251 ⟶ 3,525:
Debug.Print K & " " & Right(" " & Dict(K), 6) & " " & Dict(K) / Nb * 100 & "%"
Next
End Sub</
{{out}}
<pre>1000000 primes, last prime considered: 15485867
Line 3,308 ⟶ 3,582:
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
Limited to the first 10 million primes in order to finish in a reasonable time (around
<
import "./math" for Int
import "./sort" for Sort
var reportTransitions = Fn.new { |transMap, num|
Line 3,349 ⟶ 3,623:
reportTransitions.call(transMap, n)
}
System.print("Took %(System.clock - start) seconds.")</
{{out}}
Line 3,393 ⟶ 3,667:
9 -> 3 count: 6,513 frequency: 6.51%
9 -> 7 count: 5,671 frequency: 5.67%
9 -> 9 count: 3,995 frequency:
First 1,000,000 primes. Transitions prime % 10 -> next-prime % 10.
Line 3,437 ⟶ 3,711:
9 -> 9 count: 446,032 frequency: 4.46%
Took
</pre>
Line 3,443 ⟶ 3,717:
{{trans|Raku}}
Using [[Extensible prime generator#zkl]].
<
sieve :=Import("sieve.zkl",False,False,False).postponed_sieve;
conspiracy:=Dictionary();
Line 3,453 ⟶ 3,727:
foreach key in (conspiracy.keys.sort()){ v:=conspiracy[key].toFloat();
println("%s%,6d\tfrequency: %2.2F%".fmt(key,v,v/CNT *100))
}</
{{out}}
<pre>
|