Solves problem 43

Starts working on problem 55
This commit is contained in:
2012-10-20 20:00:36 +02:00
parent 99334f1594
commit 203ea69dbb
3 changed files with 77 additions and 6 deletions

View File

@@ -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 <br /> 40 - Finding the nth digit of the fractional part of the irrational number. > 2 sec <br />
41 - What is the largest n-digit pandigital prime that exists? < 5 min <br /> 41 - What is the largest n-digit pandigital prime that exists? < 5 min <br />
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br /> 42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
43 - Find the sum of all pandigital numbers with an unusual sub-string +divisibility property. - 28sec <br />
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br /> 45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec <br /> 46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec <br />
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br /> 48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br />
@@ -70,7 +71,6 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
## In progress: ## In progress:
43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property. <br />
47 - Find the first four consecutive integers to have four distinct primes factors. <br /> 47 - Find the first four consecutive integers to have four distinct primes factors. <br />
55 - How many Lychrel numbers are there below ten-thousand? <br /> 55 - How many Lychrel numbers are there below ten-thousand? <br />
97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br /> 97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br />

53
e_43.py
View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python
''' '''
Created on 10 feb. 2012 Created on 10 feb. 2012
@@ -17,8 +17,53 @@ d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13 d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17 d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property. 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__': if __name__ == '__main__':
print 1 print "Answer : %d " % (find_cool_pandigital())
#print "Answer : %d " % (last_ten())

28
e_55.py
View File

@@ -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__': if __name__ == '__main__':
print "Answer : %d " % (1) print palindrome(12345)
#print "Answer : %d " % (count_lychrel(10000))