How to resolve the algorithm Palindromic gapful numbers step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindromic gapful numbers step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Numbers   (positive integers expressed in base ten)   that are (evenly) divisible by the number formed by the first and last digit are known as   gapful numbers.

Evenly divisible   means divisible with   no   remainder.

All   one─   and two─digit   numbers have this property and are trivially excluded.   Only numbers   ≥ 100   will be considered for this Rosetta Code task.

1037   is a   gapful   number because it is evenly divisible by the number   17   which is formed by the first and last decimal digits of   1037.

A palindromic number is   (for this task, a positive integer expressed in base ten),   when the number is reversed,   is the same as the original number.

For other ways of expressing the (above) requirements, see the   discussion   page.

All palindromic gapful numbers are divisible by eleven.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindromic gapful numbers step by step in the FreeBASIC programming language

Source code in the freebasic programming language

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)

  

You may also check:How to resolve the algorithm Random numbers step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the zkl programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the D programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Clojure programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Common Lisp programming language