Solution to Project Euler Problem 52: Permuted multiples - 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.
Updated: Jan. 17, 2021 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 52: Permuted multiples.
Difficulty: Easy.
Objective: 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.
Input: None.
Expected Output: 142857.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
import java.util.Arrays;
public class SikademyEulerSolution {
public String run() {
for (int i = 1; ; i++) {
if (i > Integer.MAX_VALUE / 6)
throw new ArithmeticException("Overflow");
if (multiplesHaveSameDigits(i))
return Integer.toString(i);
}
}
private static boolean multiplesHaveSameDigits(int x) {
for (int i = 2; i <= 6; i++) {
if (!Arrays.equals(toSortedDigits(x), toSortedDigits(i * x)))
return false;
}
return true;
}
private static char[] toSortedDigits(int x) {
char[] result = Integer.toString(x).toCharArray();
Arrays.sort(result);
return result;
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
import itertools
def compute():
cond = lambda i: all(sorted(str(i)) == sorted(str(j * i)) for j in range(2, 7))
ans = next(i for i in itertools.count(1) if cond(i))
return str(ans)
if __name__ == "__main__":
print(compute())