-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem12.rb
47 lines (41 loc) · 962 Bytes
/
Problem12.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
puts 'Enter a number of divisors and I will tell you',
'the first triangle number to have more than that many divisors:'
divisorGoal = gets.chomp.to_i
numberDivisors = 1
x=1
require 'prime'
until numberDivisors > divisorGoal
numberDivisors=1
naturalsum = 0
for i in 0..x
naturalsum+=i
end
factorized = naturalsum.prime_division
for i in 0..factorized.length-1
numberDivisors*=factorized[i][1]+1
end
x+=1
end
puts 'The value of the first triangle number to have more than ' + divisorGoal.to_s + ' divisors is ' + naturalsum.to_s + '.'
# this was too slow
# naturalsum = 0
#
# class Integer
# def factors()
# (1..self).select{|n|(self%n).zero?}
# end
# end
#
#
# until naturalsum.factors.length > divisorGoal
# naturalsum = 0
# for i in 0..x
# naturalsum+=i
# end
# puts 'natural sum ' + naturalsum.to_s
# if naturalsum.factors.length> divisorGoal
# puts naturalsum
# else
# x+=1
# end
# end