Last active
August 29, 2015 13:57
-
-
Save kronosapiens/9414655 to your computer and use it in GitHub Desktop.
Solution to Project Euler #3
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
# [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