Mathematica 9 is now available

Programming Challenge

A Mathematica programming contest

Prepared and presented by Matthew Szudzik (mszudzik@wolfram.com)

Background

At 7 pm on 21 October 1999, participants in the 1999 Mathematica Developer conference were challenged to construct a function which can calculate the negabinary representation of any integer (see below).   Entries were due by noon the next day.  Of the 13 solutions which were received, two solutions caught the attention of the judges.  The first, by Mark Reeve, took a particularly original approach to the problem and performed its computations with far greater efficiency than any of the other entries.  The second, by Jarl Sobel, was written with unparalleled elegance while maintaining a respectable degree of efficiency.  These two solutions were deemed the winners of the contest.

The sections below contain the original statement of the challenge, the entries which were received before the deadline, an explanation of the criteria on which they were judged, and an exhibition of the winners.  After the deadline for the contest, a few especially stunning solutions were devised by Wolfram Research employees.  Although they were exempt from the contest, these solutions are included below.

Challenge

It is well known that every positive integer has a binary representation---that is, every positive integer can be written in base [Graphics:Images/winners_gr_1.gif] as a sequence of [Graphics:Images/winners_gr_2.gif]'s and [Graphics:Images/winners_gr_3.gif]'s.  In Mathematica, [Graphics:Images/winners_gr_4.gif] gives the binary representation of [Graphics:Images/winners_gr_5.gif].  For example, the binary representation of [Graphics:Images/winners_gr_6.gif] is [Graphics:Images/winners_gr_7.gif].

[Graphics:Images/winners_gr_8.gif]
[Graphics:Images/winners_gr_9.gif]

A number is found from its binary representation by adding together the corresponding powers of two.

[Graphics:Images/winners_gr_10.gif]
[Graphics:Images/winners_gr_11.gif]

Knowing this, it is easy to write a function that finds an integer, given its binary representation (in fact, Mathematica's built-in function [Graphics:Images/winners_gr_12.gif] does this automatically).

[Graphics:Images/winners_gr_13.gif]
[Graphics:Images/winners_gr_14.gif]
[Graphics:Images/winners_gr_15.gif]

It is not well known that every integer has a negabinary representation---that is, every integer can be written in base [Graphics:Images/winners_gr_16.gif] as a sequence of [Graphics:Images/winners_gr_17.gif]'s and [Graphics:Images/winners_gr_18.gif]'s.  A number is found from its negabinary representation by adding together the corresponding powers of negative two.  For example, the negabinary representation of [Graphics:Images/winners_gr_19.gif] is [Graphics:Images/winners_gr_20.gif].

[Graphics:Images/winners_gr_21.gif]
[Graphics:Images/winners_gr_22.gif]

Knowing this, it is easy to write a function that finds an integer, given its negabinary representation.

[Graphics:Images/winners_gr_23.gif]

So, the negabinary representation of [Graphics:Images/winners_gr_24.gif] is [Graphics:Images/winners_gr_25.gif] and the negabinary representation of [Graphics:Images/winners_gr_26.gif] is [Graphics:Images/winners_gr_27.gif].

[Graphics:Images/winners_gr_28.gif]
[Graphics:Images/winners_gr_29.gif]
[Graphics:Images/winners_gr_30.gif]
[Graphics:Images/winners_gr_31.gif]

Unfortunately, Mathematica's built-in function [Graphics:Images/winners_gr_32.gif] cannot automatically find the negabinary representation of an integer.

[Graphics:Images/winners_gr_33.gif]
[Graphics:Images/winners_gr_34.gif]
[Graphics:Images/winners_gr_35.gif]

Write a function [Graphics:Images/winners_gr_36.gif] that can find the negabinary representation of any integer [Graphics:Images/winners_gr_37.gif].  Solutions will be judged based on efficiency and elegance of code.

Entries

A

[Graphics:Images/winners_gr_38.gif]

B

[Graphics:Images/winners_gr_39.gif]

C

[Graphics:Images/winners_gr_40.gif]

D

[Graphics:Images/winners_gr_41.gif]
[Graphics:Images/winners_gr_42.gif]

E

[Graphics:Images/winners_gr_43.gif]

F

ToNegabinary[i_]:=
    Module[{d={},r=i,n=0},
        While[r != 0,
            PrependTo[d,Mod[r,2]];
            r = (r - Mod[r,2]*(-1)^n++)/2
        ];
        d /. {}->{0}
    ];    

G

[Graphics:Images/winners_gr_44.gif]
[Graphics:Images/winners_gr_45.gif]
[Graphics:Images/winners_gr_46.gif]
[Graphics:Images/winners_gr_47.gif]
[Graphics:Images/winners_gr_48.gif]

H

[Graphics:Images/winners_gr_49.gif]
[Graphics:Images/winners_gr_50.gif]

I

[Graphics:Images/winners_gr_51.gif]

J

[Graphics:Images/winners_gr_52.gif]
[Graphics:Images/winners_gr_53.gif]
[Graphics:Images/winners_gr_54.gif]
[Graphics:Images/winners_gr_55.gif]

K

[Graphics:Images/winners_gr_56.gif]

L

[Graphics:Images/winners_gr_57.gif]

M

[Graphics:Images/winners_gr_58.gif]

Judging

Entries to the 1999 Mathematica Developer Conference Programming Challenge were judged as follows.  First, each prospective solution [Graphics:Images/winners_gr_59.gif] was tested to verify that it gives the correct output for all integer inputs between -500 and 500.

[Graphics:Images/winners_gr_60.gif]

Any entry for which this test failed to output [Graphics:Images/winners_gr_61.gif] was disqualified.

Next, a large integer [Graphics:Images/winners_gr_62.gif] was randomly chosen.

[Graphics:Images/winners_gr_65.gif]

The efficiency of each remaining entry was judged by timing its output for this large input.

[Graphics:Images/winners_gr_66.gif]

Many entries produced recursion errors for large inputs, so it was necessary to set [Graphics:Images/winners_gr_67.gif] to [Graphics:Images/winners_gr_68.gif] in those cases.

[Graphics:Images/winners_gr_69.gif]

Lastly, the judges carefully looked at each entry and subjectively evaluated its elegance.

Winners

The Most Efficient Entry

Submitted by Mark Reeve (msreeve@earthlink.net).

[Graphics:Images/winners_gr_70.gif]

The Most Elegant Entry

Submitted by Jarl Sobel (jsobel@sdav01.seinf.abb.se).

[Graphics:Images/winners_gr_71.gif]

Other Solutions

Members of the Wolfram Research staff devised a number of notable solutions. To demonstrate that a working solution could be written by a participant with limited mathematical skill, Matthew Szudzik (mszudzik@wolfram.com) found a short algorithm which searches through all possible sequences of [Graphics:Images/winners_gr_72.gif]'s and [Graphics:Images/winners_gr_73.gif]'s until it finds the desired negabinary representation.

[Graphics:Images/winners_gr_74.gif]

David Librik found what is perhaps the ultimate solution---it is extremely efficient and elegant.

[Graphics:Images/winners_gr_75.gif]

Many of the programmers at Wolfram Research experimented with solutions written in different computer languages. Perhaps the most unusual language in which a solution was written is PostScript.  Brian Downing (bdowning@wolfram.com) exploited Mathematica's ability to follow commands written in PostScript, and wrote the following solution.  Note that it requires Mathematica's front end.

[Graphics:Images/winners_gr_76.gif]

References

See Donald E. Knuth's The Art of Computer Programming (volume 2): Seminumerical Algorithms for a discussion of negative base number representations.