All pastes #1959280 Raw Edit

Someone

public text v1 · immutable
#1959280 ·published 2010-10-11 13:14 UTC
rendered paste body
#include <stdio.h> 
#include <memory.h> 
#include <string.h> 

struct BigInteger 
{
	/* caution for possible overflow! */
    unsigned int digits[10000]; 
}; 

void mul(unsigned int a[10000], unsigned int b, unsigned int c[10000])
{
    for (int i=0; i<10000; i++)
        c[i] = a[i] * b;
	
    for (int i=0; i<10000-1; i++)
    {
        c[i+1] += c[i] / 10;
        c[i] %= 10;
    }
}

void print(struct BigInteger *num)
{
	unsigned int *a = num->digits;
	int i = 10000-1;
	while (i > 0) {
		if (a[i] == 0)
			i--;
		else 
			break;
	}
	
	for (; i >= 0; i--) {
		printf("%d", a[i]);
	}
}

int main(int argc, char *argv[]) 
{ 
	/* create two big integers. one is temporary for calculations
	 and one holds the final sum. */
	struct BigInteger s1;
	struct BigInteger s2;
	
	/* set all bits to 0. */
	memset(s1.digits, 0, sizeof(struct BigInteger));
	memset(s2.digits, 0, sizeof(struct BigInteger));
	
	struct BigInteger *tmp = &s1;
	struct BigInteger *sum = &s2;
	
	struct BigInteger *swap_holder = NULL;
	
	sum->digits[0] = 1;

	for (int i = 2; i <= 500; i++) {
		/* swap tmp and sum. */
		swap_holder = tmp;
		tmp = sum;
		sum = swap_holder;
		
		/* do multiplication. */
		mul(tmp->digits, i, sum->digits);
		//print(sum->digits);
	}
	print(sum);
}