I Failed

Objective
25 years ago, a British computer magazine had a programming contest and this was one of the puzzles.

There are a large number of 9 digit integers in the range 123456789 to 987654321 where each digit only appears once. What is the 100,000th number in this sequence?

Example
The first number is 123456789, the second is 123456798, the third is 123456879 and so on. No digit can repeat so 122345675 is not a valid number in this sequence.

The problem was "Write a program in Ansi C, C++ or C# 2.0 (C# 1.0 is allowed) that outputs the 100,000th number as fast as possible. Use any algorithm, except you cannot pre-calculate the answer and then write a program that just prints the result. Your entry must calculate the number!".

Results
The correct answer was 358926471

i tried this in Java. i got 147028357 and it took about 20 secs or so to get that number.

the faster in c++ was 0.00000050521 secs

Heres the code i wrote.. and i did it all by myself :) i learnt alot about the language trying to figure this out. but still failed the challenge haha

there's obvioulsy something going wrong somewhere

Java Code:

import java.util.*;
public class Challenge1 {
public static void main(String[] args) {
int startNumber = 123456789;
int count = 0;
ArrayList numSplit = new ArrayList();
while(count < 100000){
int num = startNumber;
//fill each ArrayList element with a number from num
while (num > 0) {
int digit = num % 10;
numSplit.add(digit);
num /= 10;
}
// find the double ups. if it finds a double up it returns 0, count += 0 is the same
// so count only goes up when it doesn't find a double up(when it returns 1)
count += findDoubles(numSplit);
//adds one more to the initial number, i orignally used just num and then found out
// when i added to the ArrayList it didn't copy the number but removed from
// num and into ArrayList
startNumber++;
//empties the arrayList and starts the process again
numSplit.clear();
}
System.out.println(startNumber);
}
//looks through each element andcompares itself to each of the elements looking
// for double ups
public static int findDoubles(ArrayList n){
ArrayList numSplit = n;
for( int i = 0; i < numSplit.size(); i++ ){
for (int j = i+1; j < numSplit.size(); j++){
if(numSplit.get(i) == numSplit.get(j)){
return 0; // if double up leave loops and try next number
}
}
}
return 1;// if no double ups are found Add 1 to Count outside this method.
}
}

I once wrote a little article about all the fiddling. It's an extremely fast algoritm but an extremely clumsy implementation thereof.

kind regards,

Jos

The problem as written states that you can't just print the answer.
My “street read” of this is that you have to compute all permutations stopping at the 100,000th.
For example, is the following code a solution?
It runs in a fraction of a millisecond on a slow machine. It prints the correct answer:

The problem as written states that you can't just print the answer.
My “street read” of this is that you have to compute all permutations stopping at the 100,000th.
For example, is the following code a solution?
It runs in a fraction of a millisecond on a slow machine. It prints the correct answer: