Project Euler #501

Aside from taking so long to run in ruby, this problem is fairly easy to crack using a brute-force testing strategy. I’m curious if anyone else has a faster way or could post a better example in a faster language than ruby.

def f(number)

  testing_range = (1..number)
 
  numbers_with_8_divisors = []
 
  testing_range.each do |n|
    divisors = []
    (1..n).each do |x|
      divisors << x if n%x == 0
    end
    numbers_with_8_divisors << n if divisors.length == 8
  end
  numbers_with_8_divisors.length
 
end

p f(10**12)
=> "too long to wait for, but this works lolz"
Project Euler #501

Leave a comment