1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 6 int main(int argc, char **argv) 7 { 8 unsigned long long data = 0, i = 0, max = 0; 9 10 if (argc < 2) 11 { 12 printf("argument error\n"); 13 return -1; 14 } 15 16 if (strlen(argv[1]) > 19) 17 { 18 printf("out of range\n"); 19 return -1; 20 } 21 22 data = atoll(argv[1]); 23 24 if (9223372036854775807ULL == data && ('8' == argv[1][18] || '9' == argv[1][18])) 25 { 26 printf("out of range\n"); 27 return -1; 28 } 29 30 printf("data : %llu\n", data); 31 while (1) 32 { 33 max = sqrt(data); 34 for (i = 2; i <= max; i++) 35 { 36 if (0 == (data % i)) 37 { 38 data = data / i; 39 printf("%llu * ", i); 40 fflush(stdout); 41 break; 42 } 43 } 44 45 if (i > max) 46 { 47 printf("%llu", data); 48 break; 49 } 50 } 51 printf("\n"); 52 53 return 0; 54 }
實現了2^64-1以下數字的求質因數,
編譯時報錯undefined reference to `sqrt'
man了下 發現編譯時要加-lm