How to resolve the algorithm Multifactorial step by step in the C programming language
How to resolve the algorithm Multifactorial step by step in the C programming language
Table of Contents
Problem Statement
The factorial of a number, written as
n !
{\displaystyle n!}
, is defined as
n !
n ( n − 1 ) ( n − 2 ) . . . ( 2 ) ( 1 )
{\displaystyle n!=n(n-1)(n-2)...(2)(1)}
. Multifactorials generalize factorials as follows: In all cases, the terms in the products are positive integers. If we define the degree of the multifactorial as the difference in successive terms that are multiplied together for a multifactorial (the number of exclamation marks), then the task is twofold:
Note: The wikipedia entry on multifactorials gives a different formula. This task uses the Wolfram mathworld definition.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multifactorial step by step in the C programming language
This C code snippet defines and uses the multifactorial function. The multifactorial of a number is defined as the product of that number and all the integers below it down to some non-negative degree.
The code includes standard input and output library and defines two constants, HIGHEST_DEGREE
and LARGEST_NUMBER
.
The function multifact
computes the multifactorial of a number n
to a given degree deg
recursively. It's a recursive implementation of the multifactorial function where the function calls itself until the degree of the multifactorial is 0.
The function multifact_i
computes the multifactorial of a number n
to a given degree deg
iteratively. It's an iterative implementation of the multifactorial function where a loop is used to calculate the multifactorial.
The main
function is the entry point of the program and it tests the multifactorial functions by printing out the multifactorials of numbers from 1 to LARGEST_NUMBER
for degrees from 1 to HIGHEST_DEGREE
.
Source code in the c programming language
/* Include statements and constant definitions */
#include <stdio.h>
#define HIGHEST_DEGREE 5
#define LARGEST_NUMBER 10
/* Recursive implementation of multifactorial function */
int multifact(int n, int deg){
return n <= deg ? n : n * multifact(n - deg, deg);
}
/* Iterative implementation of multifactorial function */
int multifact_i(int n, int deg){
int result = n;
while (n >= deg + 1){
result *= (n - deg);
n -= deg;
}
return result;
}
/* Test function to print out multifactorials */
int main(void){
int i, j;
for (i = 1; i <= HIGHEST_DEGREE; i++){
printf("\nDegree %d: ", i);
for (j = 1; j <= LARGEST_NUMBER; j++){
printf("%d ", multifact(j, i));
}
}
}
You may also check:How to resolve the algorithm Longest string challenge step by step in the Ring programming language
You may also check:How to resolve the algorithm Sum to 100 step by step in the C programming language
You may also check:How to resolve the algorithm JSON step by step in the Racket programming language
You may also check:How to resolve the algorithm Abbreviations, easy step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the Racket programming language