Skip to content

Instantly share code, notes, and snippets.

@flakyfilibuster
Created October 6, 2012 19:31
Show Gist options
  • Save flakyfilibuster/3845868 to your computer and use it in GitHub Desktop.
Save flakyfilibuster/3845868 to your computer and use it in GitHub Desktop.
prime_factors
# 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