代码价值:用时3天 //求100以内的质因数分解,同时计算x!,是否是一个完全平方数 //tips:质因数每项都为偶数个,则x就是一个完全平方数 //运行后观察并结论:任何 x! 都不是完全平方数(除了0,和1以外) #include <math.h> //ceil #include <stdio.h> //printf #include <stdlib.h> //malloc #include <string.h> //memset #include <stdbool.h> //true #include <iso646.h> //and const int primes26[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 }; //#define MAX_PRIME_DIVISOR_COUNT_UNDER_1024 10 //2^10 = 1024 #define MAX_PRIME_DIVISOR_COUNT_UNDER_128 7 //2^7 = 128 #define PRIME_COUNT_UNDER_101 26 //2^7 = 128 //data structure divisor_array = {int d_count, [d1, d1_count, d2, d2_count, d3, ...]} //data structure divisor_array max len 100 //param n, a nature number //param primes, a int[] prime array //param out, return int[] divisors array as: {d_count, [d1, d1_count, d2, d2_count, d3, ...]} // which d_count is count of divisors at first void PrimeDivisors(int n, const int *const primes, int *const out) { int d_count = 0; switch (n) { case 0: ; case 1: *out = d_count; return; } int factorCount = 0; while (((n & 1) == 0)and(n > 1)) //test factor of 2 { factorCount++; n >>= 1; } int *pout = out + 1; //write power of 2 int denom = 2; //cache =*pprimes=2 int *pprimes = (int *)primes; //pointer to prime array do { if (factorCount > 0) { *pout++ = denom; *pout++ = factorCount; d_count++; } if (n > 1) { pprimes++; denom = *pprimes; int SqrtN; SqrtN = ceil(sqrt(n)); if (denom >= SqrtN) { *pout++ = n; //n is a prime now *pout++ = 1; d_count++; break; } } else //n==1 all end { break; } factorCount = 0; //reset div_t ttt; goto loptest1; do { factorCount++; n = ttt.quot; loptest1: ttt = div(n, denom); }