Prime factorisation function

Write function void func (unsinged int n), and outputs calculated to achieve a positive integer prime factorization type. For example: n = 90, the output 90 = 3 * 2 * 3 * 5.

① is a prime outputs:

You may direct the thinking must first be a judge, if the judge is the prime end, the output of this prime number. If not, enter the following in the steps of the decomposition of it.

However, no one thought about what prime is the definition of? Prime number is a kind of self-divisible only by 1 or number, right? If you remove this before the judge directly in the next step of decomposition to judge what will happen?

If the input of the "11", "11" is a prime number, divisible only by 1 and 11. Then, set the divisor 2 from the beginning, is not divisible from 2-10 till no law? Just wait until 11, 11 divided by 11 is 1, this time the output 11. And is not the result of the judgment above the same?

②, the output of the prime factor factorization, the divisor is a prime number:

Here some may be wondering, if only divisible prime number, is not it should write a function to output prime it? Starting from 2, 3,5,7,11 such cyclic output to the prime. If not divide into 2 to 3 changes, and so on.

If you really think so, not only the source code written especially complex do not say, even after repeatedly debugging. So, is there an easier way to do that?

If, enter a number 24.24 can be decomposed into 3X8 or 4X6, right? But our thoughts go according to ①, first divisor is 2, then 3X8 directly out of the game. One might think that, in addition to 24 plus 1 is 2 get 12,2 3,12 4,3 is divided by 3 is 4 plus 1, the divisor in the non-prime, not ah! ! ! ! ! ! Moreover, 4 can be split into two 2 ah? !

So, you can be a judge at the time divided by two, ah, if obtained after the first addition to the business can continue to be divisible by 2, then add 1 divisor would like to do it! Until indivisible, and a replaced, and so on.

If the 4 to it? Are they going to appear non-prime it?

Possible, in accordance with the above idea, the divisor can not divisible sequentially accumulated in all the divisors obtained could only be prime. 4 before reaching because it has been split into two 2-up. If it is 18, the first time was 9, 9 will not occur, because the front 9 has been 3X3 get! ! !

Therefore, this question is nothing more than a tired test preparation division.

 

#include <stdio.h>
#include <string.h>

void func(unsigned int n)
{
	int i=2,flag=0;
	while(n>=2) {
		if(n%i==0) {
			if(flag==1)
				printf("*");
			printf("%d",i);
			n=n/i;
			flag=1;
		} else {
			i++;
		}
	}
	printf("\n");
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
		func(n);
	return 0;
}

 Ideas Reference: https://blog.csdn.net/Issac_X10A/article/details/81773427

Published 43 original articles · won praise 23 · views 5308

Guess you like

Origin blog.csdn.net/weixin_43442778/article/details/103793106