Muhammed Imran Hussain

LAMP/LEMP, MEAN, JAVA Developer

UVA 100 – The 3n + 1 Problem (Java Solution)

Problem Link | Download Code

3 Comments

  1. import java.util.Scanner;

    class Main{
    public static void main(String[] arg){
    int count, max, min, maxCycle=0;
    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();
    int j = sc.nextInt();

    //for checking maximum and minimum between nums
    if(i>j){
    max=i;
    min=j;
    }
    else{
    max=j;
    min=i;
    }

    //loop for every number between i and j
    for(int loop = min; loop=1){
    if(num==1){
    count++;
    break;
    }
    else if(num%2==0){
    num=num/2;
    count++;
    }
    else{
    num = num*3+1;
    count++;
    }

    }
    //check for maximum cycle
    if(count>maxCycle){
    maxCycle=count;
    }
    }
    System.out.println(i + ” ” + j + ” ” + maxCycle);
    }
    }

    bhai, i’m getting outputs same as what was on the sample. but problem is uva saying, “Although your program was successful at compilation and execution stages, it was not able of solving the proposed problem.”

    what is the problem here then?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.