Skip to content

Instantly share code, notes, and snippets.

@def-

def-/primes.nim

Created Mar 31, 2017
Embed
What would you like to do?
import os, times, future, math, sequtils, strutils
proc get_primes7(n: int32): seq[int32] =
if n < 2:
return @[]
result = @[2'i32]
if n == 2:
return
var s = newSeq[int32]()
for x in countup(3, n + 1, 2):
s.add(x)
let
mroot = int32(sqrt(n.float))
half = int32(len(s))
var
i = 0'i32
m = 3'i32
while m <= mroot:
if s[i] != 0:
var j = (m * m - 3) div 2 # int div
s[j] = 0
while j < half:
s[j] = 0
j += m
inc(i)
m = 2 * i + 3
for x in s:
if x != 0:
result.add(x)
let start_time = getTime()
let period_time = getEnv("RUN_TIME").parseInt()
while int(getTime() - start_time) < period_time:
let res = get_primes7(10000000)
echo("Found $1 prime numbers." % $res.len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.