Basic algorithm of C language 35-factorization prime factor (method 2), 35 prime factor
// Matrix Basics
/*
========================================================== ======================================
Question: enter a positive integer and break it into a matrix, for example, 60 = 2*2*3*5. If it is a prime number, the output is
For example, 307 is a prime number!
========================================================== ======================================
*/
# Include <stdio. h>
Int sushu (int n)
{
Int I, k;
K = n/2; // controls the end position!
For (I = 2; I <= k; I ++)
If (n % I = 0) break;
If (I> k) return 1;
Else return 0;
}
Void main ()
{
Int m, n, I, c, y [100];
Printf ("number of inputs to be decomposed :");
Scanf ("% d", & n );
While (n> 0)
{
M = n;
C = 0;
If (sushu (m ))
Printf ("% d is a prime number \ n", m );
Else
{
For (I = 2; I <= m; I ++)
{
While (m % I = 0) // a beginner may write if! Why can't I write it as if?
{
M/= I;
Y [c ++] = I;
}
}
Printf ("% d = % d", n, y [0]);
For (I = 1; I <c; I ++)
Printf ("* % d", y [I]);
Printf (".");
}
Printf ("\ n continue input :");
Scanf ("% d", & n );
}
}
/*
========================================================== ====================================
Rating:
This is an in-depth question. The idea is as follows: first define the judgment function of prime numbers, input a number, and then judge. If yes
Prime number. If 307 is a prime number! Otherwise proceed to the next step: Find the cause between 0 and m (note that m is dynamic)
Sub-, the first I found must be a prime number. For example, we cannot find 4 first. Because there are 2 before 4, we must first find 2, and 2 is
Prime number! After determining the prime factor I, change m to m/I, for example, 60, first locate the prime factor 2, then change to 60/2 = 30, through the loop (
At this time, the initial value of I is changed back to 2) continue to find the quality factor of 30 until the quality factor cannot be found (60 to 2 to 30, then 2 to 15, and then find
3. Change 5 to 5. The prime factor cannot be found, because 5 is a prime number.) The cycle ends! Required
Store the found Quality Factor in the pre-defined array y [], so that the output is as required at the end of the loop! If you don't need an array
Output, cannot input "*" or multiple outputs "*"! This is indeed the case. The reader can try to output the results without using arrays
Understand the above sentence!
========================================================== ======================================
*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.