-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_integer_modulo.c
More file actions
148 lines (143 loc) · 3.16 KB
/
big_integer_modulo.c
File metadata and controls
148 lines (143 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
5 kyu
Big Integer Modulo
https://www.codewars.com/kata/546e0773fa8da2013200087a
*/
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void remove_leading_zeros(char* r) {
char* prlast = r;
while (*prlast)
prlast++;
prlast--;
char* pr = r;
while (*pr == '0' && pr < prlast)
pr++;
if (r < pr)
memmove(r, pr, strlen(pr) + 1);
}
static char* subtract(const char* a, const char* b) {
char* result;
int la = strlen(a);
int lb = strlen(b);
int l = ((la > lb) ? la : lb);
char* aa = calloc(l + 1, sizeof(char));
char* bb = calloc(l + 1, sizeof(char));
for (int i = 0; i < l; i++) {
aa[i] = '0';
bb[i] = '0';
}
strcpy(aa + l - la, a);
strcpy(bb + l - lb, b);
int cmp = strcmp(aa, bb);
if (cmp == 0) {
result = calloc(2, sizeof(char));
strcpy(result, "0");
} else {
int lr = l + 1;
result = calloc(lr + 1, sizeof(char));
bool neg = false;
if (cmp < 0) {
neg = true;
char* temp = aa;
aa = bb;
bb = temp;
}
for (int i = 0; i < lr; i++)
result[i] = '0';
char* prlast = result + lr - 1;
char* pr = prlast;
char* pa = aa + l - 1;
char* pb = bb + l - 1;
int carryover = 0;
while (pa >= aa) {
int diff = *pa - *pb - carryover;
if (diff < 0) {
diff += 10;
carryover = 1;
} else
carryover = 0;
*pr = diff + '0';
pa--;
pb--;
pr--;
}
if (carryover > 0)
*pr = carryover + '0';
while (*pr == '0' && pr < prlast)
pr++;
if (neg) {
pr--;
*pr = '-';
}
if (result < pr)
memmove(result, pr, strlen(pr) + 1);
}
free(aa);
free(bb);
return result;
}
static char** divide_strings(const char* a, const char* b) {
int la = strlen(a);
int lb = strlen(b);
char** result = malloc(2 * sizeof(char*));
result[0] = calloc(la + 1, sizeof(char));
result[1] = calloc(la + 1, sizeof(char));
result[0][0] = '0';
result[1][0] = '0';
if (strcmp(a, "0") == 0 || strcmp(b, "0") == 0)
return result;
if (lb > la) {
strcpy(result[1], a);
return result;
}
if (strcmp(a, b) == 0) {
result[0][0] = '1';
return result;
}
char* aa = calloc(la + 2, sizeof(char));
char* diff;
int pos = 0;
strncpy(aa, a, lb);
for (int i = 0; i < la - lb + 1; i++) {
int count = 0;
while (true) {
diff = subtract(aa, b);
if (diff[0] == '-') {
free(diff);
break;
}
count++;
strcpy(aa, diff);
free(diff);
}
result[0][pos] = count + '0';
int laa = strlen(aa);
if (i + lb < la) {
aa[laa] = a[i + lb];
aa[laa + 1] = '\0';
}
pos++;
}
result[0][pos] = '\0';
strcpy(result[1], aa);
free(aa);
remove_leading_zeros(result[0]);
remove_leading_zeros(result[1]);
return result;
}
uint64_t big_modulo(const char* num_string, uint64_t divisor) {
char div_str[20];
uint64_t result;
sprintf(div_str, "%" PRIu64, divisor);
char** results = divide_strings(num_string, div_str);
sscanf(results[1], "%" PRIu64, &result);
free(results[0]);
free(results[1]);
free(results);
return result;
}