RosettaCodeData/Task/Palindromic-gapful-numbers/FreeBASIC/palindromic-gapful-numbers.basic
2023-07-01 13:44:08 -04:00

59 lines
1.7 KiB
Text

function is_gapful( n as uinteger ) as boolean
if n<100 then return false
dim as string ns = str(n)
dim as uinteger gap = 10*val(mid(ns,1,1)) + val(mid(ns,len(ns),1))
if n mod gap = 0 then return true else return false
end function
function is_palindrome( n as uinteger ) as boolean
dim as string ns = str(n)
for i as uinteger = 1 to len(ns)\2
if mid(ns,i,1) <> mid(ns,len(ns)+1-i,1) then return false
next i
return true
end function
function padto( n as uinteger, s as integer ) as string
dim as string outstr=""
dim as integer k = len(str(n))
for i as integer = 1 to s-k
outstr = " " + outstr
next i
return outstr + str(n)
end function
sub print_range( yays() as uinteger, first as uinteger, last as uinteger)
dim as string outstr
for i as uinteger = first to last
outstr = padto(i,4)+" : "
for d as uinteger = 1 to 9
outstr += padto(yays(d,i), 11)
next d
print outstr
next i
end sub
#define is_yay(n) (is_gapful(n) and is_palindrome(n))
#define log10(n) log(n)*0.43429448190325182765112891891660508229
dim as uinteger yays(1 to 9, 1 to 1000), nyays(1 to 9), num = 99, fd
do
num += 1 : fd = val(left(str(num),1))
if fd = 0 then continue do 'no paligap will have 0 as leading digit
if nyays(fd) = 1000 then
num = (fd+1)*10^int(log10(num))
end if
if is_yay(num) then
nyays(fd) += 1
yays(fd, nyays(fd)) = num
end if
for y as uinteger = 1 to 9
if nyays(y) < 1000 then continue do
next y
exit do
loop
'excessive output requirements for such a simple task
print_range(yays(), 1, 20)
print_range(yays(), 86, 100)
print_range(yays(), 991, 1000)