-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_044_00.py
More file actions
129 lines (102 loc) · 2.73 KB
/
problem_044_00.py
File metadata and controls
129 lines (102 loc) · 2.73 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
##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 createseq(tupleseq):
seq = ""
for i in tupleseq:
seq += i
return seq
def isPentagonal(number):
"""Returns a tuple (a,b): a = 1, if there is a number (n) that satisfies
number = n*(3n-1)/2 and 0 otherwise. b = the value of n that verifies that
condition, 0 otherwise. """
conditionplus = (1+np.sqrt(1+24*number))/2
conditionminus = (1-np.sqrt(1+24*number))/2
if int(conditionplus) == conditionplus:
pass
elif int(conditionminus) == conditionminus:
pass
else:
pass
return (a,b)
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()
euler43()
## print(evaluatelist(["SKY","REGIONAL"], triangles(1000)))
## print(valueword("ABOUT"))
## print(valueword("SKY"))
end = time.time()
print(end-start)
main()