Solution to Project Euler Problem 38: Pandigital multiples - Take the number 192 and multiply it by each of 1, 2, and 3: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3) The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5). What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?
Updated: Sept. 28, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 38: Pandigital multiples.
Difficulty: Easy.
Objective: Take the number 192 and multiply it by each of 1, 2, and 3:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?
Input: None.
Expected Output: 932718654.
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() {
int max = -1;
for (int n = 2; n <= 9; n++) {
for (int i = 1; i < pow(10, 9 / n); i++) {
String concat = "";
for (int j = 1; j <= n; j++)
concat += i * j;
if (isPandigital(concat))
max = Math.max(Integer.parseInt(concat), max);
}
}
return Integer.toString(max);
}
public static int pow(int x, int y) {
if (x < 0)
throw new IllegalArgumentException("Negative base not supported");
if (y < 0)
throw new IllegalArgumentException("Negative exponent");
int z = 1;
for (int i = 0; i < y; i++) {
if (Integer.MAX_VALUE / z < x)
throw new ArithmeticException("Overflow");
z *= x;
}
return z;
}
private static boolean isPandigital(String s) {
if (s.length() != 9)
return false;
char[] temp = s.toCharArray();
Arrays.sort(temp);
return new String(temp).equals("123456789");
}
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.
#
def compute():
ans = ""
for n in range(2, 10):
for i in range(1, 10**(9 // n)):
s = "".join(str(i * j) for j in range(1, n + 1))
if "".join(sorted(s)) == "123456789":
ans = max(s, ans)
return ans
if __name__ == "__main__":
print(compute())