Skip to content

Instantly share code, notes, and snippets.

@jjuliano
Created December 12, 2012 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjuliano/4266014 to your computer and use it in GitHub Desktop.
Save jjuliano/4266014 to your computer and use it in GitHub Desktop.
Find the largest prime factor of a composite number.
#!/usr/bin/env ruby
# Problem: Find the largest prime factor of a composite number.
# Description: The prime factors of 13195 are 5, 7, 13 and 29.
#
# What is the largest prime factor of the number 600851475143?
# Solution:
number = 600851475143
1.upto(number) do |i|
x = i/2 if (number%i).zero?
unless x.nil?
if x.odd? || x < 1
next
else
puts i
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment