Created
October 6, 2012 19:31
-
-
Save flakyfilibuster/3845868 to your computer and use it in GitHub Desktop.
prime_factors
This file contains 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
# actual funtion to get the prime factors | |
def prime_factors(n) | |
primes = [] | |
until n == 1 | |
primes << prime_number(n) | |
n /= primes.last | |
puts "primes_array:#{primes.inspect} | n:#{n}" | |
end | |
return primes.inspect | |
end | |
# function to return the prime number | |
def prime_number(n) | |
2.upto(n).each do |i| | |
return i if is_prime?(n,i) | |
end | |
end | |
# function that checks if the number is a prime factor | |
def is_prime?(number, iterator) | |
number%iterator == 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment