Prime conspiracy: Difference between revisions

→‎{{header|REXX}}: rewritten to be readable
m (→‎{{header|Wren}}: Minor tidy.)
(→‎{{header|REXX}}: rewritten to be readable)
 
(2 intermediate revisions by 2 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 2,076 ⟶ 2,116:
 
<syntaxhighlight 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);
}
 
conspiracy(1000000);
</syntaxhighlight>
{{Out}}
<syntaxhighlight 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>
 
Line 2,856 ⟶ 2,908:
=={{header|REXX}}==
The first &nbsp; '''do''' &nbsp; loop is a modified ''Sieve of Eratosthenes'' &nbsp; &nbsp; (just for odd numbers).
<syntaxhighlight 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. */</syntaxhighlight>
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}}==
2,294

edits