/* * File Name: PrimeFactorDecomposition.c * Author: MH * Since 2011/05/23 * Toolkit: Dev C++ */ # include <stdlib.h> # include <stdio.h> # include <math.h> int isprime(int); void prime(int); int main(){ int input; while(1){ printf("Please input an integer :\n"); scanf("%d", &input); if (input <= 1) printf("\ninput must larger than 1\n\n"); else if(isprime(input)==0){ // input is not a prime number printf("\n%d is not a prime\n\n", input); printf("%d = ", input); prime(input); printf("\n\n"); } else // input is a prime number printf("\n%d is a prime number\n\n", input); system("PAUSE"); system("CLS"); } return 0; } // check if input is a prime number or not int isprime(int input){ int i, first=1; for(i=2; i<=sqrt(input); i++){ if(input%i==0){ // not a prime number return 0; } } return 1; } // find all the prime factors void prime(int input){ int i; for(i=2; i<=sqrt(input); i++){ while(input!=i){ // find all same prime factors if(input%i==0){ // i is a prime number of input printf("%d * ", i); input = input/i; // divide prime factor } else // not a prime factor break; } } printf("%d", input); // the last prime factor }
搜尋此網誌
[C/C++][判斷質數與質因數分解]
isprime()先判斷是否為質數,若否,則用prime()找出所有質因數,所有質因數用乘積表示,若相同的質因數用次方表示還要修改一下,MH太懶了,自己動手改吧
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言