Prime conspiracy: Difference between revisions
Content added Content deleted
(Added Easylang) |
Walterpachl (talk | contribs) (→{{header|REXX}}: rewritten to be readable) |
||
Line 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)
#= #+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*/▼
#= # + 1; parse var i '' -1 x /*bump prime counter; get its last dig.*/▼
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)*/▼
right(!.d.f, w)", frequency of:" right(format(!.d.f / N*100, , 4)'%.', 10)▼
end /*f*/▼
{{out|output|text= when using the default input:}}▼
For 1000000 primes used in this study:▼
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>
▲{{out|output|text= when using the default input:}}
<pre>Sieve of Eratosthenes finished 23.526000 seconds
i=15485869 largest prime
h=19200000.0
digit 1
digit 1
digit 1
digit 1
digit 2
digit 3
digit 3
digit 3
digit 3
digit 3
digit 5
digit 7
digit 7
digit 7
digit 7
digit 9
digit 9
digit 9
digit 9
Frequency analysis: 5.640000 seconds
last digit Number of occurrences
3 250110
5 1
7 250015
9 249940
1000001</pre>
=={{header|Ruby}}==
|