Pages

Sunday 7 October 2012

UVA - 371 - Ackermann Functions

    #include <stdio.h>

    long func(long x){
        if(x==1)
            return 0;
        if(x%2==0)
            return 1+func(x/2);
        return 1+func((3*x)+1);
    }

    long max(long x,long y){
        if(x>y)
            return x;
        return y;
    }

    long min(long x,long y){
        if(x<y)
            return x;
        return y;
    }

    int main(){
        int i;
        long p,q;
        while(1){
            scanf("%ld %ld",&p,&q);
            if(p==0 && q==0)
                break;
            long tempP=p,tempQ=q;
            p=min(tempP,tempQ);
            q=max(tempP,tempQ);
            long max=0,index;
            if(p==1){
                max=3;
                index=1;
            }
            for(i=p;i<q+1;i++){
                long temp=func(i);
                if(max<temp){
                    max=temp;
                    index=i;
                }
            }

            printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n"
                   ,p,q,index,max);
        }
        return 0;
    }

No comments:

Post a Comment