Created
March 3, 2016 01:21
-
-
Save z2s8/596100d412b34f643e70 to your computer and use it in GitHub Desktop.
Prime finder in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test with
ruby 2.2.4onIntel(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