August 2, 2011
(Back to the Previous Post in the Series) (Forward to the Next Post in the Series)
In the previous article in this series we tried to develop different methods for identifying special numbers between 1 and 1,000,000, such that the number formed by reversing the order of the digits will evenly divide into the original number. I initially predicted that there would be at least three unique solutions provided before the undocumented REVERSE function was mentioned. A fellow Oak Table member quickly provided a solution using the REVERSE function, which happens to be the fastest of the various approaches. Another reader noticed a pattern that helps find those special numbers – if that pattern identifies all of the special numbers, it is easily the most efficient approach to finding these special numbers.
Today’s blog article also is on the topic of finding special numbers, determined as special after changing the order of the number’s digits. I used my fancy, onpoint introduction for this article in the earlier article (letters on the dice, trying to form words from those letters), so I thought that I would jump the little pony over the edge of the cliff in search of another introduction (there are probably five readers with an odd image stuck in their head at this moment). ;)
—
Have you ever noticed the disparity between the Environment Protection Agency’s (EPA or the equivalent in other countries) estimates for fuel efficiency and the actual fuel efficiency calculated with the hand held calculator? Take for instance a car that I own – that car has never achieved the EPA’s estimated fuel efficiency rating. A 412 horsepower (at the flywheel as claimed by the manufacturer, other tests have concluded that it might be higher) V8 achieving 25 miles per U.S. gallon (MPG) of fuel?
Time for the Oracle Database test case. On a frigid 32 degree day (in Celsius, that is a hot 90 degrees Fahrenheit), I lowered the windows, topped off the fuel tank with premium grade fuel, reset the car’s fuel efficiency gauge (the MPG gauge… oddly, this gauge always reads 6% to 8% lower than the actual fuel efficiency), and then set out on a roughly 117 mile (188 KM) drive down the highway with the radar detector on the dash board. While I have heard that the car’s speed limit is set at the factory to roughly 160 miles per hour (258 KM/H), the posted speed limit was typically 55 MPH (miles per hour) except in the roughly halfdozen or so cities along the way. The car never did hit the EPA’s estimated fuel efficiency rating. Somebody was playing with the numbers a bit, I think. :)








Along the route selected for the 117 mile test loop, I captured cell phone pictures showing the car’s fuel efficiency gauge (MPG gauge). Below are the cell phone pictures captured at 15 miles into the drive, roughly half way through the drive, and near the end of the 117 mile drive.
So, how does Oracle Database play a part in the introduction to this blog article? Let’s take a guess at the car’s actual fuel efficiency using a SQL statement:
SELECT 29.5 CAR, ROUND(29.5 * 1.06, 2) LOW_ACTUAL, ROUND(29.5 * 1.08, 2) HIGH_ACTUAL FROM DUAL; CAR LOW_ACTUAL HIGH_ACTUAL    29.5 31.27 31.86
So much for EPA estimates – between 25.08% and 27.44% lower than the actual fuel efficiency. Does this test case then suggest that if we do not use all of the features that are available in a tool, that we are then more efficient than expected? I think that I need another test case to decide. ;)
Side note: For the tank of fuel when the car rolled over the 1,000 mile mark, the car achieved 28.75 miles per gallon while touring some of the sites around Michigan, specifically the hills and curves along the northwest shoreline of the lower peninsula (several of those hills were steep – for one hill in particular the car downshifted three gears to maintain speed going down the hill). The car’s overall fuel efficiency since it left the factory is 25.58 MPG (excluding the 117 mile test case), so I guess that one could argue that if you TRUNC the fuel efficiency number, at the root the EPA is right. Make like a tree and leave (that was a movie quote from “Back to the Future”).
After returning from the test case I set out on my other 412 HP vehicle… cough, that would be a 4 cylinder 12 horsepower 1946 Farmall A tractor. Time to mow the lawn in second gear. Cutting a 6 foot (1.83 meter) wide path through grass that is up to 12 inches (30.48 cm) high, and capable of mowing down 2 inch diameter trees as necessary. Oh, the power of the horse…
—
—
—
OK, now that we have drifted far from the topic of this article and have had a chance to play with numbers in the long introduction, on to the challenge. Consider the four digit integer numbers between 1,000 and 9,999. There are 4! (1 * 2 * 3 * 4 = 24) possible combinations of the four digits of each of these numbers. For example, consider the number 8712 – the 24 possible combinations are:
1278 1287 1728 1782 1827 1872 2178 2187 2718 2781 2817 2871 7128 7182 7218 7281 7812 7821 8127 8172 8217 8271 8712 8721
The objective is to find all combinations of the four digit numbers between 1,000 and 9,999 that evenly divide into the original number. For the above example with the original number 8712, the combination 2178 evenly divides into 8712. Thus, the output would include:
ORIGINAL COMBINATION   8712 2178
The restrictions:
 The four digit original numbers between 1,000 and 9,999 should not be included in the output if the last digit of those numbers is 0 – the combination number may end with 0 if the original number included a 0 digit. Thus, do not include the original numbers 1000, 1010, 1020, 1030, 1040, etc.
 The original value cannot be equal to the combination value (in the above example, it is not valid to output the combination value 8712).
 Each original number and combination pair should be output at most one time.
 You may not reuse a digit position twice; all digit positions must be used once. For example, the number 7141 includes the digit 1 twice, therefore, all generated combinations of that number must include two number 1 digits.
This challenge is a bit more difficult than was the challenge in part 1 of this series. The greatest difficulty likely involves creating the 23 valid digit combinations (there are 24, but one of those combinations will always be invalid) of the four digit numbers.
The problem is solvable – will your solution be the same as mine? I will give the readers a couple of days to ponder the thoughts of why a horse entered a race between a car and a tractor before sharing my approach to solving the problem.
Here is my attempt … as mentioned in the code, Anagram credit goes to Laurent. Due to use of pl/sql, this is not a puresql solution but atleast an attempt.
I think i am missing atlast one more combinations (9009 and 0099), will check my code (it is wip).
sorry about incovenience. here are the original 2 replies (unaltered):
1:
2:
p.s. pls delete the older ones
There have been a couple of possible solutions posted so far, and there has been no more suggestions posted in the last 36 hours, so I thought that I would share the solution that I developed.
First, we need to find a way to build all 24 combinations of the 4 digits in the 4 digit numbers. For instance, we might arrange the 4 digits into the 24 combinations using a SQL statement like the following (the numbers represent the original positions of the digits):
Now for the easy part, use the above matrix to simply pull out the digits in the specified order from the list of numbers between 1000 and 9999:
Any other solutions?
what about a very short one? (disregard the timing :) ) :
I didn’t succeed right now to have a pure SQL solution. I have however created a pipelined function giving me the four digit combinations as follows
and have started figuring out how to use this function with the original numbers selected as follows
I Still have not found the solution
Mohamed,
Good idea to try a pipelined function. I have not worked with those very much.
As an experiment, I quickly put together an Excel macro/Visual Basic 6 program that accepts a number as input and outputs to the debug window all combinations of the digits in that number:
Now let’s convert that VB 6 code into a function that will eventually return pipelined rows (is SYS.AQ$_MIDARRAY the correct type, I borrowed it from another example posted by someone to this blog – it appears that we *could* use this to return VARCHAR strings:
As can be seen by the above output, if we input the 4 digit number 1234, the function outputs all combinations of the digits 1234. It will work with numbers with 1, 2, 3, 4, 5, 6, 7, 8, 9, and etc. digits. Let’s fix the function so that it returns pipelined rows:
Now the interesting thing is that if we input 1234, we are able to use the first, second, third, and fourth characters in the returned values to determine how to reorganize the digits in the original numbers that are supplied by the CONNECT BY syntax:
I am not sure if the above helps – maybe someone can figure out how to directly feed the numbers from the N inline view into the COMBINATIONS function.