How to resolve the algorithm Gapful numbers step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gapful numbers step by step in the ALGOL 68 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.

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

About   7.46%   of positive integers are   gapful.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Gapful numbers step by step in the ALGOL 68 programming language

Source code in the algol programming language

BEGIN # find some gapful numbers - numbers divisible by f*10 + b #
      # where f is the first digit and b is the final digit      #
    # unary GAPFUL operator - returns TRUE  if n is gapful       #
    #                                 FALSE otherwise            #
    OP   GAPFUL = ( INT n )BOOL:
         BEGIN
            INT abs n  = ABS n;
            INT back   = abs n MOD 10; # final digit             #
            INT front := abs n OVER 10;
            WHILE front > 9 DO front OVERAB 10 OD;
            abs n MOD ( ( front * 10 ) + back ) = 0
         END; # GAPFUL #
    # dyadic GAPFUL operator - returns an array of n gapful      #
    # numbers starting from first                                #
    PRIO GAPFUL = 9;
    OP   GAPFUL = ( INT n, INT first )[]INT:
         BEGIN
            [ 1 : n ]INT result;
            INT g pos := 0;
            FOR i FROM first WHILE g pos < n DO
                IF GAPFUL i THEN result[ g pos +:= 1 ] := i FI
            OD;
            result
         END; # GAPFUL #
    # prints a sequence of gapful numbers                        #
    PROC print gapful = ( []INT seq, INT start )VOID:
         BEGIN
            print( ( "First ", whole( ( UPB seq + 1 ) - LWB seq, 0 )
                   , " gapful numbers starting from ", whole( start, 0 )
                   , ":", newline
                   )
                 );
            FOR i FROM LWB seq TO UPB seq DO print( ( " ", whole( seq[ i ], 0 ) ) ) OD;
            print( ( newline ) )
         END; # print gapful #
    print gapful( 30 GAPFUL            100,           100 );
    print gapful( 15 GAPFUL      1 000 000,     1 000 000 );
    print gapful( 10 GAPFUL  1 000 000 000, 1 000 000 000 )
END

  

You may also check:How to resolve the algorithm Roots of a function step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the Objeck programming language
You may also check:How to resolve the algorithm Get system command output step by step in the Wren programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Lasso programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Processing programming language