الخميس، 26 ديسمبر 2013

Project Euler #3 in Ruby

Project Euler Problem#3:

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

def is_prime?(number) prime = true (2...number).each { |x| prime = false if number % x == 0 } primeenddef largest_prime(number) primes = [] (number.downto(1)).each {|x| primes.push(x) if number % x == 0 && is_prime?(x) break if primes.count == 1 } primes[0]end

My answer is written in Ruby. The code works for smaller numbers but not larger, can anyone explain what exactly is going on and how to get around it? I've seen other people with this issue -- sorry to repost -- but I'm a programming newb and I don't truly understand their answers, also haven't seen any other posts answering in Ruby. Thanks!


View the original article here

ليست هناك تعليقات:

إرسال تعليق