ODOMETER.BAS May 2000 by Marc Kummel aka Treebeard. Contact mkummel@rain.org, http://www.rain.org/~mkummel/ What is ODOMETER.BAS? ---------------------- ODOMETER is a small BASIC program I wrote to solve a tricky puzzle on my Stumpers webpage at : We've all been noticing our mileage with recent high gas prices. My car has two odometers that measure mileage. There's a six-digit counter that measures total miles, and a four-digit trip odometer that I can reset with a button. Recently, I noticed my odometer read [1 3 2 4 5 6]. Too bad the bottom was [5 4 9 0] instead of [7 8 9 0] (no tenths), not quite in order, but all the digits different at once! When should I have reset the trip odometer to make that happen? When should I next press reset so they'll all be different again as soon as possible? And the answer: If I had reset my trip odometer 7,890 miles ago, then my odometers would now read [1 3 2 4 5 6] and [7 8 9 0]. That's simple subtraction. To next see all the digits at once I should press reset at 132,478 miles. Then 478 miles later, I will see [1 3 2 9 5 6] and [0 4 7 8]. I like this kind of problem that is so easy to state, but I don't have a clue how to begin! There's no simple calculation that gives the answer. Trial and error works, but there is also a method or algorithm that finds the answer for any mileage. I first found this problem in the Programmer Of The Month (POTM) Contest archives at . The POTM site also includes C code and discussion of algorithms. It's a great site for finding tricky programming challenges. My program implements an algorithm to find the best solution that works by successively refining an initial worst-case guess. Paradoxically, the way to do this is to guess the largest number for the main odometer and the smallest number for the trip odometer. The method is to make guesses digit by digit from left to right, and refine the guess as you go along. The details are on my Stumper page. Using ODOMETER.BAS ----------------- ODOMETER is a simple DOS program. Run it in DOS, or a Command session in Windows. After it starts, the program will prompt you for a starting mileage (0 - 999999), and it will then figure when you should press reset. That's it. Just press at the prompt to quit. The Files: ----------- I wrote this program to solve a stumper. You'll have to study the source code for more info. I wrote it using MS Basic PDS v7.10, but it should work fine in any flavor of QBasic or Quick Basic. ODOMETER.EXE DOS executible to solve Reset stumper ODOMnn.BAS BASIC source code README.TXT This file TBVAULT.TXT Treebeard's Basic Vault info Conditions: ------------ This program and source code are yours to use and modify as you will, but they are offered as freeware with no warranty whatsoever. Give me credit, but do not distribute any changes under my name, or attribute such changes to me in any way. You're on your own! If you find this program or code useful, then let me know what you do with it, as a courtesy to a fellow programmer who still hacks for fun and glory. Any suggestions or comments will be much appreciated. Send comments and fixes to: Marc Kummel aka Treebeard mkummel@rain.org http://www.rain.org/~mkummel/ http://www.treebeard.org/ For more interesting Basic software with source code, check out Treebeard's Basic Vault at .