How to resolve the algorithm Tau function step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Tau function step by step in the D programming language

Table of Contents

Problem Statement

Given a positive integer, count the number of its positive divisors.

Show the result for the first   100   positive integers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Tau function step by step in the D programming language

Source code in the d programming language

import std.stdio;

// See https://en.wikipedia.org/wiki/Divisor_function
uint divisor_count(uint n) {
    uint total = 1;
    // Deal with powers of 2 first
    for (; (n & 1) == 0; n >>= 1) {
        ++total;
    }
    // Odd prime factors up to the square root
    for (uint p = 3; p * p <= n; p += 2) {
        uint count = 1;
        for (; n % p == 0; n /= p) {
            ++count;
        }
        total *= count;
    }
    // If n > 1 then it's prime
    if (n > 1) {
        total *= 2;
    }
    return total;
}

void main() {
    immutable limit = 100;
    writeln("Count of divisors for the first ", limit, " positive integers:");
    for (uint n = 1; n <= limit; ++n) {
        writef("%3d", divisor_count(n));
        if (n % 20 == 0) {
            writeln;
        }
    }
}


  

You may also check:How to resolve the algorithm DNS query step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Call a function step by step in the J programming language
You may also check:How to resolve the algorithm Entropy step by step in the SETL programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the TXR programming language