How to resolve the algorithm Long multiplication step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Long multiplication step by step in the D programming language

Table of Contents

Problem Statement

Explicitly implement   long multiplication.
This is one possible approach to arbitrary-precision integer algebra.

For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: The output of   264 * 264   is   2128,   and is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long multiplication step by step in the D programming language

Source code in the d programming language

void main() {
    import std.stdio, std.bigint;

    writeln(2.BigInt ^^ 64 * 2.BigInt ^^ 64);
}


import std.stdio, std.algorithm, std.range, std.ascii, std.string;

auto longMult(in string x1, in string x2) pure nothrow @safe {
    auto digits1 = x1.representation.retro.map!q{a - '0'};
    immutable digits2 = x2.representation.retro.map!q{a - '0'}.array;
    uint[] res;

    foreach (immutable i, immutable d1; digits1.enumerate) {
        foreach (immutable j, immutable d2; digits2) {
            immutable k = i + j;
            if (res.length <= k)
                res.length++;
            res[k] += d1 * d2;

            if (res[k] > 9) {
                if (res.length <= k + 1)
                    res.length++;
                res[k + 1] = res[k] / 10 + res[k + 1];
                res[k] -= res[k] / 10 * 10;
            }
        }
    }

    //return res.retro.map!digits;
    return res.retro.map!(d => digits[d]);
}

void main() {
    immutable two64 = "18446744073709551616";
    longMult(two64, two64).writeln;
}


  

You may also check:How to resolve the algorithm Humble numbers step by step in the PL/I programming language
You may also check:How to resolve the algorithm Giuga numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the F# programming language
You may also check:How to resolve the algorithm URL encoding step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the PureBasic programming language