-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigInt.h
107 lines (94 loc) · 2.36 KB
/
BigInt.h
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
// BigInt.h - Header file to provide the BigInt datatype and support functions.
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef __int128_t BigInt; // datatype to hold really big integer values
// Returns the given string as BigInt, e.g. stringToBigInt("123") returns 123.
static BigInt stringToBigInt(const char *str)
{
BigInt sign = 1, value = 0;
if (*str == '+')
str++;
else if (*str == '-')
{
sign = -1;
str++;
}
for (size_t i = 0; i < strlen(str); ++i)
value = (value * 10) + (str[i] - '0');
return sign * value;
}
// Returns base and exponent as BigInt, e.g. baseAndExponentToBigInt(10,3) returns 1000.
static BigInt baseAndExponentToBigInt(BigInt base, BigInt exponent)
{
BigInt result = 1;
for (BigInt i = 0; i < exponent; ++i)
result *= base;
return result;
}
// Converts the given BigInt to a string.
static void BigIntToString(BigInt n, char *str)
{
char buf[41] = {}, *bufPtr = buf + sizeof(buf) - 1; // start at the end
if (n < 0)
{
n = -n;
*str++ = '-';
}
do
{
*--bufPtr = "0123456789"[n % 10]; // save last digit
n /= 10; // drop it
} while (n);
strcpy(str, bufPtr);
}
// Special printf() to support "%B" for BigInt variables.
static void printfBigInts(const char* formatString, ...)
{
char buf[1024], *bufPtr = buf;
va_list ptr;
va_start(ptr, formatString);
for (int i = 0; formatString[i] != '\0'; ++i)
{
if (formatString[i] == '%')
{
if (formatString[++i] == 'B')
{
BigIntToString(va_arg(ptr, BigInt), bufPtr);
bufPtr += strlen(bufPtr);
}
}
else
*bufPtr++ = formatString[i];
}
*bufPtr = '\0';
fprintf(stdout, "%s\n", buf);
fflush(stdout); // to disable buffering
va_end(ptr);
}
static BigInt _cubeNumbers[100000];
static void preCalculateCubeNumbers(void)
{
for (BigInt i = 0; i < 100000; i++)
_cubeNumbers[i] = i * i * i;
}
static BigInt cubeNumber(BigInt n)
{
return n < 100000 ? _cubeNumbers[n] : n*n*n;
}
// Returns the cube root of the given number, e.g. cubicRoot(27) returns 3.
static BigInt cubicRoot(BigInt n)
{
BigInt start = 0, end = n; // set start and end for binary search
for (;;)
{
const BigInt mid = (start + end) / (BigInt)2;
if (start + 1 >= end)
return mid;
if ((mid*mid*mid) > n)
end = mid;
else
start = mid;
}
}