Jan 93 Challenge
 Volume Number: 9 Issue Number: 1 Column Tag: Challenge

# Programmers' Challenge

By Mike Scanlin, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying this article are located on MacTech CD-ROM or source code disks.

## Challenge of the Month: Travelling Salesman

Recent discussions with friends about fuel efficiency reminded me of a problem I once had in a linear algebra class: A travelling salesman needs to visit a set of cities. He can go from any city to any other city and must visit each city exactly once. The question is, what is the optimal order of visitation that minimizes total distance travelled?

The prototype of the function you write is:

```void OptimalPath(numCities,
startCityIndex, citiesPtr,
optimalPathPtr)
unsigned short numCities;
unsigned short  startCityIndex;
Point *citiesPtr;
Point *optimalPathPtr;
Example:

```

## Input:

``` numCities = 6
startCityIndex = 2
*citiesPtr = {1,4}, {2,2}, {2,1}, {5,7}, {4,6}, {2,6}

```

## Output:

``` *optimalPathPtr = {2,1}, {2,2}, {1,4}, {2,6}, {4,6}, {5,7}

```

numCities is a 1-based count of the number of cities to be visited (between 3 and 20, inclusive); startCityIndex is a zero-based index of the city in which the salesman starts ((2,1) in this example); citiesPtr is a pointer to an array of city locations (x and y locations are positive integers from 0 to 32767; each entry in the array is unique - no duplicate cities); optimalPathPtr points to an array (allocated by the caller) that OptimalPath fills in with the coordinates of the cities that the salesman should visit, in the order he should visit them (the first entry will always be citiesPtr[startCityIndex]).

## October Challenge winner!

The winner of the October “Name no one man” palindrome challenge is Stepan Riha (location unknown). His superb solution is overflowing with neat optimization tricks, starting from a good understanding of the problem at hand (right algorithm) and continuing on to efficient implementation details like precomputed multiplication tables (right code). This is a perfect example of what we’re looking for here in the Programmer’s Challenge column. Nice job, Stepan!

Credit is also given to three people who submitted solutions that were relatively close to Stepan’s winning time and code size: Leonid Braginsky (22% slower; Brighton, MA), Allen Stenger (37% slower; Gardena, CA) and Eric Slosser (40% slower; Berkeley, CA).

To those people who submitted solutions that use a precomputed lookup table containing every palindrome from 0 to 999,999,999 I have three words: Get a life. I can hear the moaning from here, “That’s not fair! He said the primary criteria was speed!” Well, that’s true, it is. However, there comes a point in programming when you have to sacrifice speed for reasonable code size and/or memory usage. It is my personal opinion that taking 30K+ to do what Stepan does in 426 bytes is not the right approach.

I have received several requests for statistics regarding the Programmer’s Challenge. Readers want to know how many entries I get, what test data I use, what the winning time and code size is, etc. I can and will satisfy some of those requests. Unfortunately, I can’t publish my test bench program or test data because of space constraints. But I can tell you that I generally test with several hand calculated values to verify that functions are returning correct results. Then I call each function several thousand times with random (same random seed for everyone; it’s fair) as well as average case inputs. I test edge cases but I don’t dwell on these. I’m not trying to trick people (a couple of people asked me about negative palindromes since I used long instead of unsigned long in my baseNumber parameter-it was an oversight on my part, not a trick); I’m just looking for good, fast code that handles a wide range of inputs.

Having said that, here are some numbers that I can share with you: The “pegs” challenge received 9 entries, “spell CAT” received 21 entries and “palindromes” received 22 entries. Of the 22 entries for palindromes, one gave bogus results, one gave a divide-by-zero error and three others took way too long (or were caught in some kind of infinite loop) so I stopped timing them. Of the remaining entries, the slowest one was 4.3 times slower than the winner and the largest one that didn’t use a huge lookup table was 2.8 times larger than the winner.

## Name No one man

```/* 1 */
/* FindNthPalindrome by Stepan Riha
*
* This algorithm works with the following
* fact in mind: An N-digit string can form
* 9 * 10^((N-1)/2) palindromes:
*
* Odd N: Even N:
* 1st  100..000..001100..0000..001
* 2nd  100..010..001100..0110..001
* 3rd  100..020..001100..0220..001
* 100.......001 100........001
* 10th 100..090..001100..0990..001
* 11th 100..101..001100..1001..001
* 12th 100..111..001100..1111..001
* ............. ..............
* last-1 999..989..999   999..9889..999
* last 999..999..999999..9999..999
*
* Speedups are achieved bye replacing x*10
* with (x<<1 + x<<3) and by doing conversions
* requireing x/10 and x%10 on my own
* approximating 1/10 with 3/32. Also, I use
* a lookup table for powers of 10, etc.
*/

/* Precomputed values:
palins10 is an array of 9*10^((n-1)/2)
multpow10is a multiplication table of
10^n (last row is all 10^9
because of integer overflow)
*/

static long palins10[] = {
9L, 9L, 90L, 90L, 900L, 900L, 9000L,
9000L, 90000L, 90000L, 900000L
};

static long multpow10[100] = {
0L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L,
0L, 10L, 20L, 30L, 40L,
50L, 60L, 70L, 80L, 90L,
0L, 100L, 200L, 300L, 400L,
500L, 600L, 700L, 800L, 900L,
0L, 1000L, 2000L, 3000L, 4000L,
5000L, 6000L, 7000L, 8000L, 9000L,
0L, 10000L, 20000L, 30000L, 40000L,
50000L, 60000L, 70000L, 80000L, 90000L,
0L, 100000L, 200000L, 300000L, 400000L,
500000L, 600000L, 700000L, 800000L,
900000L,
0L, 1000000L, 2000000L, 3000000L, 4000000L,
5000000L, 6000000L, 7000000L, 8000000L,
9000000L,
0L, 10000000L, 20000000L, 30000000L,
40000000L, 50000000L, 60000000L, 70000000L,
80000000L, 90000000L,
0L, 100000000L, 200000000L, 300000000L,
400000000L, 500000000L, 600000000L,
700000000L, 800000000L, 900000000L,
0L, 1000000000L, 1000000000L, 1000000000L,
1000000000L, 1000000000L, 1000000000L,
1000000000L, 1000000000L, 1000000000L
};

/* (x * 10) */
#define Times10(x) ((x<<1) + (x<<3))
/* x *= 10 */
#define MultBy10(x) x += x + (x<<3)
/* (x * 3 / 32) */
#define ApproxDiv10(x) ((x>>4)+(x>>5))
/* (x/2) */
#define Half(x) ((x)>>1)

long FindNthPalindrome(long baseNumber, short n);

long FindNthPalindrome(register long base, short n)
{
register long num, dig, mult10, remain,
*lop, *hip, *multpow;
long   buffer[16];

if (base >= 1000000000L)
return -1L;

if (!(num = n))
/* 0th palindrome doesn't exist */
return base;

/* palindrome =
*     n-th palindrome > baseNumber */

/* Find dig = (number of digits in
* baseNumber) - 1
* Find how many dig-digit palindromes
* are smaller than the baseNumber
*/

if (base < 10) { /* baseNumber <= 9 */
dig = 0;
/* all 1-digit strings are
* palindromes (duh!)
*/
num += base - 1;
}
else { /* baseNumber is >= 10 */
/* Convert baseNumber to string.
* This is faster than using /1
* and %10
*/
lop = buffer; dig = -1;
while (base) {
remain = base; base = 0;
goto division1;
while (mult10) {
base += mult10;
remain -= Times10(mult10);
division1:
mult10 = ApproxDiv10(remain);
}
if (remain >= 10) {
base++; remain -= 10;
}
*lop++ = remain; dig++;
}
/* Find how many dig-digit palindromes
* are smaller than this string
* Start searching from the middle
*/
hip = (lop = buffer) + Half(dig+1);
lop += (base = Half(dig));
multpow = multpow10;
for(; base >= 0 && *lop==*hip;
hip++, lop--, multpow += 10, base--)
num += multpow[*hip];
if (base >= 0) {
if (*hip > *lop)
num--;
for(; base >= 0;
hip++, multpow += 10, base--)
num += multpow[*hip];
}
num -= multpow[-9];
} /* if (baseNumber < 10) */

/* palindrome =
*num-th palindrome > 10^dig */

/* Find how many digits the palindrome
* will have
*/
hip = (lop = palins10) + dig;
while (num >= 0)
num -= *hip++;
num += *(--hip);
dig = hip - lop;
if (dig >= 9)
/* i.e. baseNumber > 999999999
* (dig is always <= 10) */
return -1L;

/* palindrome =
*num-th dig-digit palindrome */

/* Find the num-th dig-digit paindrome.
* Calculate final palindrome string. This
* is faster than using /10 and %10
* Use lop and hip to point in the correct
* row in the multiplication table
* Start filling digits from the middle of
* the string
*/
base = Half(dig+1);
hip = (lop = multpow10) + Times10(base);
base = Half(dig);
lop += Times10(base);
base = 0L;
while (num) {
remain = num; num = 0;
goto division2;
while (mult10) {
num += mult10;
remain -= Times10(mult10);
division2:
mult10 = ApproxDiv10(remain);
}
if (remain >= 10) {
num++; remain -= 10;
}
base += lop[remain];
if (lop < hip)
/* don't count middle digit twice */
base += hip[remain];
lop -= 10; hip += 10;
}
lop = multpow10 + 1;
base += *lop;
if (dig) {
MultBy10(dig);
base += lop[dig];
}

return base;
}
```

## The Rules

Here’s how it works: Each month there will be a different programming challenge presented here. First, you must write some code that solves the challenge. Second, you must optimize your code (a lot). Then, submit your solution to MacTech Magazine (formerly MacTutor). A winner will be chosen based on code correctness, speed, size and elegance (in that order of importance) as well as the postmark of the answer. In the event of multiple equally desirable solutions, one winner will be chosen at random (with honorable mention, but no prize, given to the runners up). The prize for the best solution each month is \$50 and a limited edition “I won the MacTech Magazine Programming Challenge” T-shirt (not to be found in stores).{1}

In order to make fair comparisons between solutions, all solutions must be in ANSI compatible C. All entries will be tested with the FPU and 68020 flags turned off in THINK C. When timing routines, the latest version of THINK C will be used (with ANSI Settings plus “Honor ‘register’ first” and “Use Global Optimizer” turned on) so beware if you optimize for a different C compiler.

The solution and winners for this month’s Programmers’ Challenge will be published in the issue two months later. All submissions must be received by the 15th day of the month printed on the front of this issue.

All solutions should be marked “Attn: Programmers’ Challenge Solution” and sent to Xplain Corporation (the publishers of MacTech Magazine) via “snail mail” or e-mail. If you send via snail mail, please include a disk with the solution and all related files (including contact information). See page 2 for information on “How to Contact Xplain Corporation.”

MacTech Magazine reserves the right to publish any solution entered in the Programming Challenge of the Month and all entries are the property of MacTech Magazine upon submission. The submission falls under all the same conventions of an article submission.

{1} The prize is subject to change in the event of production problems.

Community Search:
MacTech Search:

Microsoft Remote Desktop 8.0.16 - Connec...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more
Spotify 1.0.4.90. - Stream music, create...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
djay Pro 1.1 - Transform your Mac into a...
djay Pro provides a complete toolkit for performing DJs. Its unique modern interface is built around a sophisticated integration with iTunes and Spotify, giving you instant access to millions of... Read more
Vivaldi 1.0.118.19 - Lightweight browser...
Vivaldi browser. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind that users are... Read more
Stacks 2.6.11 - New way to create pages...
Stacks is a new way to create pages in RapidWeaver. It's a plugin designed to combine drag-and-drop simplicity with the power of fluid layout. Features: Fluid Layout: Stacks lets you build pages... Read more
xScope 4.1.3 - Onscreen graphic measurem...
xScope is powerful set of tools that are ideal for measuring, inspecting, and testing on-screen graphics and layouts. Its tools float above your desktop windows and can be accessed via a toolbar,... Read more
Cyberduck 4.7 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Labels & Addresses 1.7 - Powerful la...
Labels & Addresses is a home and office tool for printing all sorts of labels, envelopes, inventory labels, and price tags. Merge-printing capability makes the program a great tool for holiday... Read more
teleport 1.2.1 - Use one mouse/keyboard...
teleport is a simple utility to let you use one single mouse and keyboard to control several of your Macs. Simply reach the edge of your screen, and your mouse teleports to your other Mac! The... Read more
Apple iMovie 10.0.8 - Edit personal vide...
With an all-new design, Apple iMovie lets you enjoy your videos like never before. Browse your clips more easily, instantly share your favorite moments, and create beautiful HD movies and Hollywood-... Read more

## Latest Forum Discussions

Use Batting Average and the Apple Watch...
Batting Average, by Pixolini, is designed to help you manage your statistics. Every time you go to bat, you can use your Apple Watch to track  your swings, strikes, and hits. [Read more] | Read more »
Celebrate Studio Pango's 3rd Annive...
It is time to party, Pangoland pals! Studio Pango is celebrating their 3rd birthday and their gift to you is a new update to Pangoland. [Read more] | Read more »
Become the World's Most Important D...
Must Deliver, by cherrypick games, is a top-down endless-runner witha healthy dose of the living dead. [Read more] | Read more »
SoundHound + LiveLyrics is Making its De...
SoundHound Inc. has announced that SoundHound + LiveLyrics, will be one of the first third-party apps to hit the Apple Watch. With  SoundHound you'll be able to tap on your watch and have the app recognize the music you are listening to, then have... | Read more »
Adobe Joins the Apple Watch Lineup With...
A whole tidal wave of apps are headed for the Apple Watch, and Adobe has joined in with 3 new ways to enhance your creativity and collaborate with others. The watch apps pair with iPad/iPhone apps to give you total control over your Adobe projects... | Read more »
Z Steel Soldiers, Sequel to Kavcom'...
Kavcom has released Z Steel Soldiers, which continues the story of the comedic RTS originally created by the Bitmap Brothers. [Read more] | Read more »
Seene Lets You Create 3D Images With You...
Seene, by Obvious Engineering, is a 3D capture app that's meant to allow you to create visually stunning 3D images with a tap of your finger, and then share them as a 3D photo, video or gif. [Read more] | Read more »
Lost Within - Tips, Tricks, and Strategi...
Have you just downloaded Lost Within and are you in need of a guiding hand? While it’s not the toughest of games out there you might still want some helpful tips to get you started. [Read more] | Read more »
The Petcube Camera is a device that lets you use live video to check in on your pet, talk to them, and play with them using a laser pointer - all while you're away. And the Petcube app is coming to the Apple Watch, so you'll be able to hang out with... | Read more »
Now You Can Manage Your Line2 Calls With...
You'll be able to get your Line2 cloud phone service on the Apple Watch very soon. The watch app can send and receive messages using hands-free voice dictation, or by selecting from a list of provided responses. [Read more] | Read more »

## Price Scanner via MacPrices.net

Intel Compute Stick: A New Mini-Computing For...
The Intel Compute Stick, a new pocket-sized computer based on a quad-core Intel Atom processor running Windows 8.1 with Bing, is available now through Intel Authorized Dealers across much of the... Read more
Heal to Launch First One-Touch House Call Doc...
Santa Monica, California based Heal, a pioneer in on-demand personal health care services — will offer the first one-touch, on-demand house call doctor app for the Apple Watch. Heal’s Watch app,... Read more
Mac Notebooks: Avoiding MagSafe Power Adapter...
Apple Support says proper usage, care, and maintenance of Your Mac notebook’s MagSafe power adapter can substantially increase the the adapter’s service life. Of course, MagSafe itself is an Apple... Read more
12″ Retina MacBook In Shootout With Air And P...
BareFeats’ rob-ART morgan has posted another comparison of the 12″ MacBook with other Mac laptops, noting that the general goodness of all Mac laptops can make which one to purchase a tough decision... Read more
FileMaker Go for iPad and iPhone: Over 1.5 Mi...
FileMaker has announced that its FileMaker Go for iPad and iPhone app has surpassed 1.5 million downloads from the iTunes App Store. The milestone confirms the continued popularity of the FileMaker... Read more
Sale! 13-inch 2.7GHz Retina MacBook Pro for \$...
Best Buy has the new 2015 13″ 2.7GHz/128GB Retina MacBook Pro on sale for \$1099 – \$200 off MSRP. Choose free shipping or free local store pickup (if available). Price for online orders only, in-... Read more
Minimalist MacBook Confirms Death of Steve Jo...
ReadWrite’s Adriana Lee has posted a eulogy for the “Digital Hub” concept Steve Jobs first proposed back in 2001, declaring the new 12-inch MacBook with its single, over-subscribed USB-C port to be... Read more
13-inch 2.7GHz Retina MacBook Pro for \$1234 w...
Adorama has the 13″ 2.7GHz/128GB Retina MacBook Pro in stock for \$1234.99 (\$65 off MSRP) including free shipping plus a free LG external DVD/CD optical drive. Adorama charges sales tax in NY & NJ... Read more
13-inch 2.5GHz MacBook Pro available for \$999...
Adorama has the 13-inch 2.5GHz MacBook Pro on sale for \$999 including free shipping plus NY & NJ sales tax only. Their price is \$100 off MSRP. Read more
Save up to \$600 with Apple refurbished Mac Pr...
The Apple Store is offering Apple Certified Refurbished Mac Pros for up to \$600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more

## Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Support Technician IV - Jack Henry a...
Job Description Jack Henry & Associates is seeking an Apple Support Technician. This position while acting independently, ensures the proper day-to-day control of Read more
*Apple* Client Systems Solution Specialist -...
…drive revenue and profit in assigned sales segment and/or region specific to the Apple brand and product sets. This person will work directly with CDW Account Managers Read more
*Apple* Software Support - Casper (Can work...
…experience . Full knowledge of Mac OS X and prior . Mac OSX / Server . Apple Remote Desktop . Process Documentation . Ability to prioritize multiple tasks in a fast pace Read more
*Apple* Software Support - Xerox Corporation...
…Imaging experience Full knowledge of Mac OS X and prior Mac OSX / Server Apple Remote Desktop Process Documentation Ability to prioritize multiple tasks in a fast pace Read more