Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import times
- import future
- import math
- import sequtils
- import strutils
- proc get_primes7(n: int): seq[int] =
- ## Standard optimized sieve algorithm to get a list of prime numbers
- ## this is the function to compare your functions against!
- if n < 2:
- return @[]
- if n == 2:
- return @[2]
- var s: seq[int] = @[]
- for x in countup(3, n + 1, 2):
- s.add(x)
- # n**0.5 simpler than math.sqr(n)
- let mroot = int(pow(float(n), 0.5))
- let half = len(s)
- var i = 0
- var m = 3
- var j = 0
- while m <= mroot:
- if s[i] > 0:
- 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
- let data = lc[x | (x <- s, x > 0), int]
- return concat(@[2], data)
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement