#include <stdio.h> /*

# The shell script
set -x                                # to see the commands
gcc -W -Wall -O3 -march=athlon-xp -fomit-frame-pointer -o "${0%%.c}" "$0"
exit                                  # exit shell, the rest is C code

*/

#include <stdlib.h>
#include <string.h>
#include <assert.h>

char *allocate(char *p, int old_sz, int new_sz)
{
	char *q;
	assert(old_sz < new_sz);
	if(p)
	{
		q = realloc(p, new_sz*sizeof(char));
	} else
	{
		q = malloc(new_sz*sizeof(char));
	}
	
	if(q)
	{
		memset(q+old_sz, 0, new_sz-old_sz);
		printf("alloced %d\n", new_sz);
		return q;
	}
	free(p);
	exit(1);
}


const int base = 7;
/*const int power = 387420489;*/
const int power = 100000;

/* This program computes
 * 'base' to the power of 'power'
 * using integers and decade coding.
 * Arbitrary large numbers supported as
 * the result.
 * 'base' should a "small" number.
 */

const int len_unit = 1024;


int main()
{
	int cur_sz = 0;
	char *p = NULL;
	int i = 0;
	int carry, pos, len;
	
	p = allocate(p, cur_sz, cur_sz+len_unit);
	cur_sz += len_unit;
	p[0] = 1;
	len = 1;

	for(i=0; i<power; i++)
	{
		if(len > cur_sz-6)
		{
			p = allocate(p, cur_sz, cur_sz+len_unit);
			cur_sz += len_unit;
		}
		
		carry = 0;
		for(pos=0; ; )
		{
			carry += p[pos] * base;
			p[pos] = carry % 10;
			carry = carry / 10;

			pos++;
			if(pos==len)
			{
				if(carry==0)
					break;
				len++;
			}
		}
	}
	
	for(pos=len; pos>0; )
	{
		pos--;
		printf("%d", p[pos]);
	}
	printf("\n");
	
	free(p);
	
	return 0;
}


