100以内的质因数分解并计算完全平方数

代码价值:用时3天

//求100以内的质因数分解,同时计算x!,是否是一个完全平方数 //tips:质因数每项都为偶数个,则x就是一个完全平方数 //运行后观察并结论:任何 x! 都不是完全平方数(除了0,和1以外) #include //ceil #include //printf #include //malloc #include //memset #include //true #include //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); } while (0 == ttt.rem); } while (1); *out = d_count; return; } //param in a {array_len, int[]}, //return true if all element are even in array bool IsAbsoluteSquareNumber(int *in) { int d_count = *in; if (d_count == 0) return false; int *p = in + 1; for (int i = 0; i < d_count; i++) { if ((p[i] & 1) == 0) { continue; } else { return false; } } return true; } //out is holds int[] = {count of divisor 2, ... of divisor 3, ... of 5, ...7, ...} //out = in + out void addto(const int *const primes, int *in, int *out) { int d_count_in = in[0]; if (d_count_in == 0) return; int *p_in = in + 1; for (int i = 0; i < d_count_in; i++) { int divisor = *p_in; //search divisor in prime array return its index int iD; for (iD = 0; divisor > primes[iD]; iD++) { } if (divisor < primes[iD]) //bad divisor or bad prime { //exception } p_in++; out[iD] += *p_in; p_in++; } return; } // function search_factorial_is_absolutesquarenumber_under_101 // param range, calc range from 1 to 100 // param primes, a array of prime // param out, a {arraylen, int[]}, element_count, and n1,n2,n3,,,, void search_f_asn101(const int *const primes, int * const out) { int positiveNum_count = 0; int *pout = out + 1; void *const memblock = malloc(sizeof(int)*(1 + PRIME_COUNT_UNDER_101 + 1 + MAX_PRIME_DIVISOR_COUNT_UNDER_128)); int *const pbSum = (int *)memblock; //pointer to a mem block, holds sums of divisors which is divisors of x! int *const pbTempDivisorArray = (int *)(pbSum + 1 + PRIME_COUNT_UNDER_101); //pointer to a block of int //quick init *pbSum = 26; *(pbSum + 1) = 1; memset(pbSum + 2, 0, PRIME_COUNT_UNDER_101 - 1); for (int i = 3; i <= 100; i++) { PrimeDivisors(i, primes, pbTempDivisorArray); addto(primes, pbTempDivisorArray, pbSum+1); //x! = (x-1)! * x if (IsAbsoluteSquareNumber(pbSum)) { positiveNum_count++; *pout++ = i; } printf("%d/n", i); for (int i = 1; i < (1 + PRIME_COUNT_UNDER_101); i++) { printf("%d,", pbSum[i]); } printf("/n"); } free(memblock); *out = positiveNum_count; return; } int main(void) { int *result = (int *)malloc(100 * sizeof(int)); search_f_asn101(primes26, result); int count = *result; if (count > 0) { int *p = result + 1; for (int i = 0; i < count; i++) { printf("%d/t", p[i]); } } free(result); exit(EXIT_SUCCESS); } =============== output =============== 3 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 4 3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 5 3,1,1,0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,1,1,1,1,1,0,0,0,0, 80 78,28,13,10,11,6,4,4,3,5,2,2,1,1,1,2,1,1,1,1,1,1,0,0,0,0, 81 78,32,13,10,11,6,4,4,3,5,2,2,1,1,1,2,1,1,1,1,1,1,0,0,0,0, 82 79,32,13,10,11,6,4,4,3,5,2,2,2,1,1,2,1,1,1,1,1,1,0,0,0,0, 83 79,32,13,10,11,6,4,4,3,5,2,2,2,1,1,2,1,1,1,1,1,1,1,0,0,0, 84 81,33,13,11,11,6,4,4,3,5,2,2,2,1,1,2,1,1,1,1,1,1,1,0,0,0, 85 81,33,14,11,11,6,5,4,3,5,2,2,2,1,1,2,1,1,1,1,1,1,1,0,0,0, 86 82,33,14,11,11,6,5,4,3,5,2,2,2,2,1,2,1,1,1,1,1,1,1,0,0,0, 87 82,34,14,11,11,6,5,4,3,6,2,2,2,2,1,2,1,1,1,1,1,1,1,0,0,0, 88 85,34,14,11,12,6,5,4,3,6,2,2,2,2,1,2,1,1,1,1,1,1,1,0,0,0, 89 85,34,14,11,12,6,5,4,3,6,2,2,2,2,1,2,1,1,1,1,1,1,1,1,0,0, 90 86,36,15,11,12,6,5,4,3,6,2,2,2,2,1,2,1,1,1,1,1,1,1,1,0,0, 91 86,36,15,12,12,7,5,4,3,6,2,2,2,2,1,2,1,1,1,1,1,1,1,1,0,0, 92 88,36,15,12,12,7,5,4,4,6,2,2,2,2,1,2,1,1,1,1,1,1,1,1,0,0, 93 88,37,15,12,12,7,5,4,4,6,3,2,2,2,1,2,1,1,1,1,1,1,1,1,0,0, 94 89,37,15,12,12,7,5,4,4,6,3,2,2,2,2,2,1,1,1,1,1,1,1,1,0,0, 95 89,37,16,12,12,7,5,5,4,6,3,2,2,2,2,2,1,1,1,1,1,1,1,1,0,0, 96 94,38,16,12,12,7,5,5,4,6,3,2,2,2,2,2,1,1,1,1,1,1,1,1,0,0, 97 94,38,16,12,12,7,5,5,4,6,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,0, 98 95,38,16,12,12,7,5,5,4,6,3,2,2,2,2,3,1,1,1,1,1,1,1,1,1,0, 99 95,40,16,12,13,7,5,5,4,6,3,2,2,2,2,3,1,1,1,1,1,1,1,1,1,0, 100 97,40,16,12,13,7,5,5,4,7,3,2,2,2,2,3,1,1,1,1,1,1,1,1,1,0, Press any key to continue...

 

你可能感兴趣的:(程序题)