Archive for the ‘Mathematical simulation’ Category

UVA – 371 – Ackermann Functions   1 comment


#include <stdio.h>
int sequenceLength(unsigned int n)
{
	int length = 0;
	do
	{
		length+=(1+(n&1));
		n = ((1 + (2 * (n & 1))) *  n + 1)>> 1;
	}while(n!=1);
	return length;
}
int main()
{
	int maxI,max,n,m,t,i,c;
	scanf("%d %d",&n,&m);
	while(n||m)
	{
		maxI = -1;
		max = -1;
		if(n>m)
		{
			t = m;
			m = n;
			n = t;
		}
		for(i=n;i<=m;i++)
		{
			c = sequenceLength(i);
			if(c>max)
			{
				maxI=i;
				max = c;
			}
		}
		printf("Between %d and %d, %d generates the longest sequence of %d values.\n",n, m, maxI, max);
		scanf("%d %d",&n,&m);
	}
	return 0;
}

Posted August 12, 2011 by epicrado in Mathematical simulation