Skip to content
Archive

UVa Online Judge Challenge "100"

“Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs. Consider the following algorithm…”

UVa Online Judge Challenge "100"

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);
        }

    }
}

Related Reading

Related entries

Selected from shared tags, categories, and nearby entries in the same section.

Relationship Map

Connected Memory

This relationship map centers on the current entry and highlights connected categories and tags.

Categories 0
Tags 0
Entries 0