3 ms·
I wrote a program to check how many 9 digit numbers met the criteria. The answer I got was 897,674,400. I used the following Java code, which may be flawed, to
by justin_oaks 9y ago
I wrote a program to check how many 9 digit numbers met the criteria. The answer I got was 897,674,400.
I used the following Java code, which may be flawed, to count:
public static void main(String[] args) throws Exception{
int count=0;
int[] digitCount=new int[10];
mainLoop:
for(int i=0; i<=999999999; i++){
Arrays.fill(digitCount,0);
digitCount[i / 1 %10]++;
digitCount[i / 10 %10]++;
digitCount[i / 100 %10]++;
digitCount[i / 1000 %10]++;
digitCount[i / 10000 %10]++;
digitCount[i / 100000 %10]++;
digitCount[i / 1000000 %10]++;
digitCount[i / 10000000 %10]++;
digitCount[i / 100000000 %10]++;
int differentNumbers=0;
for(int index=0; index<10; index++){
if(digitCount[index]>3){
continue mainLoop;
}
if(digitCount[index]>0){
differentNumbers++;
}
}
if(differentNumbers>4){
count++;
}
}
System.out.println("count: "+count);
}
- schoen 9y agoGood call on the leading zeroes. I was trying to write a Python program to do the same and I would have implicitly ignored leading zeroes, which should be considered here because a password could be, for example, 000123456.
- ThrustVectoring 9y agoThe analytic solution also isn't particularly difficult, just tedious to work through by hand. There's 10 valid ways to choose the first digit. You either repeat that digit one or zero times. If once, there are 8 ways to do this, and then you have the same sub-problem except with 9 remaining characters to use and 7 remaining places to fill. If none, there's one way to do it, and then you have the same problem except with 9 remaining characters to use and 8 remaining places to fill.
- soneil 9y agofwiw, I'd start the loop at i=1000000, on the assumption that anything less is going to fail the "three digits" test once zero-padded. A bit silly granted, but it'll let you ditch half the zero-padding. (Only half, but if they're going to be repeated n times, it might be worth it).