Python Program to Count Divisors of Factorial

In the previous article, we have discussed Python Program to Reorder a List According to Given Indexes
Factorial:

The product of all positive integers less than or equal to n is the factorial of a non-negative integer n, denoted by n! in mathematics:

n! = n * (n – 1) *(n – 2) * . . . . . . . . . . 3 * 2 * 1.

4 != 4 * 3 * 2 *1= 24

Given a number and the task is to count divisors of factorial of a given number.

For example :

Let N=6

The divisors of factorial of a given number{ 6 }: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720

The count of divisors of factorial of a given number{ 6 }= 30

Examples:

Example 1:

Input:

Given Number = 7

Output:

The divisors of factorial of a given number{ 7 }:
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 28 30 35 36 40 42 45 48 56 60 63 70 72 80 84 90 105 112 120 126 140 144 168 180 210 240 252 280 315 336 360 420 504 560 630 720 840 1008 1260 1680 2520 5040 
The count of divisors of factorial of a given number{ 7 }= 60

Example 2:

Input:

Given Number = 4

Output:

The divisors of factorial of a given number{ 4 }:
1 2 3 4 6 8 12 24 
The count of divisors of factorial of a given number{ 4 }= 8

Program to Count Divisors of Factorial in Python

Below are the ways to count divisors of factorial of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
num = 5
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

The divisors of factorial of a given number{ 5 }:
1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120 
The count of divisors of factorial of a given number{ 5 }= 16

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take a variable, initialize it with the value ‘1’, and store it in another variable say ‘factorial’.
  • Loop from 1 to the given number using the for loop.
  • Calculate the factorial of a given number by multiplying the above variable “factorial” with the iterator and store it in the same variable ‘factorial’.
  • Take a variable, initialize it with the value ‘0’, and store it in another variable say ‘c’.
  • Loop from 1 to the above obtained factorial number using the for loop.
  • Check if the value of factorial modulus iterator value is equal to 0 using the if conditional statement.
  • If the statement is true, print the iterator value and increment the value of ‘c’.
  • Store it in the same variable ‘c’.
  • Print the count of divisors of factorial of a given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
num = int(input("Enter some random number = "))
# Take a variable, initialize it with the value '1', and store it in another
# variable say 'factorial'.
factorl = 1
# Loop from 1 to the given number using the for loop.
for i in range(1, num+1):
 # Calculate the factorial of a given number by multiplying the above variable "factorial"
    # with the iterator and store it in the same variable 'factorial'.
    factorl = factorl*i
# Take a variable, initialize it with the value '0', and store it in another variable
# say 'c'.
c = 0
print("The divisors of factorial of a given number{", num, "}:")
# Loop from 1 to the above obtained factorial number using the for loop.
for i in range(1, factorl+1):
  # Check if the value of factorial modulus iterator value is equal to 0 using
    # the if conditional statement.
    if(factorl % i == 0):
     # If the statement is true, print the iterator value and increment the value of 'c'.
        # Store it in the same variable 'c'.
        print(i, end=" ")
        c = c+1
print()
# Print the count of divisors of factorial of a given number.
print("The count of divisors of factorial of a given number{", num, "}=", c)

Output:

Enter some random number = 3
The divisors of factorial of a given number{ 3 }:
1 2 3 6 
The count of divisors of factorial of a given number{ 3 }= 4

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.