Nine digit positive integers of the form PQRSTUVWX are called four-six numbers if the digits P, Q, R and S (in this order) are in strictly ascending order of magnitude, while the digits S, T, U, V, W and X (in this order) are in strictly descending order of magnitude.
For example, each of 567876543 and 678986430 is a four-six number, but 567876654 is NOT a valid four-six number since the digits 876654 (in this order) are not in strictly descending order of magnitude. Similarly, 678875432 is NOT a valid four-six number since the digits 6788 (in this order) are not in strictly ascending order of magnitude.
All the possible four-six numbers are now arranged in descending order of magnitude.
What is the 4664th number?
Note: No four-six number can contain leading zeroes.
FOR a = 1 TO 6
FOR b = a + 1 TO 7
FOR c = b + 1 TO 8
FOR d = c + 1 TO 9
FOR e = 4 TO d - 1
FOR f = 3 TO e - 1
FOR g = 2 TO f - 1
FOR h = 1 TO g - 1
FOR i = 0 TO h - 1
ct = ct + 1
IF ct < 20 OR ct > 4645 THEN
PRINT ct, a; b; c; d; e; f; g; h; i
END IF
IF ct = 4664 THEN GOTO version2
NEXT
NEXT
NEXT
NEXT
NEXT
NEXT
NEXT
NEXT
NEXT
finds (to show the first 19, and the last few up to and including the 4664th):
1 1 2 3 5 4 3 2 1 0
2 1 2 3 6 4 3 2 1 0
3 1 2 3 6 5 3 2 1 0
4 1 2 3 6 5 4 2 1 0
5 1 2 3 6 5 4 3 1 0
6 1 2 3 6 5 4 3 2 0
7 1 2 3 6 5 4 3 2 1
8 1 2 3 7 4 3 2 1 0
9 1 2 3 7 5 3 2 1 0
10 1 2 3 7 5 4 2 1 0
11 1 2 3 7 5 4 3 1 0
12 1 2 3 7 5 4 3 2 0
13 1 2 3 7 5 4 3 2 1
14 1 2 3 7 6 3 2 1 0
15 1 2 3 7 6 4 2 1 0
16 1 2 3 7 6 4 3 1 0
17 1 2 3 7 6 4 3 2 0
18 1 2 3 7 6 4 3 2 1
19 1 2 3 7 6 5 2 1 0
...
4646 2 3 8 9 8 7 5 3 2
4647 2 3 8 9 8 7 5 4 0
4648 2 3 8 9 8 7 5 4 1
4649 2 3 8 9 8 7 5 4 2
4650 2 3 8 9 8 7 5 4 3
4651 2 3 8 9 8 7 6 1 0
4652 2 3 8 9 8 7 6 2 0
4653 2 3 8 9 8 7 6 2 1
4654 2 3 8 9 8 7 6 3 0
4655 2 3 8 9 8 7 6 3 1
4656 2 3 8 9 8 7 6 3 2
4657 2 3 8 9 8 7 6 4 0
4658 2 3 8 9 8 7 6 4 1
4659 2 3 8 9 8 7 6 4 2
4660 2 3 8 9 8 7 6 4 3
4661 2 3 8 9 8 7 6 5 0
4662 2 3 8 9 8 7 6 5 1
4663 2 3 8 9 8 7 6 5 2
4664 2 3 8 9 8 7 6 5 3
so the answer is 238987653.
|
Posted by Charlie
on 2008-03-16 16:03:15 |