l1-006. Continuous factor

Source: Internet
Author: User
There may be several consecutive numbers in the factor of a positive integer n. For example, 630 can be decomposed into 3*5*6*7, where 5, 6, and 7 are 3 consecutive numbers. Given any positive integer n, it is required to write a program to find the maximum number of consecutive factors, and output the smallest continuous factor sequence.

Input format:

The input gives a positive integer N (1<n<2^31) in a row.

Output format:

First, the number of the longest continuous factor is output in line 1th, and then the smallest continuous factor sequence is output in line 2nd in the format of Factor 1* factor K 2*......*, where the factor is output in ascending order and 1 is not counted.

Input Sample:
630
Sample output:
3
5*6*7
Is the search, from each can divide the factor to start the continuous search,
until no longer divisible, return to find the next can divide the factor; the
only point is that the prime number outputs itself;
*/
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
the int find (int n,int fac,int l)  //n is the remaining factor FAC for the next factor  L is the length of the continuous factor
{
    if (n%fac==0)
    {
        find (N/FAC, fac+1,l+1);
    }
    else
        return l;
}
int main ()
{
    int n,length=0,ans;
    cin>>n;
    for (int i=2; i<sqrt (n); i++)
    {
        if (n%i==0)
        {
            int l=find (n/i,i+1,1);
            if (l>length)
            {
                length=l;
                Ans=i
    ;
    }}} if (length==0)   //Description n is prime number
    {
        cout<<1<<endl;
        cout<<n<<endl;
        return 0;
    }
    cout<<length<<endl;
    for (int i=0; i<length-1; i++)
        cout<<ans+i<< "*";
    cout<<ans+length-1<<endl;

}

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.