#rem
PICAXE BASIC Program: Palindrome_Generator_v1.bas
List all Palindromes representable in a 16‑bit unsigned integer(0–65535)
Counted 754 palindrome numbers in ascending order
Brute force method tests every integer
Really, Really SLOOOOW !
Time to Execute (FAST) = 209 Seconds
Time to Execute (SLOW) = 216 Seconds
Memory Used = 229 BYTES
rhb.20251231
#endrem
#picaxe 08M2
#no_data
setfreq m32
#terminal 38400
pause 3000 ; wait for terminal to start
symbol n = w0
symbol d0 = b2
symbol d1 = b3
symbol d2 = b4
symbol d3 = b5
symbol d4 = b6
symbol t = w4
main:
time = 0
n = 0
#rem
for word = 0 to 65535
bug(infinite loop) just the way integers work in a microcontroller
at 65535 when n is incremented the value rolls around to 0
a for/loop never ends
same for byte = 0 to 255 - byte variable rolls around to 0 - infinite loop
#endrem
do
gosub GetDigits ; FASTER
'gosub GetDigits2 ; SLOWER
gosub CheckPalindrome
if b7 = 1 then
sertxd(#n,cr,lf)
endif
inc n
loop until n = 0 ; 65535 when n is incremented the value rolls around to 0
t = time / 2 ; adjusted because of resonator speed 0.5 second per tick
sertxd("Elapsed Time: ",#t," seconds",cr,lf)
end
' ---[ Extract digits (PICAXE-safe math) FASTER ]---
GetDigits: ; @hippy | @westaust55 style coding found in forum for SPI/clocking bit banging?
d0 = n // 10
d1 = n / 10 // 10
d2 = n / 100 // 10
d3 = n / 1000 // 10
d4 = n / 10000 // 10
return
' ---[ Extract digits (built-in function) SLOWER ]---
GetDigits2:
BINTOASCII n, d4, d3, d2, d1, d0
return
' ---[ Palindrome check (result in b7) ]---
CheckPalindrome:
b7 = 0
if n < 10 then
b7 = 1
return
endif
if n < 100 then
if d0 = d1 then
b7 = 1
end if
return
endif
if n < 1000 then
if d0 = d2 then
b7 = 1
end if
return
endif
if n < 10000 then
if d0 = d3 and d1 = d2 then
b7 = 1
end if
return
endif
' 5-digit (up to 65535)
if d0 = d4 and d1 = d3 then
b7 = 1
end if
return
' ---[EOF]---