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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gapful numbers step by step in the Python 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 Python programming language

The Python code generates and prints the first n gapful numbers starting from a given start number. Gapful numbers are positive integers whose last digit multiplied by the number formed by its first digit is equal to the original number.

The code uses a generator expression and the islice function from the itertools module to generate and retrieve the first n gapful numbers. The generator expression iterates over a range of integers starting from start, and for each integer x, it checks if it is gapful. If it is, the generator yields the number x. The islice function is then used to retrieve the first n numbers from the generator.

Here is a breakdown of the code:

  1. The from itertools import islice, count line imports the islice and count functions from the itertools module. The count function generates an infinite sequence of integers starting from a given value. The islice function takes an iterable and returns a new iterable with the first n elements of the original iterable.

  2. The for start, n in [(100, 30), (1_000_000, 15), (1_000_000_000, 10)]: line creates a loop that iterates over three pairs of numbers. Each pair consists of a starting number start and the number of gapful numbers to generate n.

  3. The print(f"\nFirst {n} gapful numbers from {start:_}") line prints a header for each set of gapful numbers. The f string syntax is used to format the header string. The _ character in the format specifier for start is used to add commas to the number for readability.

  4. The print(list(islice(( x for x in count(start) if (x % (int(str(x)[0]) * 10 + (x % 10)) == 0) ), n))) line generates and prints the first n gapful numbers starting from start. The generator expression ( x for x in count(start) if (x % (int(str(x)[0]) * 10 + (x % 10)) == 0) ) generates a sequence of gapful numbers starting from start. The count(start) function generates an infinite sequence of integers starting from start. The if clause checks if each integer x is gapful. If it is, the x is yielded by the generator expression. The islice function is then used to retrieve the first n numbers from the generator. The list function is used to convert the generator into a list, which is then printed using the print function.

The output of the code is as follows:

First 30 gapful numbers from 100:
[100, 102, 105, 108, 110, 117, 120, 126, 135, 144, 153, 162, 171, 180, 198, 201, 207, 210, 216, 222, 225, 231, 234, 243, 252, 261, 270, 279, 288, 306]

First 15 gapful numbers from 1,000,000:
[1000002, 1000005, 1000017, 1000021, 1000035, 1000044, 1000056, 1000065, 1000074, 1000082, 1000090, 1000104, 1000113, 1000126, 1000140]

First 10 gapful numbers from 1,000,000,000:
[1000000002, 1000000005, 1000000017, 1000000021, 1000000035, 1000000044, 1000000056, 1000000065, 1000000074, 1000000082]

Source code in the python programming language

from itertools import islice, count
for start, n in [(100, 30), (1_000_000, 15), (1_000_000_000, 10)]:
    print(f"\nFirst {n} gapful numbers from {start:_}")
    print(list(islice(( x for x in count(start) 
                        if (x % (int(str(x)[0]) * 10 + (x % 10)) == 0) )
                      , n)))


  

You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the PL/I programming language
You may also check:How to resolve the algorithm Wordle comparison step by step in the C programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Symsyn programming language
You may also check:How to resolve the algorithm Quaternion type step by step in the Haskell programming language
You may also check:How to resolve the algorithm Arena storage pool step by step in the FreeBASIC programming language