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 8367315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:07:34+00:00 2026-06-09T13:07:34+00:00

This is a program I made to simulate the Collatz Conjecture on Java: import

  • 0

This is a program I made to simulate the Collatz Conjecture on Java:

import java.util.*;
public class Collatz {
public static void main(String args[]){
    Scanner raj= new Scanner(System.in);
    int n;
    int k=0;
    System.out.print("n? ");
    n = raj.nextInt();
    while(n > 1){
        if(n%2 ==1){
            n=3*n+1;
            System.out.println(n);
            k++;
        }
        if(n%2==0){
            n=n/2;
            System.out.println(n);
            k++;
        }

    }
    System.out.print("It took " + k + " iterations!");
}

}

When I put in n=6, I get

3
10
5
16
8
4
2
1
It took 8 iterations!

But when I put, say, n= 63728127, I get

191184382
95592191
286776574
143388287
430164862
215082431
645247294
322623647
967870942
483935471
1451806414
725903207
-2117257674
-1058628837
It took 14 iterations!

What went wrong? Why? How can I fix it? Thanks!

  • 1 1 Answer
  • 0 Views
  • 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-06-09T13:07:36+00:00Added an answer on June 9, 2026 at 1:07 pm

    This is a classic case of integer overflow. Primitive ints have a limited range in Java. The solution is to always use something like BigInteger if you have to deal with large integers.

    Incidentally, things would be so much easier if Java supported operator overloading like almost all other modern languages.

    import java.util.*;
    import java.math.BigInteger;
    
    
    public class Collatz {
        public static void main(String args[]){
            Scanner raj= new Scanner(System.in);
            int k=0;
            System.out.print("n? ");
    
            BigInteger n = BigInteger.valueOf(raj.nextLong());
    
            while(n.compareTo(BigInteger.ONE) > 0){
                if(n.testBit(0)){
                    n = n.multiply(BigInteger.valueOf(3));
                    n = n.add(BigInteger.ONE);
                    System.out.println(n);
                    k++;
                }
                else {
                    n = n.divide(BigInteger.valueOf(2));
                    System.out.println(n);
                    k++;
                }
            }
            System.out.print("It took " + k + " iterations!");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program returns: package main import ( flag fmt ) func main() { num_agents
I made this small Java program using eclipse IDE. I have set the workspace
I got this program, that I made: http://www.2shared.com/file/73q6K9Yy/testv.html The Login.java is the servlet, that
I made this program for a school, and it involves a telnet server. When
I have made a program that scans rss feeds. This same program creates feeds
So I made this (very simple) program with a swing GUI with NetBeans, and
this program is for class to display the database in Listview.i want to add
Okay so I made this program to help me out with my homework and
I made this program just out of interest and wanted to make it better.
I built this program that works with files. Since I made it for a

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.