Skip to content

Instantly share code, notes, and snippets.

@z2s8
Created March 3, 2016 01:21
Show Gist options
  • Save z2s8/596100d412b34f643e70 to your computer and use it in GitHub Desktop.
Save z2s8/596100d412b34f643e70 to your computer and use it in GitHub Desktop.
Prime finder in ruby
NUMBER_OF_PRIMES = 150000
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 with ruby 2.2.4 on Intel(R) Core(TM) 2 Quad Q9400 @ 2.66GHz:

[z2s8@Sherman crystal-mine]$ du -h prime.rb 
4.0K    prime.rb
[z2s8@Sherman crystal-mine]$ time ruby ./prime.rb 

real    0m18.160s
user    0m18.051s
sys     0m0.108s

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