Created
October 13, 2014 18:01
-
-
Save danvonk/88dcd01df77532dc538d to your computer and use it in GitHub Desktop.
euler12
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
#!/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