L1-006 continuous factors C language

There may be a positive integer number N of several successive factors. 630 can be decomposed into, for example, 3 × 5 × 6 × 7, wherein 5,6,7 consecutive numbers is 3. Given a positive integer N, the number of required programming longest continuous factors is obtained, and outputs the minimum contiguous factor sequence.
Input formats:

In a given input row positive integer N (1 <N <231) .
Output formats:

First, the number of the first output line of the longest continuous factors; then in the second row by a factor of 1 Factor 2 ...... * factor k factor output format minimum continuous sequence, in which the output factor ascending order, not in the 1 Inside.
Sample input:

630
sample output:

3
567

Easy to implement in C language

#include<stdio.h>

int LenthFactorSeries(int num,int start);
int IntSqrt(int num);

main()
{
int num,i,supnum,maxlenth=0,maxstart,lenth;
scanf("%d",&num);

supnum=IntSqrt(num);
for(i=2;i<=supnum;i++){
lenth=LenthFactorSeries(num,i);
if(lenth>maxlenth){
maxlenth=lenth;
maxstart=i;
}
}
if(maxlenth==0){
maxlenth=1;
maxstart=num;
}
printf("%d\n",maxlenth);
for(i=0;i<maxlenth;i++){
printf("%d",maxstart+i);
if(i!=maxlenth-1) printf("*");
}

}

IntSqrt an int ( an int the num)
{ an
int Intsqrt = ( the num + 1) / 2, Newsqrt;

while(1){
newsqrt=(intsqrt+num/intsqrt)/2;
if(newsqrt >= intsqrt) break;
intsqrt=newsqrt;
}

return intsqrt;

}

int LenthFactorSeries(int num,int start)
{
int lenth=0;
while(num%start==0){
lenth++;
num/=start;
start++;
}
return lenth;
}

Released four original articles · won praise 0 · Views 51

Guess you like

Origin blog.csdn.net/Nexus55/article/details/103914002