Sieve of Eratosthenes: Difference between revisions

Content deleted Content added
Rdm (talk | contribs)
Rdm (talk | contribs)
Line 9,640:
<lang J> sieve 100
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</lang>
 
To see into how this works, we can change the definition:
 
<lang J>sieve=: {{
r=. 0#t=. y# j=.1
while. y>j=.j+1 do.
if. j{t do.
echo j;(y$j{.1);t=. t > y$j{.1
r=. r, j
end.
end.
}}</lang>
 
And go:<lang J> sieve 10
┌─┬───────────────────┬───────────────────┐
│2│1 0 1 0 1 0 1 0 1 0│0 1 0 1 0 1 0 1 0 1│
└─┴───────────────────┴───────────────────┘
┌─┬───────────────────┬───────────────────┐
│3│1 0 0 1 0 0 1 0 0 1│0 1 0 0 0 1 0 1 0 0│
└─┴───────────────────┴───────────────────┘
┌─┬───────────────────┬───────────────────┐
│5│1 0 0 0 0 1 0 0 0 0│0 1 0 0 0 0 0 1 0 0│
└─┴───────────────────┴───────────────────┘
┌─┬───────────────────┬───────────────────┐
│7│1 0 0 0 0 0 0 1 0 0│0 1 0 0 0 0 0 0 0 0│
└─┴───────────────────┴───────────────────┘
2 3 5 7</lang>
 
=={{header|Janet}}==