Tiberium

Untitled

Mar 30th, 2017
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import times
  3. import future
  4. import math
  5. import sequtils
  6. import strutils
  7.  
  8. proc get_primes7(n: int): seq[int] =
  9.   ## Standard optimized sieve algorithm to get a list of prime numbers
  10.   ## this is the function to compare your functions against!
  11.  
  12.   if n < 2:
  13.     return @[]
  14.  
  15.   if n == 2:
  16.     return @[2]
  17.  
  18.   var s: seq[int] = @[]
  19.  
  20.   for x in countup(3, n + 1, 2):
  21.     s.add(x)
  22.  
  23.   # n**0.5 simpler than math.sqr(n)
  24.   let mroot = int(pow(float(n), 0.5))
  25.   let half = len(s)
  26.   var i = 0
  27.   var m = 3
  28.   var j = 0
  29.  
  30.   while m <= mroot:
  31.     if s[i] > 0:
  32.       j = (m * m - 3) div 2  # int div
  33.       s[j] = 0
  34.       while j < half:
  35.         s[j] = 0
  36.         j += m
  37.     inc(i)
  38.     m = 2 * i + 3
  39.  
  40.   let data = lc[x | (x <- s, x > 0), int]
  41.   return concat(@[2], data)
  42.  
  43. let start_time = getTime()
  44. let period_time = getEnv("RUN_TIME").parseInt()
  45.  
  46. while int(getTime() - start_time) < period_time:
  47.   let res = get_primes7(10000000)
  48.   echo("Found $1 prime numbers." % $res.len)
RAW Paste Data