Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

3x+1 in java

import java.util.*;

class solution {


public static int[] clength = new int[1000000];

public static long nextnum(long n)
{

    if(n%2==0)
    {
        return n/2;
    }
    else
        return 3*n+1;
}

public static long cyclelength(long n)
{

    if(n==1)
    {
        return 1;

    }
    if (n < 1000000 && clength[(int)n] != 0)
    {
        return clength[(int)n];

    }
    long length= (1+ cyclelength(nextnum(n)));
    if (n < 1000000)
        clength[(int)n] = (int) length;

    return length;
}

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);

    while(in.hasNext())
    {
        int a=in.nextInt();
        int b=in.nextInt();
        int min=Math.min(a, b);
        int max=Math.max(a, b);
        int count=0;

        for(int n=min;n<=max;n++)
        {
            count=(int) Math.max(count,cyclelength(n));

        }
        System.out.println(a + " " + b +" " +count );
    }
}

}
Comment

PREVIOUS NEXT
Code Example
Java :: how to print multi dimension array in java using for each loop 
Java :: javafx add vm options 
Java :: centrar valores en celda jtable java netbeans 
Java :: how to use set ForceDarkAllowed(false); in android studio 
Java :: modelmapper abstract class 
Java :: how many public classes can be allowed in java 
Java :: timer tick java 
Java :: kivy menu bar 
Java :: Removing Elements in java map 
Java :: editable column 
Java :: java reverse a array 
Java :: Java int Keyword 
Java :: on offline event spigot example 
Java :: ring check if the operating system is Android or not 
Java :: mock ioexception mockito on BufferedReader 
Java :: shuffle a string in java 
Java :: how to show the hex detail of a file in java 
Java :: online currency rate api 
Java :: SpringBootStarter maven dependency 
Java :: how to make jframe full screen 
Java :: constructors in java 
Java :: java secureRandom certain range 
Java :: how to extract a set of binary strings in java 
Java :: getcokor from drawable in java android studio 
Java :: fill a 2d array java 
Java :: @android:color/system_neutral1_1000 
Java :: datapicker java 
Java :: change replication factor hadoop cluster command 
Java :: square operator java 
Java :: chemistry formula on android 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =