The sieve of Sundaram: Difference between revisions

From Rosetta Code
Content added Content deleted
(Realize in F#)
 
Line 40: Line 40:
The millionth Sudaram prime is 15485867
The millionth Sudaram prime is 15485867
</pre>
</pre>

=={{header|Nim}}==
<lang Nim>import strutils

const N = 8_000_000

type Mark {.pure.} = enum None, Mark1, Mark2

var mark: array[1..N, Mark]
for n in countup(4, N, 3): mark[n] = Mark1


var count = 0 # Count of primes.
var list100: seq[int] # First 100 primes.
var last = 0 # Millionth prime.
var step = 5 # Current step for marking.

for n in 1..N:
case mark[n]
of None:
# Add/count a new odd prime.
inc count
if count <= 100:
list100.add 2 * n + 1
elif count == 1_000_000:
last = 2 * n + 1
break
of Mark1:
# Mark new numbers using current step.
if n > 4:
for k in countup(n + step, N, step):
if mark[k] == None: mark[k] = Mark2
inc step, 2
of Mark2:
# Ignore this number.
discard


echo "First 100 Sundaram primes:"
for i, n in list100:
stdout.write ($n).align(3), if (i + 1) mod 10 == 0: '\n' else: ' '
echo()
if last == 0:
quit "Not enough values in sieve. Found only $#.".format(count), QuitFailure
echo "The millionth Sundaram prime is ", ($last).insertSep()</lang>

{{out}}
<pre>First 100 Sundaram primes:
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 103 107 109 113 127
131 137 139 149 151 157 163 167 173 179
181 191 193 197 199 211 223 227 229 233
239 241 251 257 263 269 271 277 281 283
293 307 311 313 317 331 337 347 349 353
359 367 373 379 383 389 397 401 409 419
421 431 433 439 443 449 457 461 463 467
479 487 491 499 503 509 521 523 541 547

The millionth Sundaram prime is 15_485_867</pre>

Revision as of 15:38, 8 August 2021

The sieve of Sundaram is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The sieve of Erastothenes: you've been there; done that; have the T-shirt. The sieve of Erastothenes was ancient history when Euclid was a schoolboy. You are ready for something less than 3000 years old. You are ready for The sieve of Sundaram.

Starting with the ordered set of +ve integers, mark every third starting at 4 (4;7;10...).

Step through the set and if the value is not marked output 2*n+1. So from 1 to 4 output 3 5 7.

4 is marked so skip for 5 and 6 output 11 and 13.

7 is marked, so no output but now also mark every fifth starting at 12 (12;17;22...)

as per to 10 and now mark every seventh starting at 17 (17;24;31....)

as per for every further third element (13;16;19...) mark every (9th;11th;13th;...) element.

The output will be the ordered set of odd primes.

Using your function find and output the first 100 and the millionth Sundaram prime.

The faithless amongst you may compare the results with those generated by The sieve of Erastothenes.

F#

<lang fsharp> // The sieve of Sudaram. Nigel Galloway: August 7th., 2021 let sPrimes()=

 let sSieve=System.Collections.Generic.Dictionary<int,(unit -> int) list>()
 let rec fN g=match g with h::t->(let n=h() in if sSieve.ContainsKey n then sSieve.[n]<-h::sSieve.[n] else sSieve.Add(n,[h])); fN t|_->()
 let     fI n=if sSieve.ContainsKey n then fN sSieve.[n]; sSieve.Remove n|>ignore; None else Some(2*n+1) 
 let     fG n g=let mutable n=n in (fun()->n<-n+g; n)
 let     fE n g=if not(sSieve.ContainsKey n) then sSieve.Add(n,[fG n g]) else sSieve.[n]<-(fG n g)::sSieve.[g] 
 let     fL    =let mutable n,g=4,3 in (fun()->n<-n+3; g<-g+2; fE (n+g) g; n)
 sSieve.Add(4,[fL]); Seq.initInfinite((+)1)|>Seq.choose fI

sPrimes()|>Seq.take 100|>Seq.iter(printf "%d "); printfn "" printfn "The millionth Sudaram prime is %d" (Seq.item 999999 (sPrimes())) </lang>

Output:
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 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547
The millionth Sudaram prime is 15485867

Nim

<lang Nim>import strutils

const N = 8_000_000

type Mark {.pure.} = enum None, Mark1, Mark2

var mark: array[1..N, Mark] for n in countup(4, N, 3): mark[n] = Mark1


var count = 0 # Count of primes. var list100: seq[int] # First 100 primes. var last = 0 # Millionth prime. var step = 5 # Current step for marking.

for n in 1..N:

 case mark[n]
 of None:
   # Add/count a new odd prime.
   inc count
   if count <= 100:
     list100.add 2 * n + 1
   elif count == 1_000_000:
     last = 2 * n + 1
     break
 of Mark1:
   # Mark new numbers using current step.
   if n > 4:
     for k in countup(n + step, N, step):
       if mark[k] == None: mark[k] = Mark2
     inc step, 2
 of Mark2:
   # Ignore this number.
   discard


echo "First 100 Sundaram primes:" for i, n in list100:

 stdout.write ($n).align(3), if (i + 1) mod 10 == 0: '\n' else: ' '

echo() if last == 0:

 quit "Not enough values in sieve. Found only $#.".format(count), QuitFailure

echo "The millionth Sundaram prime is ", ($last).insertSep()</lang>

Output:
First 100 Sundaram primes:
  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 103 107 109 113 127
131 137 139 149 151 157 163 167 173 179
181 191 193 197 199 211 223 227 229 233
239 241 251 257 263 269 271 277 281 283
293 307 311 313 317 331 337 347 349 353
359 367 373 379 383 389 397 401 409 419
421 431 433 439 443 449 457 461 463 467
479 487 491 499 503 509 521 523 541 547

The millionth Sundaram prime is 15_485_867