' https://projecteuler.net/problem=4
' 906609
sub main()
top = 999
bottom = 100
firstFound = firstPalindrome( 99, 10 )
if firstFound[0] >= firstFound[1]
high = firstFound[0]
low = firstFound[1]
else
high = firstFound[1]
low = firstFound[0]
end if
firstFound = high*low
print "First Palindrome: "; firstFound, high, low
' Focus search on numbers greater than first pair/combination of i,j
max = maxPalindrome( top, low, firstFound )
print "Max: "; max
end sub
' Find first and return [i, j]
function firstPalindrome( high, low ) as object
done = false
for i = high to low step -1
for j = high to low step -1
p = i*j
if palindromic(p)
found = [i, j]
' print i, j, i*j
done = true
exit for
end if
end for
if done then exit for
end for
return found
end function
' Find max within range by going over all combinations
function maxPalindrome( high, low, knownMax ) as object
newMax = knownMax
for i = floor((high+low)/2) to high
for j = low to high
p = i*j
if palindromic(p)
if p > newMax
newMax = p
end if
end if
end for
end for
return newMax
end function
function palindromic(n) as boolean
n$ = n.toStr()
' Any single digit number is a palindrome.
if n$.len() = 1 return true
isPalindrome = true
' Two digits or more
' Compare
for i = 0 to int( n$.len() / 2 )
if n$.mid(i,1) <> n$.mid(n$.len()-1-i,1)
isPalindrome = false
exit for
end if
end for
return isPalindrome
end function
function floor(in)
return int(in)
end function
Comments
Join the discussionNo comments yet. Be the first to comment.
BrightScript
BrightAuthor plugin development with BrightScript, the scripting programming language for the BrightSign player.