Archive for May 22nd, 2009

BiggestPalindrome

May 22, 2009

Once again, I cheated by using Mathematica instead of one of the two sanctioned computing environments (Python or Sage). To test whether or not a number was a palindrome, I used Mathematica’s built-in abilities to convert an expression into a string, and to reverse the characters in any given string. Here is my code (it is based on an algorithm I remember Eric describing last week):

Palindrome[x_] := (y = ToString[x]; y == StringReverse[y]);

BiggestPalindrome[n_] := (x = 1;
  For[i = 10^n - 1, i > 10^(n - 1) - 1, --i,
   For[j = i, j > 10^(n - 1) - 1, --j, m = i*j;
    If[Palindrome[m] && m >= x, x = m]]]; x)

Typing BiggestPalindrome[n] will give the largest palindrome that is the product of two n-digit numbers.It took Mathematica about 5.75 seconds to compute the solution when n=3. I asked Mathematica to give me the answer for n=4, but I got tired of waiting for it to finish.

I’m sure there must be a way to cut down on the number of loops to go through. If there is, I’m sure somebody else will produce it.

Moving On

May 22, 2009

So, I was updating the sidebar links for next week’s problems, and looked ahead at the two after that as well, and thought, “We could easily do all of those in a week”. In my mind, one of them is trivial (lcm), and one is… extensive (factoring). That leaves two that seem pretty reasonable for a week (and, I hope, neither particularly difficult).

So let’s shoot for problems 3-6 next week. If I misjudged interest in these problems, we can always leave some on the bill for the following week as well.

Also, if you want to see messages like this in the future, about when the assignment gets updated, leave a comment below. Unless there’s some noteworthy change to the schedule, I’ll just keep updating the sidebar every Friday afternoon/evening (Saturday if it works out that way) without any more word on it.