This a way to generate the
wonderland number.
It has six digits.
If you multiply it by 2,3,4,5, or
6, the resulting number has all the same digits in at as the original number.
The only difference is the
position that they are in.
import
java.util.ArrayList;
import java.util.List;
public class
WonderLandNumber {
public static void main(String[] args) {
for (int i = 100000; i < 1000000; i++) {
if (isWonderLandNumber(i)) {
System.out.println(i);
break;
}
}
}
public static boolean
isWonderLandNumber(int number) {
return containsAll(number, 2 * number) && containsAll(number, 3 * number) && containsAll(number, 4 * number)
&&
containsAll(number, 5 * number) && containsAll(number, 6 * number);
}
public static boolean containsAll(int number, int multiple) {
String
one = String.valueOf(number);
String
two = String.valueOf(multiple);
return one.length() == two.length()
&& getDigits(one).containsAll(getDigits(two));
}
public static
List getDigits(String number) {
List
digits = new
ArrayList<>();
for (String digit : number.split("")) {
digits.add(digit);
}
return digits;
}
}
Output:
142857
No comments:
Post a Comment