Skip to content

Instantly share code, notes, and snippets.

@z2s8
Created March 3, 2016 01:13
Show Gist options
  • Save z2s8/aa19d5b40cecdf381465 to your computer and use it in GitHub Desktop.
Save z2s8/aa19d5b40cecdf381465 to your computer and use it in GitHub Desktop.
Prime finder in crystal-lang
require "math"
NUMBER_OF_PRIMES = 1500000
primes = [2]
test_number = 2
primes_found = 0
while primes_found < NUMBER_OF_PRIMES
test_primitiveness = true
i = 0
while (primes.size > i && primes[i] <= Math.sqrt(test_number) && test_primitiveness)
if test_number % primes[i] == 0
test_primitiveness = false
end
i += 1
end
if test_primitiveness
primes << test_number
primes_found += 1
end
test_number += 1
end
@z2s8
Copy link
Author

z2s8 commented Mar 3, 2016

Test run on Intel(R) Core(TM) 2 Quad Q9400 @ 2.66GHz with Crystal 0.11.1:

[z2s8@Sherman crystal-mine]$ crystal build --release prime.cr 
[z2s8@Sherman crystal-mine]$ time ./prime 

real    0m4.932s
user    0m4.925s
sys     0m0.009s
[z2s8@Sherman crystal-mine]$ time ./prime 

real    0m4.911s
user    0m4.900s
sys     0m0.013s
[z2s8@Sherman crystal-mine]$ time ./prime 

real    0m4.919s
user    0m4.911s
sys     0m0.010s
[z2s8@Sherman crystal-mine]$ du -h prime
268K    prime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment