Skip to content

Commit

Permalink
prime factors
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Aug 2, 2011
1 parent 391e632 commit 5b82cfa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lantoli/lib/primefactors.rb
@@ -1,3 +1,5 @@
require 'mathn'

class PrimeFactors

def self.generate number
Expand All @@ -13,7 +15,7 @@ def self.generate number
candidate = (candidate==2) ? 3 : candidate + 2 # performance tip: only odd candidates
end
end
factors << number unless number <= limit # performance tip
factors << number unless number <= limit # performance tip - square root
factors
end

Expand All @@ -23,7 +25,7 @@ class Integer
def factor_of? number
number % self == 0
end

def sqrt
Math.sqrt(self).to_i
end
Expand Down
5 changes: 4 additions & 1 deletion lantoli/spec/primefactors_spec.rb
Expand Up @@ -12,7 +12,10 @@
288 => [2,2,2,2,2,3,3],
24 => [2,2,2,3],
100 => [2,2,5,5],
123456789 => [3,3,3607,3803]
123456789 => [3,3,3607,3803],
123456791 => [123456791],
1234567927 => [1234567927],
2469135854 => [2,1234567927]
}

primeTests.each do | number, factors|
Expand Down

0 comments on commit 5b82cfa

Please sign in to comment.