-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInteger.c
232 lines (223 loc) · 5.77 KB
/
BigInteger.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "BigInteger.h"
#include <stdio.h>
#include <stdlib.h>
// Function to initialize a BigInteger
BigInteger* initialize()
{
BigInteger* bigInt = (BigInteger*)malloc(sizeof(BigInteger));
bigInt->head = NULL;
bigInt->tail = NULL;
bigInt->sign = 1;
return bigInt;
}
void atend(BigInteger* bigInt, int digit)
{
Node* new = (Node*)malloc(sizeof(Node));
new->digit = digit;
new->next = NULL;
new->prev = bigInt->tail;
if (bigInt->tail)
{
bigInt->tail->next = new;
} else
{
bigInt->head = new;
}
bigInt->tail = new;
}
BigInteger* createFromInt(int n)
{
BigInteger* bigInt = initialize();
if (n < 0)
{
bigInt->sign = -1;
n = -n;
}
while (n)
{
atend(bigInt, n % 10);
n /= 10;
}
return bigInt;
}
void display(BigInteger* bigInt)
{
if (bigInt->sign == -1)
printf("-");
Node* temp = bigInt->tail;
while (temp)
{
printf("%d", temp->digit);
temp = temp->prev;
}
printf("\n");
}
BigInteger* add(BigInteger* a, BigInteger* b)
{
// This function assumes both a and b are non-negative for simplicity.
BigInteger* result = initialize();
Node* A = a->head;
Node* B = b->head;
int carry = 0;
while (A || B || carry)
{
int sum = carry + (A ? A->digit : 0) + (B ? B->digit : 0);
atend(result, sum % 10);
carry = sum / 10;
if (A) A = A->next;
if (B) B = B->next;
}
return result;
}
int compare(BigInteger* a, BigInteger* b)
{
if (a->sign == 1 && b->sign == -1)
return 1;
if (a->sign == -1 && b->sign == 1)
return -1;
int multiplier = a->sign; // since both numbers have the same sign
Node* A = a->tail;
Node* B = b->tail;
while (A && B)
{
if (A->digit != B->digit)
{
return (A->digit >B->digit) ? 1 * multiplier : -1 * multiplier;
}
A = A->prev;
B = B->prev;
}
if (A)
return 1 * multiplier; // a is longer
if (B)
return -1 * multiplier; // b is longer
return 0; // equal
}
BigInteger* sub(BigInteger* a, BigInteger* b)
{
int cmp = compare(a, b);
if (cmp == 0)
{
BigInteger* result = initialize();
atend(result, 0);
return result;
}
BigInteger* big = cmp > 0 ? a : b;
BigInteger* small = cmp > 0 ? b : a;
BigInteger* result = initialize();
Node* tempB = big->head;
Node* tempS = small->head;
int borrow = 0;
while (tempB)
{
int differnce = tempB->digit - borrow - (tempS? tempS->digit : 0);
if (differnce < 0)
{
differnce += 10;
borrow = 1;
}
else
{
borrow = 0;
}
atend(result, differnce);
tempB= tempB->next;
if (tempS) tempS = tempS->next;
}
// Handle negative results
if (cmp < 0) result->sign = -1;
// Removing leading zeros (if any)
while (result->tail && result->tail->digit == 0)
{
Node* temp = result->tail;
result->tail = result->tail->prev;
if (result->tail) result->tail->next = NULL;
else result->head = NULL;
free(temp);
}
return result;
}
BigInteger* copy(BigInteger* bigInt)
{
BigInteger* newInt = initialize();
Node* current = bigInt->head;
while (current)
{
atend(newInt, current->digit);
current = current->next;
}
newInt->sign = bigInt->sign;
return newInt;
}
BigInteger* div1(BigInteger* divi, BigInteger* divisor)
{
if (divisor==0)
{
printf("Division by zero!\n");
return NULL; // Division by zero is undefined.
}
BigInteger* quotient = initialize();
BigInteger* Dividend = copy(divi);
while (compare(Dividend, divisor) >= 0)
{
Dividend = sub(Dividend, divisor);
BigInteger* oldQ = quotient;
quotient = add(quotient, createFromInt(1));
// Clean up old quotient from previous iteration
// This can be optimized further by incrementing the least significant digit of quotient directly
}
return quotient;
}
BigInteger* mod(BigInteger* divi, BigInteger* divisor)
{
if (divisor==0)
{
printf("Division by zero!\n");
return NULL; // Division by zero is undefined
}
BigInteger* Dividend = copy(divi);
while (compare(Dividend, divisor) >= 0)
{
BigInteger* Temp = Dividend;
Dividend = sub(Dividend, divisor);
free(Temp); // Cleaning up the temporary variable.
}
return Dividend; // The remainder after all possible subtractions.
}
BigInteger* mul(BigInteger* a, BigInteger* b)
{
BigInteger* prod = initialize();
atend(prod, 0); // Initialize to zero
if (a==0|| b==0)
return prod;
int posA = 0;
for (Node* nodeA = a->head; nodeA != NULL; nodeA = nodeA->next)
{
BigInteger* temp = initialize();
for (int i = 0; i < posA; i++)
{
atend(temp, 0);
}
int carry = 0;
int posB = 0;
for (Node* nodeB = b->head; nodeB != NULL; nodeB = nodeB->next)
{
int mult = nodeA->digit * nodeB->digit + carry;
atend(temp, mult % 10);
carry = mult / 10;
posB++;
}
while (carry)
{
atend(temp, carry % 10);
carry /= 10;
}
BigInteger* old = prod;
prod = add(prod, temp);
free(old);
free(temp);
posA++;
}
prod->sign = a->sign * b->sign;
return prod;
}