Project Euler Problem 52 Solution


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.


import Data.List (sort)

main :: IO ()
main = print $ head [n | n <- [1..], all ((== (sort . show) n) . (sort . show) . (n*)) [1..6]]
$ ghc -O2 -o permuted-multiples permuted-multiples.hs
$ time ./permuted-multiples
real   0m0.212s
user   0m0.212s
sys    0m0.000s


#!/usr/bin/env python

def get_digits(n):
    return sorted(str(n))

def same_digits(n, iterations=2):
    digits = get_digits(n)
    for i in range(1, iterations):
        if not digits == get_digits(n+(i*n)):
            return False
    return True

def main():
    n = 0
    found = False
    while not found:
        n += 1
        if same_digits(n,6):
            found = True

if __name__ == "__main__":
$ time python3
real   0m0.299s
user   0m0.296s
sys    0m0.000s