Last active
August 29, 2015 14:16
-
-
Save spencerroan/495610723cd6e4c22198 to your computer and use it in GitHub Desktop.
playing with a prime number seive 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
| # http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
| class Primes | |
| def self.time_it | |
| t = Time.now | |
| rval = yield | |
| elapsed = Time.now - t | |
| #return rval, elapsed | |
| puts elapsed | |
| return rval | |
| end | |
| def self.detect_primes(primes, limit); | |
| time_it do | |
| last = primes.last + 1 | |
| (last..limit).each do |number| | |
| primes << number unless primes.any? { |prime| number % prime == 0} | |
| end | |
| primes | |
| end | |
| end | |
| def self.detect_primes2(primes, limit) | |
| time_it do | |
| last = primes.last + 1 | |
| cache = Hash.new { |hsh,key| hsh[key] = key ** 2 } | |
| (last..limit).each do |number| | |
| primes << number unless primes.select { |p| cache[p] <= number }.any? { |prime| number % prime == 0 } | |
| end | |
| primes | |
| end | |
| end | |
| def self.detect_primes3(primes, limit) | |
| time_it do | |
| last = primes.last + 1 | |
| (last..limit).each do |number| | |
| primes << number unless primes.select { |p| p**2 <= number }.any? { |prime| number % prime == 0 } | |
| end | |
| primes | |
| end | |
| end | |
| def self.detect_primes4(primes, limit) | |
| time_it do | |
| last = primes.last + 1 | |
| (last..limit).each do |number| | |
| num_sq = number ** 0.5 | |
| primes << number unless primes.select { |p| p <= num_sq }.any? { |prime| number % prime == 0 } | |
| end | |
| primes | |
| end | |
| end | |
| def self.detect_primes5(primes, limit) | |
| time_it do | |
| searchable_primes = primes.clone | |
| last_searchable_prime_squared = searchable_primes.last ** 2 | |
| last = primes.last + 1 | |
| (last..limit).each do |number| | |
| if last_searchable_prime_squared <= number | |
| searchable_primes << primes[searchable_primes.size] | |
| last_searchable_prime_squared = searchable_primes.last ** 2 | |
| end | |
| primes << number unless searchable_primes.any? { |prime| number % prime == 0 } | |
| end | |
| primes | |
| end | |
| end | |
| end | |
| #max = 50_000 | |
| #puts primes = Primes.detect_primes([2], max).count | |
| #Primes.detect_primes2([2], max) | |
| #Primes.detect_primes3([2], max) | |
| #puts Primes.detect_primes4([2], max).count | |
| #puts Primes.detect_primes4([2], max).count | |
| # | |
| primes = Primes.detect_primes([2], 2**16) | |
| puts #{primes.count} primes found" | |
| logs = primes.map {|p| Math.log(p,2) }. | |
| puts logs.inject(Hash.new(0)) {|memo, log| memo[log.floor] += 1; memo}.sort.inspect | |
| stuff.each_cons(2) {|(a,b),(c,d)| puts "primes log base 2 range: #{a}-#{c}, count: #{b}-#{d}, count_ratio: #{(d/b.to_f).round(2)}" } |
i wonder about building my own recursive square root finder that goes to the nearest integer instead of the one that exists...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
change to PrimeTime and change inputs hehehe!