I already know that if we have the digits 0 through 9 and if we want to make a number containing all those numbers without a repating digit so the total 10 digit numbers -no digit repeated- will be 3,628,800
so is there a way i could do this in JAVA ?
Copyright © 2024 Q2A.MX - All rights reserved.
Answers & Comments
Verified answer
// generate the digits, then shuffle them
ArrayList<Byte> list = new ArrayList<Byte>();
for (byte i = 0; i < 10; i++) {
list.add(i);
}
Collections.shuffle(list);
// parse the value. Use a long, since you can get overflow with 10-digit ints.
String s = "";
for (Byte b : list) {
s += b;
}
Long lg = Long.parseLong(s);
Your best bet, and most impressive to your teacher will be to use recursion for this one. It actually sounds like a fun assignment. If I wasn't so swamped with my own work I would take a crack at it =) Good luck!
Hint: 10! is 3628800, that's 10 * 9 * 8 * ... * 3 * 2 * 1 in case you haven't had stats or discrete math. This one can actually be done with pencil and paper now that I think about it.
For the 1st digit, generate a random quantity in [0,a million), multiply via 8, and around right down to an integer. the end result would be in [0,8). comparable for digits 2 and 3. For the subsequent 3 digits, take a random quantity in [0,a million) and multiply via 741, and around right down to integer. That integer represents the subsequent 3 digits.
declare a string "0123456789" and randomly swap their location.