-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_044_02.py
More file actions
184 lines (159 loc) · 4.24 KB
/
problem_044_02.py
File metadata and controls
184 lines (159 loc) · 4.24 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
##Pentagonal numbers are generated by the formula,
##Pn=n(3n−1)/2. The first ten pentagonal numbers are:
##
##1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
##
##It can be seen that P4 + P7 = 22 + 70 = 92 = P8.
##However, their difference, 70 − 22 = 48, is not pentagonal.
##
##Find the pair of pentagonal numbers, Pj and Pk,
##for which their sum and difference are pentagonal and
##D = |Pk − Pj| is minimised; what is the value of D?
import numpy as np
import itertools
import math
import time
import eulertools as et
def createpent(n):
pn = n * (3 * n - 1) / 2
return pn
## 1. Find the first couple of pentagonal numbers whose difference is also
## a pentagonal number.
## 1.a. Create a list of pentagonal numbers.
## 1.b. Create the next pentagonal number.
## 1.c. Test if the new pentagonal number, minus any of the pentagonal numbers
## in the list yields a pentagonal number, if so, add it to a set of combinations.
def euler44():
setpent = set()
listpent = set()
listdif = []
dictdif = {}
proceed = 1
current_n = 1
while proceed == 1:
#1.b
pn = createpent(current_n)
listpent.add(pn)
## print(listpent)
## input("a")
#1.c
## print(current_n)
## if pn == 92:
## input(92)
for i in listpent:
## print(i)
try:
dif = pn - i
## print("dif " + str(dif))
if dif in listpent:
## print(dif,pn,i,current_n)
## sv = frozenset[pn, i, dif]
## print(sv)
if isPentagonal(pn+i) != 3:
## setpent.add(sv)
## print(setpent)
print(dif, pn, i)
## input("b " + str(current_n))
proceed = 0
else:
pass
except:
pass
current_n += 1
## if current_n % 1000 == 0:
## print(current_n)
## input("c " + str(current_n))
## listpent
## input("listpent")
#print(current_n, listpent)
def isPentagonal(number):
conditionplus = (1+np.sqrt(1+24*number))/6
## conditionminus = (1-np.sqrt(1+24*number))/6
if int(conditionplus) == conditionplus:
m = 1
## elif int(conditionminus) == conditionminus:
## m = 2
else:
m = 3
return m
##def convertfiletolist(filename):
## f = open(filename)
## lw = f.read().replace('"',"").split(",")
## f.close()
## return lw
##
##def maxvalue(listw):
## maxl = 0
## maxv = ord("Z") - 64
## for i in listw:
## maxl = max(maxl, len(i))
## return maxl*maxv
##
##def valueword(word):
## value = 0
## for i in word:
## value += ord(i)
## value = value -(64*len(word))
#### print("value = ", value, end = " ")
## return value
##
##def evaluatelist(lw,st):
## count = 0
## for i in lw:
#### print(i, end = " ")
## vw = valueword(i)
#### print(vw, end = "\n")
## if vw in st:
## count +=1
#### print("new", i, vw, "\n")
## return count
##
##def evaluaten(n,lp):
## sn = str(n)
## c = 0
## d = 0
## for i in range(2,9):
## if int(sn[i-1:i+2]) % lp[d] == 0:
## c += 0
## else:
## c += 1
## d += 1
##
## if c == 0: return True
## else: return False
##
##def euler43():
## """Generates listofprimes < 18, evaluates permutations 0-9 for Euler43"""
##
## lp = et.primesfrom2to(18)
## print(lp)
## s = ""
## for x in range(0,10):
## s += str(x)
##
##
##
## listes=[]
## lv = []
## for n in itertools.permutations(s):
## sn = createseq(n)
## if evaluaten(sn,lp):
## listes.append(int(sn))
##
##
## print(sum(listes))
## print(listes[1])
## print(evaluate(str(listes[1]),lp))
##
####
####print(evaluaten("1406357289",et.primesfrom2to(18)))
####input("result")
def main():
start = time.time()
euler44()
## print(evaluatelist(["SKY","REGIONAL"], triangles(1000)))
## print(valueword("ABOUT"))
## print(valueword("SKY"))
end = time.time()
print(end-start)
main()