Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:25:57+00:00 2026-05-14T03:25:57+00:00

I’m trying to solve this problem, its not a homework question, its just code

  • 0

I’m trying to solve this problem, its not a homework question, its just code I’m submitting to uva.onlinejudge.org so I can learn better java trough examples. Here is the problem sample input :

 3 100
 34 100
 75 250
 27 2147483647
 101 304
 101 303
 -1 -1

Here is simple output :

 Case 1: A = 3, limit = 100, number of terms = 8
 Case 2: A = 34, limit = 100, number of terms = 14
 Case 3: A = 75, limit = 250, number of terms = 3
 Case 4: A = 27, limit = 2147483647, number of terms = 112
 Case 5: A = 101, limit = 304, number of terms = 26
 Case 6: A = 101, limit = 303, number of terms = 1

The thing is this has to execute within 3sec time interval otherwise your question won’t be accepted as solution, here is with what I’ve come up so far, its working as it should just the execution time is not within 3 seconds, here is code :

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int start;
    int limit;
    int terms;
    int a = 0;

    while (stdin.hasNext()) {
      start = stdin.nextInt();
      limit = stdin.nextInt();
      if (start > 0) {
        terms = getLength(start, limit);
        a++;
      } else {
        break;
      }
      System.out.println("Case "+a+": A = "+start+", limit = "+limit+", number of terms = "+terms);
    }
  }

  public static int getLength(int x, int y) {
    int length = 1;
    while (x != 1) {
      if (x <= y) {
        if ( x % 2 == 0) {
          x = x / 2;
          length++;
        }else{
          x = x * 3 + 1;
          length++;
        }
      } else {
        length--;
        break;
      }
    }

    return length;
  }
}

And yes here is how its meant to be solved :

An algorithm given by Lothar Collatz produces sequences of integers, and is described as follows:

Step 1:
    Choose an arbitrary positive integer A as the first item in the sequence. 
Step 2:
    If A = 1 then stop. 
Step 3:
    If A is even, then replace A by A / 2 and go to step 2. 
Step 4:
    If A is odd, then replace A by 3 * A + 1 and go to step 2. 

And yes my question is how can I make it work inside 3 seconds time interval?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:25:58+00:00Added an answer on May 14, 2026 at 3:25 am

    From Googling I found this thread where a couple of other people have had the same problem and the solution is to use 64-bit arithmetic instead of 32-bit arithmetic.

    Try changing int to long and see if that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Thanks, turns out it was TreeViewItem.Style CAN be set to… May 15, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer How about... DELETE FROM Scores S1 WHERE Score < (SELECT… May 15, 2026 at 4:52 pm
  • Editorial Team
    Editorial Team added an answer The extension method IEnumerable.Distinct is not a predicate function. It… May 15, 2026 at 4:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.