Recursive integer factor decomposition

Source: Internet
Author: User

First, the problem description

A positive integer greater than 1 n can be decomposed into N=X1*X2*...*XM. Given an integer greater than 1, then count the number of its constituent forms, such as input 12:

12=12;
12=6*2;
12=4*3;
12=3*4; 12=3*2*2;
12=2*6; 12=2*3*2;
12=2*2*3;
A total of 8 different decomposition types.

As input 10:

10=10;

10=5*2;

10=2*5;

A total of 3 different decomposition types.

Second, the problem analysis

From the decomposition of 12 can be seen from 12 to 2 to find 12 factor, if divisible, then the quotient as a new value, continue to look for its factors, until it can no longer be decomposed, that is, a prime number, so this program uses recursive thinking.

Third, program design

#include <iostream>

using namespace Std;

int count=1; //counting starting from 1, now contains the number itself

void out (int n); //recursive function, function declaration

void Main ()
{
int number;
cout<< "Please input the number:";
cin>>number;

Out (number); //Execute function

cout<< "Total number of decomposition cases:" <<count;

while (1);
}

Recursive functions
void out (int n)
{
for (int j=n-1;j>1;j--) //descending from n-1 to 2, looking for approximate
if (n%j==0)
{
count++; //If it is possible to be removed, add 1
Out (n/j); ///recursive function to continue the recursion of the quotient
}

}

Iv. Results of the procedure

Recursive integer factor decomposition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.