Prime conspiracy: Difference between revisions
Content added Content deleted
(Added Easylang) |
Walterpachl (talk | contribs) (→{{header|REXX}}: rewritten to be readable) |
||
Line 2,908: | Line 2,908: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The first '''do''' loop is a modified ''Sieve of Eratosthenes'' (just for odd numbers). |
The first '''do''' loop is a modified ''Sieve of Eratosthenes'' (just for odd numbers). |
||
<syntaxhighlight lang="rexx">/*REXX pgm shows a table of |
<syntaxhighlight lang="rexx">/*REXX pgm shows a table of which last digit follows the previous last digit */ |
||
/* for N primes */ |
|||
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.*/ |
|||
Parse Arg n . /* N: the number of primes to be looked at */ |
|||
If n==''|n=="," Then /* Not specified? */ |
|||
n=1000000 /* Use the default */ |
|||
w=length(n-1) /* W: width used for formatting o*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
do i=3 by 2; if @.i=='' then iterate /*This number composite? Then ignore it*/ |
|||
⚫ | |||
!.r.x= !.r.x +1; r= x /*bump the last digit counter for prev.*/ |
|||
if #==Np then leave /*Done? Then leave this DO loop. */ |
|||
end /*i*/ /* [↑] examine almost all odd numbers.*/ |
|||
⚫ | |||
⚫ | |||
do f=1 for 9; if !.d.f==0 then iterate /*don't show if the count is zero. */ |
|||
say 'digit ' d "──►" f ' has a count of: ', |
|||
⚫ | |||
⚫ | |||
end /*d*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
|||
⚫ | |||
<pre> |
|||
⚫ | |||
h=n*(2**max(4,(w%2+1))) /* used as a rough limit for the sieve */ |
|||
⚫ | |||
prime.=1 /* assume all numbers are prime */ |
|||
⚫ | |||
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 /* initialize all the frequency counts */ |
|||
Say 'For' n 'primes used in this study:' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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 */ |
|||
⚫ | |||
frequency.r.x=frequency.r.x+1 /* bump the frequency counter */ |
|||
⚫ | |||
End |
|||
End |
|||
Say 'i='i 'largest prime' |
|||
Say 'h='h |
|||
⚫ | |||
Do d=1 For 9 |
|||
If d//2|d==2 Then |
|||
⚫ | |||
Do f=1 For 9 |
|||
If frequency.d.f>0 Then |
|||
Say 'digit ' d '-->' f ' has a count of: ' right(frequency.d.f,w)||, |
|||
⚫ | |||
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> |
|||
⚫ | |||
<pre>Sieve of Eratosthenes finished 23.526000 seconds |
|||
⚫ | |||
i=15485869 largest prime |
|||
h=19200000.0 |
|||
digit 1 |
digit 1 --> 1 has a count of: 42853, frequency of: 4.2853%. |
||
digit 1 |
digit 1 --> 3 has a count of: 77475, frequency of: 7.7475%. |
||
digit 1 |
digit 1 --> 7 has a count of: 79453, frequency of: 7.9453%. |
||
digit 1 |
digit 1 --> 9 has a count of: 50153, frequency of: 5.0153%. |
||
digit 2 |
digit 2 --> 3 has a count of: 1, frequency of: 0.0001%. |
||
digit 3 |
digit 3 --> 1 has a count of: 58255, frequency of: 5.8255%. |
||
digit 3 |
digit 3 --> 3 has a count of: 39668, frequency of: 3.9668%. |
||
digit 3 |
digit 3 --> 5 has a count of: 1, frequency of: 0.0001%. |
||
digit 3 |
digit 3 --> 7 has a count of: 72828, frequency of: 7.2828%. |
||
digit 3 |
digit 3 --> 9 has a count of: 79358, frequency of: 7.9358%. |
||
digit 5 |
digit 5 --> 7 has a count of: 1, frequency of: 0.0001%. |
||
digit 7 |
digit 7 --> 1 has a count of: 64230, frequency of: 6.4230%. |
||
digit 7 |
digit 7 --> 3 has a count of: 68595, frequency of: 6.8595%. |
||
digit 7 |
digit 7 --> 7 has a count of: 39603, frequency of: 3.9603%. |
||
digit 7 |
digit 7 --> 9 has a count of: 77586, frequency of: 7.7586%. |
||
digit 9 |
digit 9 --> 1 has a count of: 84596, frequency of: 8.4596%. |
||
digit 9 |
digit 9 --> 3 has a count of: 64371, frequency of: 6.4371%. |
||
digit 9 |
digit 9 --> 7 has a count of: 58130, frequency of: 5.8130%. |
||
digit 9 |
digit 9 --> 9 has a count of: 42843, frequency of: 4.2843%. |
||
Frequency analysis: 5.640000 seconds |
|||
</pre> |
|||
last digit Number of occurrences |
|||
⚫ | |||
⚫ | |||
3 250110 |
|||
5 1 |
|||
7 250015 |
|||
9 249940 |
|||
1000001</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |