Analysis prime factors
With this article you need to recall the prime factors is and how to analyze a number of prime factors.
Integrate the prime factor is the multiplication between the numbers together in which all numbers are prime. Example:
10 = 2 * 5. In that 2 and 5 is the prime.
18 = 2 * 3 * 3. In that 2 and 3 is the prime.
Analyzing the prime factors:
At the class level 1 and 2 something I do not remember, we have learned and. We will carry out consecutive division of needs analysis for the primes until the injury is 1 then stop. Example analysis 140.
140 | 2 70 | 2 35 | 5 7 | 7 1
We take 140 divide 2 (2 is prime) get 70. See 70 still divided by the 2 Should we continue to be divided 35. See 35 not divisible 2 another, followed primes 3 but also not divisible divisible 5 Should we divide 5 get 7. Go here 7 divide 7 get 1. Normally 1 should we stop. So 140 = 2 * 2 * 5 * 7
So we have recalled how. Summed up as we get them divided primes but can repeat. While still divisible number 2 it does not just divide until they find another split is to divide and to do the same.
But how to know the primes will be used to divide? With the baby as these examples we can mentally agile, but with a larger number of difficult mentally we are and nor is any prime number can also be used, eg number 140 we used to number no 3 though some 3 is prime. Simply select the number that we present our brand is divisible by using loop to browse and check out the number of words 2 to n (n is the number to be analyzed). So now try to code the direction that we think about the stars.
/** * Nhap vao 1 so tu nhien va phan tich ra thua so nguyen to */ #include <stdio.h> #include <math.h> int isPrime(int n) { int i; int m = (int) sqrt(n); for (i = 2; i <= m; i++) { if(n % i == 0) return 0; } return 1; } int main() { int n, i; printf("Enter number n = "); scanf("%d", &n); printf("%d = ", n); for (i = 2; i <= n; i++) { while( isPrime(i) && (n % i == 0) ) { printf("%d.", i); n = n / i; } } return 0; }
As the above code you see we have followed exactly the steps on. Note that there 1 function to check the number of light elements.
However, the code above can be greatly shortened. You try to think a little before you read on.
Have you noticed that when we were divided 140 give 2 Since one can not divide 2 anymore then it certainly is not divisible 4, 6, 8… because if divisible 4,6,8… then surely it is divisible 2. Similarly when the check had bastard can divisible 3 it can not be divisible by 6,9,12… From there we see the numbers behind it n is never divisible if it is a multiple of the number tested that can only divisible numbers have not checked, that the check number that is not prime numbers. So we will not need to check the number is prime or not. Our code will be more compact so much that no wrong results.
/** * Nhap vao 1 so tu nhien va phan tich ra thua so nguyen to */ #include <stdio.h> #include <math.h> int main() { int n, i; printf("Enter number n = "); scanf("%d", &n); printf("%d = ", n); for (i = 2; i <= n; i++) { while(n % i == 0) { printf("%d.", i); n /= i; } } return 0; }
Very simple, right! ^^. If you do that then this algorithm based on the algorithm sieve elements.
nếu như SNT = 3 thì phân tích ra là 1 .Đáng lẽ nó phải là 3 chứ bạn
mình bị lộn rồi bạn ơi só rỳ 🙂
phai viet ham kiem tra nguyen to nua ak
Needless nhé. Bạn đọc đoạn dưới.
nếu như nhập 11 thì nó trả về kết quả là -1 thì phải code ntn ạ??