Microsoft Small Basic

Largest palindrome

Modified: 2008/12/24 01:19 by admin - Categorized as: Samples
Problem 4

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers.


maxPalindrome = 0

For i = 990 To 100 Step -11 ' Palindromes are always multiples of 11
  For j = 999 To 100 Step -1
    product = i * j
    
    CheckPalindrome()
    If isPalindrome = "True" Then
      TextWindow.WriteLine("Considered " + product + "...")
      If product > maxPalindrome Then
        maxPalindrome = product
        Goto EndInnerLoop
      EndIf
    EndIf      
  EndFor
EndInnerLoop:
EndFor

TextWindow.WriteLine("Maximum Palindrome = " + maxPalindrome)

Sub CheckPalindrome
  isPalindrome = "False"
  
  productReverse = ""
  length = Text.GetLength(product)
  
  For k = 0 To length - 1
    subText = Text.GetSubText(product, k, 1)
    productReverse = Text.Append(subText, productReverse)
  EndFor
  
  If productReverse = product Then
    isPalindrome = "True"
  EndIf
EndSub

ScrewTurn Wiki version 2.0.35. Some of the icons created by FamFamFam.