How to resolve the algorithm Tau function step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Tau function step by step in the C 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 C programming language
Overview:
This C program calculates the number of divisors (sometimes called the "tau function") for the first n positive integers up to a specified limit. It uses an efficient algorithm to determine the divisor count for each number.
Function divisor_count:
- Input: An unsigned integer
n
representing a positive integer. - Output: The number of divisors of
n
. - Algorithm:
- Count the powers of 2 in
n
and increment the divisor count accordingly. - Iterate through odd prime factors up to the square root of
n
. For each prime factorp
, count the number of times it dividesn
. - If
n
is still greater than 1 after the loop, it means there is a single prime factor greater than the square root ofn
. Increment the divisor count by 2 in this case.
- Count the powers of 2 in
Function main:
- Setup:
- Defines a limit of 100 positive integers for which to calculate divisor counts.
- Loop over Integers:
- Iterates from 1 to the specified limit.
- For each integer
n
, calls thedivisor_count
function to calculate the number of divisors.
- Print Results:
- Prints the divisor count for each integer on a single line, followed by a newline every 20 integers.
Example Output:
If you run this program, you will see the following output:
Count of divisors for the first 100 positive integers:
1 2 2 3 2 4 2 4 3 4 2 6 2 4 3 4 5 2 4 6 2 4 3 4 5 6 2 8 3 4 2 4 5 6 7 2 4 6 3 4 5 6 2 4 7 8 2 4 3 4 5 6 7 2 8 9 2 4 5 6 3 4 5 6 7 2 8 3 4
This output shows the number of divisors for each positive integer up to 100. For example, the number 1 has 1 divisor (itself), while the number 12 has 6 divisors (1, 2, 3, 4, 6, and 12).
Source code in the c programming language
#include <stdio.h>
// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1;
// Deal with powers of 2 first
for (; (n & 1) == 0; n >>= 1) {
++total;
}
// Odd prime factors up to the square root
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 then it's prime
if (n > 1) {
total *= 2;
}
return total;
}
int main() {
const unsigned int limit = 100;
unsigned int n;
printf("Count of divisors for the first %d positive integers:\n", limit);
for (n = 1; n <= limit; ++n) {
printf("%3d", divisor_count(n));
if (n % 20 == 0) {
printf("\n");
}
}
return 0;
}
You may also check:How to resolve the algorithm Time a function step by step in the Forth programming language
You may also check:How to resolve the algorithm Write entire file step by step in the K programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the zkl programming language
You may also check:How to resolve the algorithm Factorial step by step in the Jsish programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Klingphix programming language