Last active
April 8, 2022 03:17
-
-
Save Trimad/62001cd9dedc465e861827dc908d931c to your computer and use it in GitHub Desktop.
UVa 100
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * * * * * * * * | |
* Tristan Madden | |
* 10/05/2017 | |
* uva100 | |
* * * * * * * * */ | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment