This commit is contained in:
steinkirch 2013-11-11 19:11:15 -05:00
parent f525063843
commit cf40c0610f
3 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,45 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
'''
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
'''
def find_sum_proper_divisors(n):
sum_proper_div = 0
for i in range(1, n):
if n%i == 0:
sum_proper_div += i
return sum_proper_div
def amicable_numbers(N):
sum_div_list = [find_sum_proper_divisors(i) for i in range(1, N+1)]
sum_amicable_numbers = 0
set_div = set()
for a in range(1, N):
da = sum_div_list[a-1]
if da < N:
b = da
db = sum_div_list[b-1]
if a != b and db == a and a not in set_div and b not in set_div:
sum_amicable_numbers += a + b
set_div.add(a)
set_div.add(b)
return sum_amicable_numbers
def main():
print(amicable_numbers(10000))
print('Tests Passed!')
if __name__ == '__main__':
main()

View File

@ -0,0 +1,76 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
# very dumb version, need to be optimized
'''
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 x186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
'''
def is_pandigital(n):
size = len(n)
pan = set([x for x in range(1,size + 1)])
for i in n:
if int(i) in pan:
pan = pan - {int(i)}
else:
return False
return True
def find_multiples(n):
multiples = set([1, n])
for i in range(2, n):
if n%i == 0:
multiples.add(i)
multiples.add(n//i)
return multiples
def find_pandigital():
sum_pan = 0
for i in range(10, 987654322):
if i//10 == 0:
continue
digits = set(str(i))
if len(digits) != len(str(i)):
continue
multiples = find_multiples(i)
mult_set = set()
for j in multiples:
for c in str(j):
if c in digits or int(c) == 0:
continue
for c in str(j):
mult_set.add(c)
k = i//j
for c in str(k):
if c in digits or int(c) == 0:
continue
for c in str(k):
mult_set.add(c)
if len(mult_set ) == 9:
sum_pan += j + k + i
return sum_pan
def main():
assert(is_pandigital('15234') == True )
assert(is_pandigital('15233') == False )
print(find_pandigital())
print('Tests Passed!')
if __name__ == '__main__':
main()

View File

@ -0,0 +1,73 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
'''
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.
The product 7254 is unusual, as the identity, 39 x186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
'''
def is_pandigital(n):
size = len(n)
pan = set([x for x in range(1,size + 1)])
for i in n:
if int(i) in pan:
pan = pan - {int(i)}
else:
return False
return True
def find_multiples(n):
multiples = set([1, n])
for i in range(2, n):
if n%i == 0:
multiples.add(i)
multiples.add(n//i)
return multiples
def find_pandigital():
sum_pan = 0
for i in range(10, 987654322):
if i//10 == 0:
continue
digits = set(str(i))
if len(digits) != len(str(i)):
continue
multiples = find_multiples(i)
mult_set = set()
for j in multiples:
for c in str(j):
if c in digits or int(c) == 0:
continue
for c in str(j):
mult_set.add(c)
k = i//j
for c in str(k):
if c in digits or int(c) == 0:
continue
for c in str(k):
mult_set.add(c)
if len(mult_set ) == 9:
sum_pan += j + k + i
return sum_pan
def main():
assert(is_pandigital('15234') == True )
assert(is_pandigital('15233') == False )
print(find_pandigital())
print('Tests Passed!')
if __name__ == '__main__':
main()