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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Tau function step by step in the Rust 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 Rust programming language

Source code in the rust programming language

// returns the highest power of i that is a factor of n,
// and n divided by that power of i
fn factor_exponent(n: i32, i: i32) -> (i32, i32) {
	if n % i == 0 {
		let (a, b) = factor_exponent(n / i, i);
		(a + 1, b)
	} else {
		(0, n)
	}
}

fn tau(n: i32) -> i32 {
	for i in 2..(n+1) {
		if n % i == 0 {
			let (count, next) = factor_exponent(n, i);
			return (count + 1) * tau(next);
		}
	}
	return 1;
}

fn main() {
	for i in 1..101 {
		print!("{} ", tau(i));
	}
}


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Swift programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Go programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Logo programming language