實現了任意大數與 2^64-1以下的數相乘, 兩個任意大數可以將其中一個拆分成多個因數, 兩個大數質數暫未考慮 ...
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <stdbool.h> 5 6 #define MAX_LENGTH 128 7 #define ASSIST_LENGTH 19 8 9 int main(int argc, char **argv) 10 { 11 unsigned long long data[MAX_LENGTH] = {0}, tmp = 0; 12 int i = 0, j = 0, o_len = MAX_LENGTH - ASSIST_LENGTH; 13 bool c_flag = true; 14 15 if (argc < 3) 16 { 17 printf("argument error\n"); 18 return -1; 19 } 20 21 if (strlen(argv[1]) > o_len) 22 { 23 printf("out of range\n"); 24 return -1; 25 } 26 27 if (strlen(argv[2]) > 19) 28 { 29 printf("out of range\n"); 30 return -1; 31 } 32 33 tmp = atoll(argv[2]); 34 35 if (9223372036854775807ULL == tmp && ('8' == argv[2][18] || '9' == argv[2][18])) 36 { 37 printf("out of range\n"); 38 return -1; 39 } 40 41 for (i = strlen(argv[1])-1; i >= 0; i--) 42 { 43 data[j] = argv[1][i] - 48; 44 j++; 45 } 46 47 for (i = o_len - 1; i >= 0; i--) 48 { 49 if (c_flag && 0 == data[i]) 50 { 51 continue; 52 } 53 54 c_flag = false; 55 printf("%llu", data[i]); 56 } 57 printf(" * %llu\n", tmp); 58 59 for (i = 0; i < o_len; i++) 60 { 61 if (0 == data[i]) 62 { 63 continue; 64 } 65 66 data[i] *= tmp; 67 } 68 69 for (i = 0; i < o_len; i++) 70 { 71 if (0 == data[i]) 72 { 73 continue; 74 } 75 76 tmp = data[i]; 77 78 for (j = ASSIST_LENGTH-1; j >= 1; j--) 79 { 80 data[i+j] += (tmp / (unsigned long long)pow(10,j)); 81 tmp %= (unsigned long long)pow(10,j); 82 } 83 84 data[i] %= 10; 85 } 86 87 c_flag = true; 88 for (i = MAX_LENGTH - 1; i >= 0; i--) 89 { 90 if (c_flag && 0 == data[i]) 91 { 92 continue; 93 } 94 95 c_flag = false; 96 printf("%llu", data[i]); 97 } 98 printf("\n"); 99 100 return 0; 101 }
實現了任意大數與 2^64-1以下的數相乘,
兩個任意大數可以將其中一個拆分成多個因數,
兩個大數質數暫未考慮