# Permuted multiples (Euler Problem 52)¶

Back to overview.

https://projecteuler.net/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.

In [56]:

    from functools import reduce
    from operator import eq

    def get_length(n):
        """ Returns the number of digits for n.
            Returns wrong result for n = 0. """
        length = 0
        while n:
            length += 1
            n //= 10
        return length

    assert(get_length(100) == 3)

    def get_digits(n):
        d = []
        while n:
            d.append(n % 10)
            n //= 10
        return d

    assert(get_digits(213) == [3, 1, 2])

    def get_digits_count(n):
        digits_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        for d in get_digits(n):
            digits_count[d] += 1
        return digits_count

    assert(get_digits_count(1258744) == [0, 1, 1, 0, 2, 1, 0, 1, 1, 0])

In [64]:

    def check_if_multiples_contain_same_digits(n, max_multiple=2):
        n_digits_count = get_digits_count(n)
        n_length = get_length(n)
        n_multiple = n * max_multiple
        while n != n_multiple:
            if n_length != get_length(n_multiple) or n_digits_count != get_digits_count(n_multiple):
                return False
            n_multiple -= n
        return True

    assert(check_if_multiples_contain_same_digits(125874))
    assert(check_if_multiples_contain_same_digits(125875) == False)

In [70]:

    for i in range(1, 10000000):
        if check_if_multiples_contain_same_digits(i, 6):
            s = i
            break

In [72]:

    print(s)
    assert(s == 142857)

    142857

In [ ]: