-1

I am new to java programming language. Why there are different output in this code?Can explain the problem to me?Thank you very much. public class Collatz {

public static void main(String[]arguments)
{   int max=0;
    int real=0;
    int a=0;



    for (int i=2;i<1000000;i++)
    {   
        real=i;
        int count=1;
        while(real>1)
        {
            if(real%2==0)
            {
                real=real/2;


            }
            else
            {
                real=3*real+1;


            }
            count++;
            }
            if(count>max){max=count;a=i;}
        }
        System.out.println(a+"&"+max);}

Output is 910107&476 public class Collatz {

public static void main(String[]arguments)
{   long max=0;
    long real=0;
    long a=0;



    for (int i=2;i<1000000;i++)
    {   
        real=i;
        int count=1;
        while(real>1)
        {
            if(real%2==0)
            {
                real=real/2;


            }
            else
            {
                real=3*real+1;


            }
            count++;
            }
            if(count>max){max=count;a=i;}
        }
        System.out.println(a+"&"+max);}

Output is 837799&525 Can tell me what is the problem?Thank you.

F.Long
  • 1
  • 1
    Because your value overflow the `int` (and `long` probably). So you reach the maximum value and start back to the minimal ("bigger" negative value) and keep working with that. Both `int` and `long` have a different maximum value, so the overflow occurs at different moment, giving a different result. **EDIT** : For "unlimited" numerical value, you have a class `BigInteger` and `BigDecimal` but they are more expensive to use, so this will take some time to execute (perf vs precision is a choice) – AxelH Sep 07 '17 at 06:59
  • You can check the relations of using these particular data types here. https://stackoverflow.com/questions/32608153/why-not-use-long-for-all-integer-values – Procrastinator Sep 07 '17 at 07:06
  • Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Sep 07 '17 at 07:09
  • In your case: always clearly describe *expected* and *actual* results. Dont leave it to use to first figure what your code is doing and printing, to then spent further thinking on identifying your potential problem. – GhostCat Sep 07 '17 at 07:10
  • @F.Long you are welcome, read the answers and don't hesitate to upvote those. Are you sure about your logic ? – AxelH Sep 07 '17 at 07:18
  • @AxelH Is that any problem to my code and logic?I am a beginner to Java language.If i got any wrong , please show me . I want to Improve myself. – F.Long Sep 07 '17 at 11:31
  • @LegendaryGoldCat Sorry.Today is my first day to use stackoverflow .Sorry for causing problem to you. May i know what website (like ProjectEuler) or book could help to improve my fundamental of java ? – F.Long Sep 07 '17 at 11:34
  • @F.Long No problem. That is why I give you feedback. If you want to learn java **basics** - then dont turn to *code puzzles*. If you really want to learn the *basics* ... start here: https://docs.oracle.com/javase/tutorial/ ... and work your way top to bottom (more or less) – GhostCat Sep 07 '17 at 11:37

2 Answers2

0

Because your value overflow the int (and long probably).

for (int i=2;i<1000000;i++){   
    real=i;
    int count=1;
    while(real>1)

You will loop a lot of time, especially with the logic in the while loop

Since you do a multiplication, the value will rise really fast and reach the maximum value and start back to the minimal ("bigger" negative value) and keep working with that.

Both int and long have a different maximum value, so the overflow occurs at different moment, giving a different result.

You can find the length of each primitive type here but following is the interesting part :

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2^64-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

For "unlimited" numerical value, you have the class BigInteger and BigDecimal but they are more expensive to use, so this will take some time to execute (perf vs precision is a choice)

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
0

For that you actually just need to look up the range of both variable types: http://www.cafeaulait.org/course/week2/02.html

To keep it simple: The value you are calculating here is most definitely too long for an Integer as it goes from -2,147,483,648 to 2,147,483,647 and that probably goes the same for long.