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

Published on 12 May 2024 09:40 PM
#D

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

Source code in the d programming language

import std.conv;
import std.stdio;

string commatize(ulong n) {
    auto s = n.to!string;
    auto le = s.length;
    for (int i = le - 3; i >= 1; i -= 3) {
        s = s[0..i]
          ~ ","
          ~ s[i..$];
    }
    return s;
}

void main() {
    ulong[] starts = [cast(ulong)1e2, cast(ulong)1e6, cast(ulong)1e7, cast(ulong)1e9, 7123];
    int[] counts = [30, 15, 15, 10, 25];
    for (int i = 0; i < starts.length; i++) {
        int count = 0;
        auto j = starts[i];
        ulong pow = 100;
        while (true) {
            if (j < pow * 10) {
                break;
            }
            pow *= 10;
        }
        writefln("First %d gapful numbers starting at %s:", counts[i], commatize(starts[i]));
        while (count < counts[i]) {
            auto fl = (j / pow) * 10 + (j % 10);
            if (j % fl == 0) {
                write(j, ' ');
                count++;
            }
            j++;
            if (j >= 10 * pow) {
                pow *= 10;
            }
        }
        writeln("\n");
    }
}


  

You may also check:How to resolve the algorithm Password generator step by step in the OCaml programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the F# programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Function composition step by step in the WDTE programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the 6502 Assembly programming language