Skip to content

Instantly share code, notes, and snippets.

@danvonk
Created October 13, 2014 18:01
Show Gist options
  • Save danvonk/88dcd01df77532dc538d to your computer and use it in GitHub Desktop.
Save danvonk/88dcd01df77532dc538d to your computer and use it in GitHub Desktop.
euler12
#!/usr/bin/env ruby
def numDivisors(x)
factors = 0
(2..x).take(Math.sqrt(x).to_i + 1).each do |u|
if x % u == 0
factors += 2
end
end
factors += 2
return factors
end
count = 1
tri = 0
loop do
tri = (1..count).inject(:+) #triangle number
break if numDivisors(tri) >= 500
count = count + 1
end
puts "First triangle number to have 500 factors is #{tri}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment