Sequence of primes by trial division: Difference between revisions

→‎{{header|REXX}}: changed comments and some program logic, made prime list (internal) complete with even prime, added output.
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(→‎{{header|REXX}}: changed comments and some program logic, made prime list (internal) complete with even prime, added output.)
Line 63:
This is an open-ended approach and it's a simple implementation and could be optimized greatly.
<br>The method used is to divided all odd numbers by all previous odd primes up to and including the &nbsp; <big>'''√'''</big> &nbsp; of the odd number.
<lang rexx>/*REXX programpgm lists a sequence of primes by testing primality by trail divisiondiv.*/
parse arg n . /*let user choose how many, maybe*/
if n=='' then n=26 /*if not, then assume the default*/
if n<1 then exit /*handle special case of 0 primes*/
@.1=2; say right(2@.1, 9) /*display 2 as a special case. */
#=1 /*# is the number of primes found*/
/* [↑] N default lists up to 101*/
do j= 3 by 2 while #<n /*start with the first odd prime.*/
do k=2 to # while @.k*k*2<=j /*divide J with all primes ≤ √J.*/
if j//@.k==0 then iterate j /*÷ by a previous prime? ^prime.*/
end /*j*/ /* [↑] only divide up to √J. */
#=#+1 /*bump the prime number counter. */
@.#=j /*define this prime in the array.*/
say right(j, 9) /*display this prime to the term.*/
end /*j*/ /* [↑] only display N primes. */
/* [↓] display the prime count. */
say # ' primes found.' /*stick a fork in it, we're done.*/</lang><br>
'''output''' using the default input of: &nbsp; <tt> 26 </tt>
<pre>
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101
26 primes found.
</pre>
 
=={{header|Scala}}==