Pages

Sunday 9 September 2012

UVA - 100-The 3n + 1 problem

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
         Scanner s=new Scanner(System.in);
         while(s.hasNext()){
            int pair1=s.nextInt();
            int pair2=s.nextInt();
            int temp1=pair1,temp2=pair2;
            pair1=Math.min(temp1, temp2);
            pair2=Math.max(temp1, temp2);
            long max=1;
            for(int j=pair1;j<pair2+1;j++){
                long temp=getcycles(j);
                if(temp> max)
                   max=temp;
            }
            System.out.println(temp1+" "+temp2+" "+max);
        }
    }
    static long getcycles(long x){
        if(x==1)return 1;
        if(x%2!=0) return 1+getcycles(3*x+1);
        return 1+getcycles(x/2);
    }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete