Skip to content

Instantly share code, notes, and snippets.

@kronosapiens
Last active August 29, 2015 13:57
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 kronosapiens/9414655 to your computer and use it in GitHub Desktop.
Save kronosapiens/9414655 to your computer and use it in GitHub Desktop.
Solution to Project Euler #3
# [08:31:21] euler
# ƒ: ruby 03_lpf.rb
# 71
# 839
# 1471
# 6857
# 0.020000 0.000000 0.020000 ( 0.013420)
require 'pry'
require 'prime'
require 'benchmark'
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
class LPF
def lpf(prime)
primes = Prime.each
factor_array = []
prime_fac = primes.next
while prime_fac <= prime
if (prime % prime_fac) == 0
factor_array << prime_fac
prime = prime / prime_fac
else
prime_fac = primes.next
end
end
factor_array
end
end
l = LPF.new
puts Benchmark.measure {
answer = l.lpf(600851475143)
puts answer}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment