prime_finder 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/prime_finder.rb +65 -0
  2. metadata +46 -0
@@ -0,0 +1,65 @@
1
+ #
2
+ # Uses the Sieve of Eratosthenes algorithm to find the prime_finder numbers given an upper bound
3
+ #
4
+ class PrimeFinder
5
+
6
+ def find_primes *bounds
7
+
8
+ if bounds.size==0 || bounds.size > 2
9
+ raise "Either specify a lower bound and upper bound OR just the upper bound."
10
+ end
11
+
12
+ if bounds.size == 1
13
+ lower_bound = 2
14
+ upper_bound = bounds[0]
15
+ else
16
+ lower_bound = bounds[0]
17
+ upper_bound = bounds[1]
18
+ end
19
+
20
+ sqrt_upper = (Math.sqrt upper_bound).round
21
+ working_arr = Array.new(upper_bound) {|e| e = false}
22
+ return_arr = []
23
+
24
+ (2 .. sqrt_upper).each { |m|
25
+ if !working_arr[m]
26
+
27
+ if m >= lower_bound
28
+ return_arr << m
29
+ end
30
+
31
+ k = m * m
32
+ while k <= upper_bound
33
+ working_arr[k] = true
34
+ k += m
35
+ end
36
+ end
37
+ }
38
+
39
+ (sqrt_upper+1 .. upper_bound).each { |n|
40
+ if !working_arr[n] && n >= lower_bound
41
+ return_arr << n
42
+ end
43
+ }
44
+
45
+ return_arr
46
+
47
+ end
48
+
49
+ ########
50
+ # Main #
51
+ ########
52
+
53
+ if $0 == __FILE__
54
+ finder = PrimeFinder.new
55
+ if ARGV.size == 0
56
+ puts "USAGE: ruby prime_finder.rb <lower bound> <upper bound>"
57
+ puts "OR: ruby prime_finder.rb <upper bound>"
58
+ puts "EXAMPLE: ruby prime_finder.rb 30"
59
+ puts "EXAMPLE: ruby prime_finder.rb 2 30"
60
+ else
61
+ bounds = ARGV[0,(ARGV.length)].collect{|i| i.to_i}
62
+ puts finder.find_primes *bounds
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prime_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Chang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Uses the Sieve of Eratosthenes algorithm to find the prime numbers given
15
+ lower and/or upper bounds
16
+ email: rchang@fulcrum.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/prime_finder.rb
22
+ homepage: http://rubygems.org/gems/prime_finder
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.22
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Find prime numbers
46
+ test_files: []