Prime conspiracy: Difference between revisions

Content added Content deleted
(Added Easylang)
(→‎{{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 what last digit follows the previous last digit for N primes*/
<syntaxhighlight lang="rexx">/*REXX pgm shows a table of which last digit follows the previous last digit */
parse arg N . /*N: the number of primes to be genned*/
/* 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.*/
H= N* (2**max(4, (w%2+1) ) ) /*used as a rough limit for the sieve. */
Parse Arg n . /* N: the number of primes to be looked at */
@.= . /*assume all numbers are prime (so far)*/
If n==''|n=="," Then /* Not specified? */
#= 1 /*primes found so far {assume prime 2}.*/
n=1000000 /* Use the default */
do j=3 by 2; if @.j=='' then iterate /*Is composite? Then skip this number.*/
w=length(n-1) /* W: width used for formatting o*/
#= #+1 /*bump the prime number counter. */
do m=j*j to H by j+j; @.m= /*strike odd multiples as composite. */
end /*m*/
if #==Np then leave /*Enough primes? Then done with gen. */
end /*j*/ /* [↑] gen using Eratosthenes' sieve. */
!.= 0 /*initialize all the frequency counters*/
say 'For ' N " primes used in this study:" /*show hdr information about this run. */
r= 2 /*the last digit of the very 1st prime.*/
#= 1 /*the number of primes looked at so far*/
do i=3 by 2; if @.i=='' then iterate /*This number composite? Then ignore it*/
#= # + 1; parse var i '' -1 x /*bump prime counter; get its last dig.*/
!.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.*/
say /* [↓] display the results to the term*/
do d=1 for 9; if d//2 | d==2 then say /*display a blank line (if appropriate)*/
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: ',
right(!.d.f, w)", frequency of:" right(format(!.d.f / N*100, , 4)'%.', 10)
end /*f*/
end /*d*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
For 1000000 primes used in this study:


h=n*(2**max(4,(w%2+1))) /* used as a rough limit for the sieve */
h=h*1.2 /* make sure it is large enough */
prime.=1 /* assume all numbers are prime */
nn=1 /* primes found so far {2 is the firt prime)*/
Do j=3 By 2 while nn<n
If prime.j Then Do
nn=nn+1 /* bump the prime number counter. */
Do m=j*j To h By j+j
prime.m=0 /* strike odd multiples as composite */
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:'
/*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=&nbsp; when using the default input:}}
<pre>Sieve of Eratosthenes finished 23.526000 seconds
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 --> 1 has a count of: 42853, frequency of: 4.2853%.
digit 1 ──► 3 has a count of: 77475, frequency of: 7.7475%.
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 --> 7 has a count of: 79453, frequency of: 7.9453%.
digit 1 ──► 9 has a count of: 50153, frequency of: 5.0153%.
digit 1 --> 9 has a count of: 50153, frequency of: 5.0153%.


digit 2 ──► 3 has a count of: 1, frequency of: 0.0001%.
digit 2 --> 3 has a count of: 1, frequency of: 0.0001%.


digit 3 ──► 1 has a count of: 58255, frequency of: 5.8255%.
digit 3 --> 1 has a count of: 58255, frequency of: 5.8255%.
digit 3 ──► 3 has a count of: 39668, frequency of: 3.9668%.
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 --> 5 has a count of: 1, frequency of: 0.0001%.
digit 3 ──► 7 has a count of: 72828, frequency of: 7.2828%.
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 3 --> 9 has a count of: 79358, frequency of: 7.9358%.


digit 5 ──► 7 has a count of: 1, frequency of: 0.0001%.
digit 5 --> 7 has a count of: 1, frequency of: 0.0001%.


digit 7 ──► 1 has a count of: 64230, frequency of: 6.4230%.
digit 7 --> 1 has a count of: 64230, frequency of: 6.4230%.
digit 7 ──► 3 has a count of: 68595, frequency of: 6.8595%.
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 --> 7 has a count of: 39603, frequency of: 3.9603%.
digit 7 ──► 9 has a count of: 77586, frequency of: 7.7586%.
digit 7 --> 9 has a count of: 77586, frequency of: 7.7586%.


digit 9 ──► 1 has a count of: 84596, frequency of: 8.4596%.
digit 9 --> 1 has a count of: 84596, frequency of: 8.4596%.
digit 9 ──► 3 has a count of: 64371, frequency of: 6.4371%.
digit 9 --> 3 has a count of: 64371, frequency of: 6.4371%.
digit 9 ──► 7 has a count of: 58130, frequency of: 5.8130%.
digit 9 --> 7 has a count of: 58130, frequency of: 5.8130%.
digit 9 ──► 9 has a count of: 42843, frequency of: 4.2843%.
digit 9 --> 9 has a count of: 42843, frequency of: 4.2843%.
Frequency analysis: 5.640000 seconds
</pre>
last digit Number of occurrences
1 249934
2 1
3 250110
5 1
7 250015
9 249940
1000001</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==