What is Project Euler

Project Euler is a website with a collection of programming and mathematical problems. Each challenge varies in difficulty and there are over 600 challenges. I highly recommend anyone interested in puzzles and programming to create an account and start solving. About Project Euler

** Stop here if you have not yet tried and solved Problem 3. **

**

**

**

**

Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

class Problem3
  require 'benchmark'

  def question
    "The prime factors of 13195 are 5, 7, 13 and 29.

    What is the largest prime factor of the number 600851475143 ?"
  end

  def largest_prime_factor n
    max = 0
    limit = n
    runner = 2

    while(runner <= limit) do
      factor = 0
      if limit % runner == 0
        limit = limit / runner
        factor = runner
        max = factor if factor > max
      else
        runner = runner + 1
      end
    end

    max
  end

  def solve
    largest_prime_factor(600851475143)
  end

  def benchmark
    Benchmark.measure { solve }
  end

  def correct?
    solve === answer
  end

  private

  def answer
    6857
  end
end

 

Did you like this article? Check out these too.


 

Found this useful? Have a suggestion? Get in touch at blog@hocnest.com.