Prime conspiracy: Difference between revisions
→{{header|REXX}}: rewritten to be readable
m (Separated Rust code into modules) |
Walterpachl (talk | contribs) (→{{header|REXX}}: rewritten to be readable) |
||
(22 intermediate revisions by 14 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).
<
OP SET = ( INT n, REF[]BITS b )REF[]BITS:
BEGIN
Line 140 ⟶ 195:
FI
OD
OD</
{{out}}
<pre>
Line 164 ⟶ 219:
9->9 42843 4.28
</pre>
=={{header|AppleScript}}==
<big><code> i </code></big>'s "preference" for any <big><code> j </code></big> is a percentage of the transitions from <big><code> 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++}}
<
#include <stdbool.h>
#include <stdio.h>
Line 287 ⟶ 430:
return 0;
}</
{{out}}
<pre>1000000 primes, last prime considered: 15485863
Line 312 ⟶ 455:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace PrimeConspiracy {
Line 358 ⟶ 501:
}
}
}</
{{out}}
<pre>1 -> 1 count: 42853 frequency : 4.29%
Line 381 ⟶ 524:
=={{header|C++}}==
<
#include <iostream>
#include <cmath>
Line 435 ⟶ 578:
}
return 0 ;
}</
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.29 %
Line 456 ⟶ 599:
9 -> 7 count: 58130 frequency: 5.81 %
9 -> 9 count: 42843 frequency: 4.28 %
</pre>
===Alternative using primesieve library===
{{libheader|Primesieve}}
<syntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <primesieve.hpp>
void compute_transitions(uint64_t limit) {
primesieve::iterator it;
std::map<std::pair<uint64_t, uint64_t>, uint64_t> transitions;
for (uint64_t count = 0, prev = 0; count < limit; ++count) {
uint64_t prime = it.next_prime();
uint64_t digit = prime % 10;
if (prev != 0)
++transitions[std::make_pair(prev, digit)];
prev = digit;
}
std::cout << "First " << limit << " prime numbers:\n";
for (auto&& pair : transitions) {
double freq = (100.0 * pair.second)/limit;
std::cout << pair.first.first << " -> " << pair.first.second
<< ": count = " << std::setw(7) << pair.second
<< ", frequency = " << std::setprecision(2)
<< std::fixed << freq << " %\n";
}
}
int main(int argc, const char * argv[]) {
compute_transitions(1000000);
compute_transitions(100000000);
return 0;
}</syntaxhighlight>
{{out}}
<pre>
First 1000000 prime numbers:
1 -> 1: count = 42853, frequency = 4.29 %
1 -> 3: count = 77475, frequency = 7.75 %
1 -> 7: count = 79453, frequency = 7.95 %
1 -> 9: count = 50153, frequency = 5.02 %
2 -> 3: count = 1, frequency = 0.00 %
3 -> 1: count = 58255, frequency = 5.83 %
3 -> 3: count = 39668, frequency = 3.97 %
3 -> 5: count = 1, frequency = 0.00 %
3 -> 7: count = 72827, frequency = 7.28 %
3 -> 9: count = 79358, frequency = 7.94 %
5 -> 7: count = 1, frequency = 0.00 %
7 -> 1: count = 64230, frequency = 6.42 %
7 -> 3: count = 68595, frequency = 6.86 %
7 -> 7: count = 39603, frequency = 3.96 %
7 -> 9: count = 77586, frequency = 7.76 %
9 -> 1: count = 84596, frequency = 8.46 %
9 -> 3: count = 64371, frequency = 6.44 %
9 -> 7: count = 58130, frequency = 5.81 %
9 -> 9: count = 42843, frequency = 4.28 %
First 100000000 prime numbers:
1 -> 1: count = 4623041, frequency = 4.62 %
1 -> 3: count = 7429438, frequency = 7.43 %
1 -> 7: count = 7504612, frequency = 7.50 %
1 -> 9: count = 5442344, frequency = 5.44 %
2 -> 3: count = 1, frequency = 0.00 %
3 -> 1: count = 6010981, frequency = 6.01 %
3 -> 3: count = 4442561, frequency = 4.44 %
3 -> 5: count = 1, frequency = 0.00 %
3 -> 7: count = 7043695, frequency = 7.04 %
3 -> 9: count = 7502896, frequency = 7.50 %
5 -> 7: count = 1, frequency = 0.00 %
7 -> 1: count = 6373982, frequency = 6.37 %
7 -> 3: count = 6755195, frequency = 6.76 %
7 -> 7: count = 4439355, frequency = 4.44 %
7 -> 9: count = 7431870, frequency = 7.43 %
9 -> 1: count = 7991431, frequency = 7.99 %
9 -> 3: count = 6372940, frequency = 6.37 %
9 -> 7: count = 6012739, frequency = 6.01 %
9 -> 9: count = 4622916, frequency = 4.62 %
</pre>
=={{header|D}}==
{{trans|Kotlin}}
<
import std.range;
import std.stdio;
Line 512 ⟶ 733:
writefln(" frequency: %4.2f%%", transMap[trans] / 10_000.0);
}
}</
{{out}}
<pre>First 1,000,000 primes. Transitions prime % 10 -> next-prime % 10.
Line 534 ⟶ 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 556 ⟶ 817:
(vector+= trans (+ (* (% p1 m) m) (% p2 m)) 1))
(print-trans trans m N))
</syntaxhighlight>
{{out}}
<pre>
Line 579 ⟶ 840:
=={{header|Elixir}}==
<
def conspiracy(m) do
IO.puts "#{m} first primes. Transitions prime % 10 → next-prime % 10."
Line 605 ⟶ 866:
end
Prime.conspiracy(1000000)</
{{out}}
Line 633 ⟶ 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 662 ⟶ 923:
=={{header|Factor}}==
<
sequences sorting ;
IN: rosetta-code.prime-conspiracy
Line 681 ⟶ 942:
1,000,000 dup header transitions [ print-trans ] each ;
MAIN: main</
{{out}}
<pre>
Line 707 ⟶ 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 750 ⟶ 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 971 ⟶ 1,232:
=={{header|FreeBASIC}}==
<
' updated 09-08-2018 Using bit-sieve of odd numbers
' compile with: fbc -s console
Line 1,028 ⟶ 1,289:
Sleep
End
</syntaxhighlight>
{{out}}
Output is shown side by side
Line 1,057 ⟶ 1,318:
Expect a run time of ~20−60 seconds to generate and process the full 100 million primes.
<
import (
Line 1,128 ⟶ 1,389:
}
fmt.Println()
}</
{{out}}
Line 1,197 ⟶ 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,209 ⟶ 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,237 ⟶ 1,498:
This gets the job done:
<
1->1 42853 0.042853
1->3 77475 0.0774751
Line 1,256 ⟶ 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,280 ⟶ 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,299 ⟶ 1,560:
9->3 64371 6.43711%
9->7 58130 5.81301%
9->9 42843 4.2843%</
'''Extra Credit:'''
Line 1,309 ⟶ 1,570:
In other words:
<
combine=: ~.@[ ,. ' ',. ":@(%/&1 99999999)@(+//.)
/:~ combine&;/|: (~.;#/.~)@dgpairs@((+ i.)/)"1 (1e6*i.100),.1e6+99>i.100
Line 1,330 ⟶ 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,377 ⟶ 1,638:
return composite;
}
}</
<pre>1 -> 1 : 4,285300
Line 1,398 ⟶ 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,421 ⟶ 1,784:
for ((i, j), fr) in trans
@printf("%i → %i: freq. %3.4f%%\n", i, j, 100fr / tot)
end</
{{out}}
Line 1,446 ⟶ 1,809:
=={{header|Kotlin}}==
<
// compiled with flag -Xcoroutines=enable to suppress 'experimental' warning
Line 1,493 ⟶ 1,856:
println(" frequency: ${"%4.2f".format(transMap[trans]!! / 10000.0)}%")
}
}</
{{out}}
Line 1,521 ⟶ 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,571 ⟶ 1,934:
end
end
end</
{{out}}
<pre>1 -> 1 count: 42853 frequency: 4.2853 %
Line 1,592 ⟶ 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.
import std/[algorithm, math, sequtils, strformat, tables]
const N = 1_020_000_000 # Size of sieve of Eratosthenes.
proc newSieve(): seq[bool] =
## Create a sieve with only odd values.
## Index "i" in sieve represents value "n = 2 * i + 3".
result.setLen(N)
for item in result.mitems: item = true
# Apply sieve.
var i = 0
const Limit = sqrt(2 * N.toFloat + 3).int
while true:
let n = 2 * i + 3
if n > Limit:
break
if result[i]:
# Found prime, so eliminate multiples.
for k in countup((n * n - 3) div 2, N - 1, n):
result[k] = false
inc i
var isPrime = newSieve()
proc countTransitions(isPrime: seq[bool]; nprimes: int) =
## Build the transition count table and print it.
var counts = [(2, 3)].toCountTable() # Count of transitions.
var d1 = 3 # Last digit of first prime in transition.
var count = 2 # Count of primes (starting with 2 and 3).
for i in 1..isPrime.high:
if isPrime[i]:
inc count
let d2 = (2 * i + 3) mod 10 # Last digit of second prime in transition.
counts.inc((d1, d2))
if count == nprimes: break
d1 = d2
# Check if sieve was big enough.
if count < nprimes:
echo &"Found only {count} primes; expected {nprimes} primes. Increase value of N."
quit(QuitFailure)
# Print result.
echo &"{nprimes} first primes. Transitions prime (mod 10) → next-prime (mod 10)."
for key in sorted(counts.keys.toSeq):
let count = counts[key]
let freq = count.toFloat * 100 / nprimes.toFloat
echo &"{key[0]} → {key[1]} Count: {count:7d} Frequency: {freq:4.2f}%"
echo ""
isPrime.countTransitions(10_000)
isPrime.countTransitions(1_000_000)
isPrime.countTransitions(100_000_000)
</syntaxhighlight>
{{out}}
<pre>
10000 first primes. Transitions prime (mod 10) → next-prime (mod 10).
1 → 1 Count: 365 Frequency: 3.65%
1 → 3 Count: 833 Frequency: 8.33%
1 → 7 Count: 889 Frequency: 8.89%
1 → 9 Count: 397 Frequency: 3.97%
2 → 3 Count: 1 Frequency: 0.01%
3 → 1 Count: 529 Frequency: 5.29%
3 → 3 Count: 324 Frequency: 3.24%
3 → 5 Count: 1 Frequency: 0.01%
3 → 7 Count: 754 Frequency: 7.54%
3 → 9 Count: 907 Frequency: 9.07%
5 → 7 Count: 1 Frequency: 0.01%
7 → 1 Count: 655 Frequency: 6.55%
7 → 3 Count: 722 Frequency: 7.22%
7 → 7 Count: 323 Frequency: 3.23%
7 → 9 Count: 808 Frequency: 8.08%
9 → 1 Count: 935 Frequency: 9.35%
9 → 3 Count: 635 Frequency: 6.35%
9 → 7 Count: 541 Frequency: 5.41%
9 → 9 Count: 379 Frequency: 3.79%
1000000 first primes. Transitions prime (mod 10) → next-prime (mod 10).
1 → 1 Count: 42853 Frequency: 4.29%
1 → 3 Count: 77475 Frequency: 7.75%
1 → 7 Count: 79453 Frequency: 7.95%
1 → 9 Count: 50153 Frequency: 5.02%
2 → 3 Count: 1 Frequency: 0.00%
3 → 1 Count: 58255 Frequency: 5.83%
3 → 3 Count: 39668 Frequency: 3.97%
3 → 5 Count: 1 Frequency: 0.00%
3 → 7 Count: 72827 Frequency: 7.28%
3 → 9 Count: 79358 Frequency: 7.94%
5 → 7 Count: 1 Frequency: 0.00%
7 → 1 Count: 64230 Frequency: 6.42%
7 → 3 Count: 68595 Frequency: 6.86%
7 → 7 Count: 39603 Frequency: 3.96%
7 → 9 Count: 77586 Frequency: 7.76%
9 → 1 Count: 84596 Frequency: 8.46%
9 → 3 Count: 64371 Frequency: 6.44%
9 → 7 Count: 58130 Frequency: 5.81%
9 → 9 Count: 42843 Frequency: 4.28%
100000000 first primes. Transitions prime (mod 10) → next-prime (mod 10).
1 → 1 Count: 4623041 Frequency: 4.62%
1 → 3 Count: 7429438 Frequency: 7.43%
1 → 7 Count: 7504612 Frequency: 7.50%
1 → 9 Count: 5442344 Frequency: 5.44%
2 → 3 Count: 1 Frequency: 0.00%
3 → 1 Count: 6010981 Frequency: 6.01%
3 → 3 Count: 4442561 Frequency: 4.44%
3 → 5 Count: 1 Frequency: 0.00%
3 → 7 Count: 7043695 Frequency: 7.04%
3 → 9 Count: 7502896 Frequency: 7.50%
5 → 7 Count: 1 Frequency: 0.00%
7 → 1 Count: 6373982 Frequency: 6.37%
7 → 3 Count: 6755195 Frequency: 6.76%
7 → 7 Count: 4439355 Frequency: 4.44%
7 → 9 Count: 7431870 Frequency: 7.43%
9 → 1 Count: 7991431 Frequency: 7.99%
9 → 3 Count: 6372940 Frequency: 6.37%
9 → 7 Count: 6012739 Frequency: 6.01%
9 → 9 Count: 4622916 Frequency: 4.62%
</pre>
=={{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 1,655 ⟶ 2,187:
'''Extra credit:''' is included PrimeLimit = 2038074743-> 100'000'000 Primes
<
program primCons;
{$IFNDEF FPC}
Line 1,783 ⟶ 2,315:
OutputTransitions(CntTransitions);
end.
</syntaxhighlight>
{{Out}}
<pre>PrimCnt 10000 100000 1000000 10000000 100000000
Line 1,817 ⟶ 2,349:
{{libheader|ntheory}}
<
my $upto = 1_000_000;
Line 1,831 ⟶ 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 1,877 ⟶ 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 1,914 ⟶ 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 1,966 ⟶ 2,583:
(T
(cons (car Tally) (bump-trans Trans (cdr Tally))))))
</syntaxhighlight>
{{Out}}
<pre>
Line 1,994 ⟶ 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,054 ⟶ 2,671:
plus(M, N, M2),
remove_multiples(N, M2, L, R).
</syntaxhighlight>
{{Out}}
<pre>
Line 2,082 ⟶ 2,699:
=={{header|Python}}==
{{trans|D}}
<
if n < 2:
return False
Line 2,130 ⟶ 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,154 ⟶ 2,771:
=={{header|R}}==
<
suppressMessages(library(gmp))
Line 2,180 ⟶ 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,206 ⟶ 2,823:
=={{header|Racket}}==
<
(require math/number-theory)
Line 2,224 ⟶ 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,254 ⟶ 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,267 ⟶ 2,884:
}
say "$_ \tfrequency: {($_.value/$upto*100).round(.01)} %" for %conspiracy.sort;</
{{out}}
<pre>1 → 1 count: 42853 frequency: 4.29 %
Line 2,291 ⟶ 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,363 ⟶ 3,020:
end
prime_conspiracy(1_000_000)</
{{out}}
<pre>1000000 first primes. Transitions prime % 10 → next-prime % 10.
Line 2,388 ⟶ 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,435 ⟶ 3,092:
println!();
compute_transitions(100000000);
}</
<
use crate::bit_array;
Line 2,472 ⟶ 3,129:
!self.composite.get(n / 2 - 1)
}
}</
<
pub struct BitArray {
array: Vec<u32>,
Line 2,497 ⟶ 3,154:
}
}
}</
{{out}}
Line 2,547 ⟶ 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"
fn compute_transitions(limit
use std::collections::BTreeMap;
let mut transitions = BTreeMap::new();
Line 2,567 ⟶ 3,224:
}
println!("First {} prime numbers:", limit);
for ((from, to), c) in &
let freq = 100.0 * (*c as f32) / (limit as f32);
println!(
"{} -> {}: count = {:7}, frequency = {:.2} %",
from, to, c, freq
);
}
}
Line 2,577 ⟶ 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 2,634 ⟶ 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 2,658 ⟶ 3,318:
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</
=={{header|Seed7}}==
Line 2,669 ⟶ 3,329:
Executing the [http://seed7.sourceforge.net/faq.htm#compile compiled] Seed7 program takes only 0.08 seconds.
<
include "float.s7i";
Line 2,718 ⟶ 3,378:
flt(count * 100)/flt(total) digits 2 lpad 4 <& " %");
end for;
end func;</
{{out}}
Line 2,745 ⟶ 3,405:
=={{header|Sidef}}==
{{trans|zkl}}
<
var upto = 1e6
Line 2,758 ⟶ 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 2,783 ⟶ 3,443:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 2,865 ⟶ 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 2,922 ⟶ 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 2,963 ⟶ 3,623:
reportTransitions.call(transMap, n)
}
System.print("Took %(System.clock - start) seconds.")</
{{out}}
Line 3,007 ⟶ 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,051 ⟶ 3,711:
9 -> 9 count: 446,032 frequency: 4.46%
Took
</pre>
Line 3,057 ⟶ 3,717:
{{trans|Raku}}
Using [[Extensible prime generator#zkl]].
<
sieve :=Import("sieve.zkl",False,False,False).postponed_sieve;
conspiracy:=Dictionary();
Line 3,067 ⟶ 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>
|