Product of factors of number
The "Product of Factors of a Number" problem involves finding all the factors of a given positive integer and then calculating their product. A factor of a number is a positive integer that divides the number without leaving a remainder. The product of these factors is simply the result of multiplying all the factors together. In this article, we will explain the problem, provide a suitable example, present the pseudocode and algorithm to solve the problem, and analyze the time complexity of the given code.
Problem Statement
Given a positive integer num
, we need to find all the factors of num
and then calculate
their product.
Example
Let's take an example to illustrate the problem. Consider the number 20
.
Factors of 20 are: 1, 2, 4, 5, 10, and 20.
Product of factors = 1 * 2 * 4 * 5 * 10 * 20 = 800
Pseudocode
factorProduct(num)
result = num
for i = 2 to num/2
if num is divisible by i
print i
result = result * i
print num
print "Factors Product :", result
Algorithm Explanation
 Start by initializing the variable
result
to the value ofnum
. This variable will store the product of factors.  Loop through all integers
i
from 2 tonum/2
. We start from 2 because every number is divisible by 1, and we go up tonum/2
because any factor greater thannum/2
would result in a quotient less than 2, which means it's not a factor.  Inside the loop, check if
num
is divisible byi
using the modulo operator (num % i == 0
). If the condition is true, it meansi
is a factor ofnum
.  Print the value of
i
, as it is one of the factors.  Update the
result
by multiplying it with the factori
.  After the loop, print the value of
num
to include it as one of the factors.  Print the final result, which is the product of all the factors stored in the
result
variable.
Code Solution
Here given code implementation process.
// C Program
// Product of factors of number
#include <stdio.h>
// Calculate factors product of a number
void factorProduct(int num)
{
printf("\n Factors of a number %d is \n", num);
int result = num;
// Execute loop through by 2 to num/2
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
printf(" %d", i);
// calculated product
result *= i;
}
}
// Last factors
printf(" %d", num);
// Display calculated result
printf("\n Factors Product : %d\n", result);
}
int main()
{
// Test Cases
factorProduct(15);
factorProduct(40);
factorProduct(31);
return 0;
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
/*
Java Program
Product of factors of number
*/
public class Factorization
{
// Calculate factors product of a number
public void factorProduct(int num)
{
System.out.print("\n Factors of a number " + num + " is \n");
int result = num;
// Execute loop through by 2 to num/2
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
System.out.print(" " + i);
// calculated product
result *= i;
}
}
// Last factors
System.out.print(" " + num);
// Display calculated result
System.out.print("\n Factors Product : " + result + "\n");
}
public static void main(String[] args)
{
Factorization task = new Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Product of factors of number
*/
class Factorization
{
public:
// Calculate factors product of a number
void factorProduct(int num)
{
cout << "\n Factors of a number " << num << " is \n";
int result = num;
// Execute loop through by 2 to num/2
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
cout << " " << i;
// calculated product
result *= i;
}
}
// Last factors
cout << " " << num;
// Display calculated result
cout << "\n Factors Product : " << result << "\n";
}
};
int main()
{
Factorization task = Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
return 0;
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
// Include namespace system
using System;
/*
C# Program
Product of factors of number
*/
public class Factorization
{
// Calculate factors product of a number
public void factorProduct(int num)
{
Console.Write("\n Factors of a number " + num + " is \n");
int result = num;
// Execute loop through by 2 to num/2
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
Console.Write(" " + i);
// calculated product
result *= i;
}
}
// Last factors
Console.Write(" " + num);
// Display calculated result
Console.Write("\n Factors Product : " + result + "\n");
}
public static void Main(String[] args)
{
Factorization task = new Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
<?php
/*
Php Program
Product of factors of number
*/
class Factorization
{
// Calculate factors product of a number
public function factorProduct($num)
{
echo "\n Factors of a number ". $num ." is \n";
$result = $num;
// Execute loop through by 2 to num/2
for ($i = 2; $i <= intval($num / 2); ++$i)
{
if ($num % $i == 0)
{
echo " ". $i;
// calculated product
$result *= $i;
}
}
// Last factors
echo " ". $num;
// Display calculated result
echo "\n Factors Product : ". $result ."\n";
}
}
function main()
{
$task = new Factorization();
// Test Cases
$task>factorProduct(15);
$task>factorProduct(40);
$task>factorProduct(31);
}
main();
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
/*
Node Js Program
Product of factors of number
*/
class Factorization
{
// Calculate factors product of a number
factorProduct(num)
{
process.stdout.write("\n Factors of a number " + num + " is \n");
var result = num;
// Execute loop through by 2 to num/2
for (var i = 2; i <= parseInt(num / 2); ++i)
{
if (num % i == 0)
{
process.stdout.write(" " + i);
// calculated product
result *= i;
}
}
// Last factors
process.stdout.write(" " + num);
// Display calculated result
process.stdout.write("\n Factors Product : " + result + "\n");
}
}
function main()
{
var task = new Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
main();
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
# Python 3 Program
# Product of factors of number
class Factorization :
# Calculate factors product of a number
def factorProduct(self, num) :
print("\n Factors of a number ", num ," is ")
result = num
i = 2
# Execute loop through by 2 to num/2
while (i <= int(num / 2)) :
if (num % i == 0) :
print(" ", i, end = "")
# calculated product
result *= i
i += 1
# Last factors
print(" ", num, end = "")
# Display calculated result
print("\n Factors Product :", result )
def main() :
task = Factorization()
# Test Cases
task.factorProduct(15)
task.factorProduct(40)
task.factorProduct(31)
if __name__ == "__main__": main()
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
# Ruby Program
# Product of factors of number
class Factorization
# Calculate factors product of a number
def factorProduct(num)
print("\n Factors of a number ", num ," is \n")
result = num
i = 2
# Execute loop through by 2 to num/2
while (i <= num / 2)
if (num % i == 0)
print(" ", i)
# calculated product
result *= i
end
i += 1
end
# Last factors
print(" ", num)
# Display calculated result
print("\n Factors Product : ", result ,"\n")
end
end
def main()
task = Factorization.new()
# Test Cases
task.factorProduct(15)
task.factorProduct(40)
task.factorProduct(31)
end
main()
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
/*
Scala Program
Product of factors of number
*/
class Factorization
{
// Calculate factors product of a number
def factorProduct(num: Int): Unit = {
print("\n Factors of a number " + num + " is \n");
var result: Int = num;
var i: Int = 2;
// Execute loop through by 2 to num/2
while (i <= (num / 2).toInt)
{
if (num % i == 0)
{
print(" " + i);
// calculated product
result *= i;
}
i += 1;
}
// Last factors
print(" " + num);
// Display calculated result
print("\n Factors Product : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Factorization = new Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
/*
Swift 4 Program
Product of factors of number
*/
class Factorization
{
// Calculate factors product of a number
func factorProduct(_ num: Int)
{
print("\n Factors of a number ", num ," is ");
var result: Int = num;
var i: Int = 2;
// Execute loop through by 2 to num/2
while (i <= num / 2)
{
if (num % i == 0)
{
print(" ", i, terminator: "");
// calculated product
result *= i;
}
i += 1;
}
// Last factors
print(" ", num, terminator: "");
// Display calculated result
print("\n Factors Product : ", result );
}
}
func main()
{
let task: Factorization = Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
main();
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
/*
Kotlin Program
Product of factors of number
*/
class Factorization
{
// Calculate factors product of a number
fun factorProduct(num: Int): Unit
{
print("\n Factors of a number " + num + " is \n");
var result: Int = num;
var i: Int = 2;
// Execute loop through by 2 to num/2
while (i <= num / 2)
{
if (num % i == 0)
{
print(" " + i);
// calculated product
result *= i;
}
i += 1;
}
// Last factors
print(" " + num);
// Display calculated result
print("\n Factors Product : " + result + "\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Factorization = Factorization();
// Test Cases
task.factorProduct(15);
task.factorProduct(40);
task.factorProduct(31);
}
Output
Factors of a number 15 is
3 5 15
Factors Product : 225
Factors of a number 40 is
2 4 5 8 10 20 40
Factors Product : 2560000
Factors of a number 31 is
31
Factors Product : 31
Resultant Output Explanation
let's evaluate the output for the given test cases:

factorProduct(15);
 Factors of 15 are: 3, 5, 15
 Product of factors = 3 * 5 * 15 = 225

factorProduct(40);
 Factors of 40 are: 2, 4, 5, 8, 10, 20, 40
 Product of factors = 2 * 4 * 5 * 8 * 10 * 20 * 40 = 2560000

factorProduct(31);
 Factors of 31 are: 31
 Product of factors = 31
Time Complexity
Let's analyze the time complexity of the given code. The loop runs from 2 to num/2
, so its time
complexity can be considered as O(num/2). However, the constant factor is ignored in Big O notation. Hence, the time
complexity of the code is approximately O(num).
In simple terms, the time taken by the code to calculate the product of factors depends linearly on the input
num
. As num
grows larger, the time taken by the code also grows linearly.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment