Skip to content

Instantly share code, notes, and snippets.

@sarony
Created December 12, 2013 01:15
Show Gist options
  • Save sarony/7921670 to your computer and use it in GitHub Desktop.
Save sarony/7921670 to your computer and use it in GitHub Desktop.
require 'pry'
num = 13195
class FindLargestPrimeFactor
def initialize(num)
@num = num
end
def run
@factors = find_factors(@num)
@prime_factors = find_prime_factors(@factors)
largest_prime(@prime_factors)
end
def find_factors(num)
i = 1
num_factors = []
(num-1).times do |n|
if num%i==0
num_factors << i
end
i+=1
end
num_factors
end
# find all the prime factors
def find_prime_factors(num_factors)
prime_factors = []
num_factors.each do |num|
i = 1
factors = []
(num).times do |n|
if num%i==0
factors << i
end
i+=1
end
if factors.count == 2
prime_factors << num
end
end
prime_factors
end
# find the largest prime number
def largest_prime(prime_factors)
prime_factors.sort.last
end
end
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment