Prime conspiracy: Difference between revisions
→{{header|REXX}}: rewritten to be readable
m (→{{header|jq}}: simplify) |
Walterpachl (talk | contribs) (→{{header|REXX}}: rewritten to be readable) |
||
(4 intermediate revisions by 4 users not shown) | |||
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}}==
Line 1,950 ⟶ 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,997 ⟶ 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 2,081 ⟶ 2,116:
<syntaxhighlight lang="parigp">
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}}
<syntaxhighlight lang="parigp">
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>
Line 2,861 ⟶ 2,908:
=={{header|REXX}}==
The first '''do''' loop is a modified ''Sieve of Eratosthenes'' (just for odd numbers).
<syntaxhighlight lang="rexx">/*REXX pgm shows a table of
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}}==
Line 3,495 ⟶ 3,582:
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
Limited to the first 10 million primes in order to finish in a reasonable time (around
<syntaxhighlight lang="
import "./math" for Int
import "./sort" for Sort
var reportTransitions = Fn.new { |transMap, num|
Line 3,580 ⟶ 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,624 ⟶ 3,711:
9 -> 9 count: 446,032 frequency: 4.46%
Took
</pre>
|