Euler 0013
The Problem:
WhatThere is the100 value50-digit ofnumbers thethat first triangle numberneeded to havebe oversummed fivetogether. hundredThe divisors?attached file has been added to do this.
Considerations:
WeIn don'tpython actuallythis needproblem tois computeirrelevant allbecause ofwe can fit giant numbers into the divisorsinteger of a number, since the number of divisors can be calculated by knowing the number of prime factors.
12, for example, has prime factors 2(2 of these)space and 3.python Itwill haswork a total of 6 divisors, which can be calculated by taking the quantity of each prime factor and adding 1, 2 for 2 becomes 3 and 1 for 3 becomes 2... Ok, maybe that's an obtuse way of explainingwith it... If you want a good explanation of it, you can find it here: https://mathschallenge.net/library/number/number_of_divisors
The Approach:
We areread goingthe file, sum the numbers, convert to usestring, and then convert back into number after slicing the samefirst prime10 factors finder that we have before, but instead of we are going to multiple by a running divisors count.digits.
The Code:
import math
#This is a modification of the prime factor finder
def calculate_divisors(number):
upper_limitnumbers_file = numberopen("numbers.txt", current_upper_limit"r")
total = upper_limit
sqrt_limit = math.sqrt(upper_limit)
current_divisors = 1
#we are going to hardcode 2 and 30
for loopingnumber sakein ifnumbers_file:
upper_limit % 2 == 0:
current_div = 1
while current_upper_limit % 2 == 0:
current_upper_limit = current_upper_limit/2
current_divtotal += 1
current_divisors *= current_div
if upper_limit % 3 == 0:
current_div = 1
while current_upper_limit % 3 == 0:
current_upper_limit = current_upper_limit/3
current_div += 1
current_divisors *= current_div
current = 6
while current < sqrt_limit:
#print(prime_factors, current_upper_limit)
if current_upper_limit % (current - 1) == 0:
current_div = 1
while current_upper_limit % (current - 1) == 0:
current_upper_limit = current_upper_limit/(current-1)
current_div += 1
current_divisors *= current_div
if current_upper_limit % (current + 1) == 0:
current_div = 1
while current_upper_limit % (current + 1) == 0:
current_upper_limit = current_upper_limit/(current+1)
current_div += 1
current_divisors *= current_div
current += 6
return current_divisors
bailout = 100000 #keep checking up to a bailout number
triangle = 1
for i in range(2,bailout):
triangle += i
if calculate_divisors(triangle) >= 500:
breakint(number)
print(i,str(total)[:10])
triangle)