all factors of a numbe r Algorithm
The algorithm to find all factors of a number is a systematic computational procedure used to identify and list all the factors or divisors of a given integer. Factors are the numbers that can be multiplied together to obtain the original number. The primary goal of this algorithm is to break down the given number into its constituent factors, which can be further used for various purposes such as prime factorization, solving arithmetic problems, or simplifying fractions.
The algorithm starts by iterating through the numbers from 1 to the square root of the given number, checking if the current number divides the given number without leaving a remainder. If a number is found to be a factor, both the current number and the result of dividing the given number by the current number are added to the list of factors. This process continues until the square root of the given number is reached, ensuring that all possible factors are considered. Once the iteration is complete, the list of factors is returned as the final output. This algorithm is efficient and relatively fast, as it only requires checking up to the square root of the given number rather than iterating through all possible numbers up to the given number itself.
//
// All Factors of a number
//
// The All ▲lgorithms Project
//
// https://allalgorithms.com/
// https://github.com/allalgorithms/cpp
//
// Contributed by: divyanshu132
// Github: @divyanshu132
//
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n=12;
while(n%2==0)
{
cout<<"2 ";
n=n/2;
}
for(int i=3;i<sqrt(n);i=i+2)
{
while(n%i==0)
{
cout<<i<<" ";
n=n/i;
}
}
if(n>1)
cout<<n;
}