-
Notifications
You must be signed in to change notification settings - Fork 1
/
euler52.py
71 lines (53 loc) · 1.65 KB
/
euler52.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# Permuted multiples
# ==================
# Problem 52
# It can be seen that the number, 125874, and its double, 251748, contain exactly
# the same digits, but in a different order.
#
# Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain
# the same digits.
# .. rubric:: Solution
# .. py:module:: euler52
# :synopsis: Permuted multiples
# We'll use :py:func:`euler04.digits`.
from euler04 import digits
# We'll determine if two numbers, represented as digit tuples,
# are permutations of each other.
# .. py:function:: isPermutation( a, b )
# Are the two sequences permutations of each other?
#
# :param a: sequence
# :param b: sequence
# :returns: True of they're permutations of each other.
def isPermutation( a, b ):
"""Is sequence *a* a permutation of sequence *b*?
>>> from euler52 import isPermutation
>>> n= 125874
>>> isPermutation( digits(n), digits(2*n) )
True
>>> isPermutation( digits(n), digits(3*n) )
True
"""
return sorted(a) == sorted(b)
# Test the module components.
def test():
import doctest
doctest.testmod(verbose=1)
# Compute the answer.
def answer():
for i in range(1,1000000000):
di= digits(i)
p = [ isPermutation( di, digits(k*i) ) for k in range(2,7) ]
if all(p):
return i
# Confirm the answer.
def confirm(answer):
assert answer == 142857, "{0!r} Incorrect".format(ans)
# Create some output.
if __name__ == "__main__":
test()
a= answer()
confirm(a)
print( "The smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain
" the same digits:", a )