diff --git a/README.markdown b/README.markdown
index 3dabbed..0655658 100644
--- a/README.markdown
+++ b/README.markdown
@@ -48,6 +48,7 @@ Should be used in order to help future reuse of code :)
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions? - 1min
+41 - What is the largest n-digit pandigital prime that exists? < 5 min
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053
@@ -60,7 +61,6 @@ Should be used in order to help future reuse of code :)
26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
33 - Discover all the fractions with an unorthodox cancelling method.
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?
-41 - What is the largest n-digit pandigital prime that exists?
97 - Find the last ten digits of the non-Mersenne prime: 28433 × 2^7830457 + 1.
**WARNING : Spoil inside for those who want to solve problems by themselves :)**
diff --git a/e_41.py b/e_41.py
index 4439939..19a4844 100644
--- a/e_41.py
+++ b/e_41.py
@@ -10,6 +10,81 @@ We shall say that an n-digit number is pandigital if it makes use of all the dig
What is the largest n-digit pandigital prime that exists?
'''
+val = 123456789 # all numbers to create the biggest pandigital number
+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
+
+# I could use a decorator to remove trivial non primes here
+def all_permutations(seq):
+ """permutates a sequence and returns 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 divisible_by_2(val):
+ """
+ Returns True if val contains an even number (ex : 998)
+ """
+ return ((val % 2) == 0)
+
+def divisible_by_5(val):
+ """
+ Returns True if any circular permutation of val is divisible by (ex : 907)
+ """
+ return (str(5) == str(val)[-1])
+
+def divisible_by_3(val):
+ """
+ Returns True if any circular permutation of val is divisible by 3 (ex : 12)
+ """
+ temp = sum([int(p) for p in str(val)])
+ if len(str(temp)) > 1:
+ divisible_by_3(temp)
+ else:
+ return ((temp % 3) == 0)
+
+def check_easy_out(pelist):
+ """
+ Returns the number of circular primes below max_val
+
+ TODO : do this while creating all the permutations !
+ """
+ pred = list(pelist)
+ for p in pelist:
+ if p > 11: # my filter does not work for values under 10
+ if (divisible_by_2(p) or divisible_by_5(p) or divisible_by_3(p)):
+ pred.remove(p)
+ return pred
+
+def biggest_pandigital():
+ """
+ Returns the biggest pandigital prime number
+ """
+ for n in range(9): # from 0 to 9 numbers removed of max pand number
+ print " n : %d" %(n)
+ root = str(val)[:len(str(val)) -n]
+ perms = [ int(p) for p in all_permutations(root)]
+ perms = check_easy_out(perms)
+ perms.sort(reverse=True)
+ for p in perms :
+ if is_prime(p):
+ return p
+
+ return -1 # problem
if __name__ == '__main__':
- print 1
- #print "Answer : %d " % (last_ten())
\ No newline at end of file
+ print "Answer : %d " % (biggest_pandigital())
\ No newline at end of file