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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindromic gapful numbers step by step in the Raku 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 Raku programming language

Source code in the raku programming language

constant @digits = '0','1','2','3','4','5','6','7','8','9';

# Infinite lazy iterator to generate palindromic "gap" numbers
my @npal = flat [ @digits ], [ '00','11','22','33','44','55','66','77','88','99' ],
  {
    sink @^previous, @^penultimate;
    [ flat @digits.map: -> \digit { @penultimate.map: digit ~ * ~ digit  } ]
  } … *;

# Individual lazy palindromic gapful number iterators for each start/end digit
my @gappal = (1..9).map: -> \digit {
    my \divisor = digit + 10 * digit;
    @npal.map: -> \this { next unless (my \test = digit ~ this ~ digit) %% divisor; test }
}

# Display
( "(Required) First 20 gapful palindromes:",              ^20, 7
  ,"\n(Required) 86th through 100th:",                 85..99, 8
  ,"\n(Optional) 991st through 1,000th:",            990..999, 10
  ,"\n(Extra stretchy) 9,995th through 10,000th:", 9994..9999, 12
  ,"\n(Meh) 100,000th:",                                99999, 14
).hyper(:1batch).map: -> $caption, $range, $fmt {
    my $now = now;
    say $caption;
    put "$_: " ~ @gappal[$_-1][|$range].fmt("%{$fmt}s") for 1..9;
    say round( now - $now, .001 ), " seconds";
}


  

You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the Quackery programming language
You may also check:How to resolve the algorithm First class environments step by step in the R programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the APL programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the C programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the PARI/GP programming language