-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler-012
More file actions
23 lines (18 loc) · 741 Bytes
/
euler-012
File metadata and controls
23 lines (18 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require 'prime'
triangle_number_index = 1
loop do
# Calculate the nth triangle number
triangle_number = (triangle_number_index * (triangle_number_index + 1)) / 2
# Get the prime factorization of the triangle number
prime_factors = triangle_number.prime_division
# Calculate the number of divisors
# For each prime factor p^a, it contributes (a+1) to the divisor count
# The total divisor count is the product of these contributions
divisor_count = prime_factors.map { |base, power| power + 1 }.reduce(:*)
if divisor_count > 500
puts "First triangle number with over 500 divisors: #{triangle_number}"
puts "This is triangle number ##{triangle_number_index}"
break
end
triangle_number_index += 1
end