How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the C++ programming language

Table of Contents

Problem Statement

Consider the number 24. Its proper divisors are: 1, 2, 3, 4, 6, 8 and 12. Their product is 13,824 and the cube root of this is 24. So 24 satisfies the definition in the task title. Compute and show here the first 50 positive integers which are the cube roots of the product of their proper divisors. Also show the 500th and 5,000th such numbers. Compute and show the 50,000th such number. OEIS considers 1 to be the first number in this sequence even though, strictly speaking, it has no proper divisors. Please therefore do likewise.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Numbers which are the cube roots of the product of their proper divisors step by step in the C++ programming language

This program prints the first 50 numbers which are the cube roots of the products of their proper divisors.

The program uses the function divisor_count to calculate the number of divisors of a number. The function takes an unsigned integer n as input and returns the number of divisors of n.

The function divisor_count works by first counting the number of times n is divisible by 2. It then counts the number of times n is divisible by each odd prime number up to the square root of n. If n is not divisible by any prime number greater than its square root, then n is prime and the function returns 2.

The main function of the program uses a loop to iterate through the numbers from 1 to 50000. For each number n, the function divisor_count is called to calculate the number of divisors of n. If n has exactly 8 divisors, then the function prints n to the console.

The program also prints the 500th, 5000th, and 50000th numbers which are the cube roots of the products of their proper divisors.

Source code in the cpp programming language

#include <iomanip>
#include <iostream>

unsigned int divisor_count(unsigned int n) {
    unsigned int total = 1;
    for (; (n & 1) == 0; n >>= 1)
        ++total;
    for (unsigned int p = 3; p * p <= n; p += 2) {
        unsigned int count = 1;
        for (; n % p == 0; n /= p)
            ++count;
        total *= count;
    }
    if (n > 1)
        total *= 2;
    return total;
}

int main() {
    std::cout.imbue(std::locale(""));
    std::cout << "First 50 numbers which are the cube roots of the products of "
                 "their proper divisors:\n";
    for (unsigned int n = 1, count = 0; count < 50000; ++n) {
        if (n == 1 || divisor_count(n) == 8) {
            ++count;
            if (count <= 50)
                std::cout << std::setw(3) << n
                          << (count % 10 == 0 ? '\n' : ' ');
            else if (count == 500 || count == 5000 || count == 50000)
                std::cout << std::setw(6) << count << "th: " << n << '\n';
        }
    }
}


  

You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Ada programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Forward difference step by step in the jq programming language
You may also check:How to resolve the algorithm Perlin noise step by step in the Swift programming language