Question
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.
Haskell
import Data.List (sort)
main :: IO ()
= print $ head [n | n <- [1..], all ((== (sort . show) n) . (sort . show) . (n*)) [1..6]] main
$ ghc -O2 -o permuted-multiples permuted-multiples.hs
$ time ./permuted-multiples
real 0m0.112s
user 0m0.112s
sys 0m0.000s
Python
#!/usr/bin/env python
def get_digits(n):
return sorted(str(n))
def same_digits(n, iterations=2):
= get_digits(n)
digits for i in range(1, iterations):
if not digits == get_digits(n+(i*n)):
return False
return True
def main():
= 0
n = False
found while not found:
+= 1
n if same_digits(n,6):
= True
found print(n)
if __name__ == "__main__":
main()
$ time python3 same-digits.py
real 0m0.427s
user 0m0.426s
sys 0m0.000s