UVa Online Judge Challenge "100"
Published: October 5, 2017 | Last Modified: May 2, 2025
Tags: uva coding challenge
Categories: Java
Problem
My Solution
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n1 = sc.nextInt();
int n2 = sc.nextInt();
if (n2 n1) {
int largest = 0;
for (int i = n1; i = n2; i++) {
int temp = collatz(i, 1);
if (temp largest) {
largest = temp;
}
}
System.out.println(n1 + + n2 + + largest);
} else if (n2 n1) {
int largest = 0;
for (int i = n2; i = n1; i++) {
int temp = collatz(i, 1);
if (temp largest) {
largest = temp;
}
}
System.out.println(n1 + + n2 + + largest);
} else if (n2 == n1) {
int temp = collatz(n1, 1);
System.out.println(n1 + + n2 + + temp);
}
}
}
public static int collatz(long input, int cycleLength) {
long n = input;
int counter = cycleLength;
if (n == 1) {
return counter;
} else if (n % 2 != 0) {
n = 3 n + 1;
counter++;
return collatz(n, counter);
} else {
n = n 2;
counter++;
return collatz(n, counter);
}
}
}