Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Ackermann Functions

371.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))

int length(int len, long num)
{
	if (num<=0) {
		return len;
	} else if ((num==1) && (len!=0)) {
		return len;
	} else if (num%2) {
		return length(len+1, 3*num+1);
	} else {
		return length(len+1, num/2);
	}
}

int main(int argc, char *argv[])
{
	char buf[1024];

	while(fgets(buf, 1024, stdin)) {
		long a, b;
		long i;
		int li;
		long max;
		int lmax;
		if (sscanf(buf, "%ld %ld", &a, &b) != 2 ) {
			exit(1);
		}
		max=0;
		lmax=0;
		if (a==0 && b==0) {
			exit(0);
		}
		for(i=MIN(a,b); i<=MAX(a,b); i++) {
			li = length(0,i);
			if (li>lmax) {
				max=i;
				lmax=li;
			}
		}
		printf("Between %ld and %ld, %ld generates the longest sequence of %d values.\n",
			MIN(a,b), MAX(a,b), max, lmax);
	}
	exit(0);
}