mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Starts problem 35
Seems like I have a small problem. I have to make this solution more elegant and efficient. Working on it now Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
2
e_24.py
2
e_24.py
@@ -29,8 +29,6 @@ def all_permutations(seq):
|
||||
temp = []
|
||||
for k in range(len(seq)):
|
||||
part = seq[:k] + seq[k+1:]
|
||||
print "k : %s" % (k)
|
||||
print "part : %s" %(part)
|
||||
for m in all_permutations(part):
|
||||
temp.append(seq[k:k+1] + m)
|
||||
return temp
|
||||
|
||||
2
e_27.py
2
e_27.py
@@ -27,7 +27,7 @@ def is_prime(value):
|
||||
Returns True or False depending whether value is prime or not.
|
||||
"""
|
||||
start = 2
|
||||
while (start < value / 2):
|
||||
while (start <= value / 2):
|
||||
if value % start == 0 :
|
||||
return False
|
||||
else :
|
||||
|
||||
74
e_35.py
74
e_35.py
@@ -12,6 +12,78 @@
|
||||
##---
|
||||
"""
|
||||
|
||||
def all_permutations(seq):
|
||||
"""permutate a sequence and return a list of the permutations"""
|
||||
if not seq:
|
||||
return [seq] # is an empty sequence
|
||||
else:
|
||||
temp = []
|
||||
for k in range(len(seq)):
|
||||
part = seq[:k] + seq[k+1:]
|
||||
for m in all_permutations(part):
|
||||
temp.append(seq[k:k+1] + m)
|
||||
return temp
|
||||
|
||||
def is_prime(value):
|
||||
"""
|
||||
Returns True or False depending whether value is prime or not.
|
||||
"""
|
||||
start = 2
|
||||
while (start <= value / 2):
|
||||
if value % start == 0 :
|
||||
return False
|
||||
else :
|
||||
start += 1
|
||||
return True
|
||||
|
||||
def prime_list(max_val):
|
||||
"""
|
||||
Returns a list of all primes below max_val
|
||||
"""
|
||||
plist = []
|
||||
cur = 2
|
||||
while (cur < max_val):
|
||||
if (cur % 1000) == 0:
|
||||
print "%d/%d" % (cur, max_val)
|
||||
|
||||
if (is_prime(cur)):
|
||||
plist.append(cur)
|
||||
cur += 1
|
||||
|
||||
return plist
|
||||
|
||||
def perm_primes(max_val):
|
||||
"""
|
||||
Returns the number of circular primes below max_val
|
||||
"""
|
||||
circulars = 0
|
||||
circl = []
|
||||
|
||||
plist = prime_list(max_val)
|
||||
print "###"
|
||||
# stuff here
|
||||
cpt = 0
|
||||
for prime in plist:
|
||||
cpt += 1
|
||||
print "%d/%d" % ( cpt, len(list))
|
||||
print circulars, circl
|
||||
perms = all_permutations(str(prime)) # finds all permutations
|
||||
|
||||
# counts how many permutations are in prime list given one prime
|
||||
p_cpt = 0
|
||||
for perm in perms:
|
||||
if int(perm) in plist:
|
||||
p_cpt += 1
|
||||
|
||||
# if prime is circular
|
||||
if p_cpt == len(perms):
|
||||
circulars += 1
|
||||
circl.append(prime)
|
||||
|
||||
return circulars, circl
|
||||
|
||||
if __name__ == '__main__' :
|
||||
print "Answer is : %d" % (1000000)
|
||||
answer, plist = perm_primes(1000000)
|
||||
print "Answer is : %d" % (answer)
|
||||
print plist
|
||||
raw_input()
|
||||
Reference in New Issue
Block a user