/* * File Name: FactorDecomposition.c * Author: MH * Since 2011/05/23 * Toolkit: Dev C++ */ # include <stdlib.h> # include <stdio.h> # include <math.h> int isprime(int); void factor(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's factor are :\n", 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 factors void factor(int input){ int i; for(i=1; i<=input; i++){ if(input%i==0){ // i is a factor of input printf("%d ", i); } } }
搜尋此網誌
[C/C++][判斷質數與因數分解]
之前做過判斷質數與質因數分解的程式,那如果不是要列出質因數,而是要列出因數呢?那就更簡單了,把prime()裡的改一改就好了,只要整除就是因數
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言