-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path012.py
More file actions
101 lines (78 loc) · 1.89 KB
/
012.py
File metadata and controls
101 lines (78 loc) · 1.89 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!python
problem = """
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The fi
rst ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
https://projecteuler.net/problem=12
OR
Must find the smallest number t, such that
t = n*(n+1)/2
for all n, also satisfying
t = PI(p^m)
is the prime factorization of t.
The number of divisors is D = PI(m+1)
600 = 2^3 * 3 * 5^2 --> (3+1)(1+1)(2+1) = 24 divisors
"""
print(problem)
# first get the list of the first X primes:
import math
top=3000
primeset=[2,3]
m=1
x=len(primeset)
def check_prime(n):
global x,primeset
primetest=True
topn=math.sqrt(n)
for i in primeset:
if i > topn:
break
if (n%i) == 0:
primetest=False
if primetest == True:
primeset.append(n)
x=len(primeset)
# print("found",x,n)
while x < top:
check_prime((6*m)-1)
check_prime((6*m)+1)
m+=1
n = 2
bigtop=50000
dictofmax={'n':0,'Tri':0,'Expos':0}
numofdivs=0
while numofdivs < 500:
keeptri = int(n * ( n+1 ) /2)
tri=keeptri
listofdivs=[]
while tri > 1:
for m in primeset:
if m <= tri:
if ( tri % m ) == 0:
listofdivs.append(m)
tri /= m
setofdivs=set(listofdivs)
dicty={}
numofdivs=1
for p in setofdivs:
expo=listofdivs.count(p)
numofdivs*=(expo+1)
dicty[p]=expo
if numofdivs > dictofmax['Expos']:
dictofmax['n']=n
dictofmax['Tri']=keeptri
dictofmax['Expos']=numofdivs
print(n,keeptri,listofdivs,setofdivs,dicty,numofdivs,dictofmax)
n+=1
print()
print(keeptri,numofdivs)