Skip to content

Instantly share code, notes, and snippets.

@sanxiyn
Created April 18, 2011 19:53
Show Gist options
  • Save sanxiyn/926042 to your computer and use it in GitHub Desktop.
Save sanxiyn/926042 to your computer and use it in GitHub Desktop.
2^100000
#include <stdio.h>
#include <stdlib.h>
#define BASE 10000
struct number {
int n;
int *a;
};
typedef struct number *N;
N allocate_number(int n) {
N v;
v = (N)malloc(sizeof(struct number));
v->n = n;
v->a = (int *)calloc(n, sizeof(int));
return v;
}
void free_number(N a) {
free(a->a);
free(a);
}
N make_number(int i) {
N v;
v = allocate_number(1);
v->a[0] = i;
return v;
}
N multiply_number(N a, N b) {
int i, j, k;
int da, db, carry;
N c = allocate_number(a->n + b->n);
for (i = 0; i < a->n; i++) {
da = a->a[i];
carry = 0;
for (j = 0, k = i; j < b->n; j++, k++) {
db = b->a[j];
carry += c->a[k] + da * db;
c->a[k] = carry % BASE;
carry /= BASE;
}
if (carry)
c->a[k] += carry;
}
return c;
}
void print_number(N a) {
int i, d;
int first = 1;
for (i = a->n-1; i > -1; i--) {
d = a->a[i];
if (d) {
printf(first ? "%d" : "%04d", d);
first = 0;
}
}
printf("\n");
}
N tenth_power_of_number(N a) {
N a2, a4, a5, a10;
a2 = multiply_number(a, a);
a4 = multiply_number(a2, a2);
a5 = multiply_number(a4, a);
a10 = multiply_number(a5, a5);
free_number(a2);
free_number(a4);
free_number(a5);
return a10;
}
int main() {
int i;
N a, b;
a = make_number(2);
for (i = 0; i < 5; i++) {
b = tenth_power_of_number(a);
free_number(a);
a = b;
}
print_number(a);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment