Created
January 14, 2015 13:46
-
-
Save kenornotes/5db61368135cb8b20920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//計算階乘 | |
int factorial(int x) { | |
int i, result = 1;; | |
for (i = 2; i <= x; i++) { | |
result *= i; | |
} | |
return result; | |
} | |
//判定是否為質數 | |
int isPrime(int x) { | |
int i; | |
for ( i = 2; i < x; i++ ) { | |
if ( x % i == 0 ) | |
return 0; | |
} | |
return 1; | |
} | |
//做質因數分解 | |
void factor(int x, int a[]) { | |
int i = 0, count = 0; | |
//最後除完為 1 | |
while ( x > 1 ) { | |
//計算這個質數可以被整除幾次 | |
while ( x % a[i] == 0 ) { | |
x /= a[i]; | |
count ++; | |
} | |
//算好印出,計數器歸零,陣列推進一格 | |
printf("%d, ", count); | |
count = 0; | |
i++; | |
} | |
printf("\n"); | |
} | |
//取得質數陣列 | |
//會得到 arr[] = {2, 3, 5, 7, 11, 13....}; | |
void getFactor(int arr[], int size) { | |
int i, x = 2; | |
for (i = 0; i < size; i++) { | |
while (!isPrime(x)) { | |
x++; | |
} | |
arr[i] = x; | |
x++; | |
} | |
} | |
int main() { | |
//size可以自訂,此處取到前500個質數 | |
int n, p, i, j, primeArraySize = 500; | |
scanf("%d", &n); | |
p = factorial(n); | |
printf("%d! = %d\n", n, p); | |
int prime[primeArraySize]; | |
getFactor(prime, primeArraySize); | |
factor(p, prime); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment