Permalink
Browse files

faster prime number iterator

  • Loading branch information...
jpsilvashy committed Mar 20, 2012
1 parent 211b460 commit 6c9d3720a9ce93941a640b4a4e981918ceaa9b79
Showing with 18 additions and 18 deletions.
  1. +18 −18 ruby/problem_003.rb
View
@@ -9,30 +9,30 @@
val = ARGV[0].to_i
-def factors_of(int)
- factors = []
-
- (1..int).each do |x|
- if (int % x) == 0
- factors << x
- end
+def is_prime?(n)
+ for d in 2..(Math.sqrt(n))
+ if (n % d) == 0
+ return false
end
+end
- factors
+true
end
time = Benchmark.measure do
-
- factors_of(val).each do |f|
- if f %
-
+ count = val
+ val.times do
+ if count.even?
+ count -= 1
+ else
+ if is_prime?(count) == true
+ puts "\n#{count} is prime!"
+ break
+ end
+ print '.'
+ count -= 2
end
end
-
end
-puts time
-
-# if factors_of(val).length == 2
-# puts "is prime"
-# end
+puts time

0 comments on commit 6c9d372

Please sign in to comment.