From 203ea69dbb24ec16de270adead4e4711fe3cd53c Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sat, 20 Oct 2012 20:00:36 +0200 Subject: [PATCH] Solves problem 43 Starts working on problem 55 --- README.markdown | 2 +- e_43.py | 53 +++++++++++++++++++++++++++++++++++++++++++++---- e_55.py | 28 +++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 53f6f96..9ec9381 100644 --- a/README.markdown +++ b/README.markdown @@ -59,6 +59,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o 40 - Finding the nth digit of the fractional part of the irrational number. > 2 sec
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
+43 - Find the sum of all pandigital numbers with an unusual sub-string +divisibility property. - 28sec
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053
@@ -70,7 +71,6 @@ So you may find some of the code here quite ugly. And this is the case :). Why o ## In progress: -43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property.
47 - Find the first four consecutive integers to have four distinct primes factors.
55 - How many Lychrel numbers are there below ten-thousand?
97 - Find the last ten digits of the non-Mersenne prime: 28433 � 2^7830457 + 1.
diff --git a/e_43.py b/e_43.py index d52405d..f7afe80 100644 --- a/e_43.py +++ b/e_43.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python ''' Created on 10 feb. 2012 @@ -17,8 +17,53 @@ d6d7d8=572 is divisible by 11 d7d8d9=728 is divisible by 13 d8d9d10=289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. -''' + +Will be very long due to dumb implementation. +Would be way better with a generator implementation of all_permutations already +''' +my_primes = [2, 3, 5, 7, 11, 13, 17] + + +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_cool(str_number): + """ + Returns True of number (in a string form) has the cool divisible property + """ + for i in range(len(str_number) - 3): + cur = str_number[i + 1:i + 4] + if (int(cur) % my_primes[i]) != 0: + return False + return True + + +def find_cool_pandigital(): + """ + Returns the sum of all 0 to 9 pandigital numbers with cool property. + """ + cases = all_permutations("1234567890") + cool = [] + cpt = 0 + for case in cases: + #if int(case[0]) != 0: + # check for cool property + if is_cool(case): + cool.append(int(case)) + + print cool + print len(cool) + return sum(cool) if __name__ == '__main__': - print 1 - #print "Answer : %d " % (last_ten()) \ No newline at end of file + print "Answer : %d " % (find_cool_pandigital()) diff --git a/e_55.py b/e_55.py index bbd484e..33bc5a9 100644 --- a/e_55.py +++ b/e_55.py @@ -42,5 +42,31 @@ the theoretical nature of Lychrel numbers. """ +def reverse(num): + """ + Returns the palidromic number of num + """ + str_num = str(num) + rev = '' + for i in range(len(str_num)): + rev += str_num[len(str_num) - i - 1] + + return int(rev) + + +def count_lychrel(max_num, max_it=50): + """ + Returns the number of lychrel numbers smaller than max_num + The maximum iteration number defines how many times the process is repeated + before considreing a number to be a lychrel number + """ + lychrels = [] + for i in xrange(1, max_num + 1): + cur = i + + return 1 + + if __name__ == '__main__': - print "Answer : %d " % (1) + print palindrome(12345) + #print "Answer : %d " % (count_lychrel(10000))