isefire

CollatzExtended

Jul 15th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. /* Collatz
  2.  * <Description>
  3.  * Famous unsolved problem in number theory, known as the Collatz problem.
  4.  * This program prints the number less than N(runtime argument) that has the
  5.  * largest number of iterations through collatz(), and then prints that
  6.  * number.
  7.  *
  8.  * <Usage>
  9.  * %javac Collatz.java
  10.  *
  11.  * %java Collatz 3
  12.  * Value of n for which n < N # of recursive calls for collatz(n) is maximized: 2
  13.  * Value of # of recursive calls: 2
  14.  *
  15.  * java Collatz 7
  16.  * Value of n for which n < N # of recursive calls for collatz(n) is maximized: 6
  17.  * Value of # of recursive calls: 9
  18.  *
  19.  *
  20.  * java Collatz 846
  21.  * Value of n for which n < N # of recursive calls for collatz(n) is maximized: 703
  22.  * Value of # of recursive calls: 171
  23.  *
  24.  * <References>
  25.  * CIS201-L UAB SUMMER 2014 Dr. Sloan
  26.  * http://introcs.cs.princeton.edu/java/23recursion/Collatz.java.html
  27.  *
  28.  * @author
  29.  * Dan Latham <[email protected]>
  30.  *
  31.  * @version 0.0.1
  32.  *
  33.  */
  34. public class Collatz
  35. {
  36.     public static int strip(String word)
  37.     {
  38.         int length = word.split(" ").length;
  39.         return length;
  40.     }
  41.     public static String collatz(int n, String word)
  42.     {
  43.         if (n == 1) return word;
  44.         else if (n % 2 == 0)
  45.         {
  46.             word = word+" "+n/2;
  47.             //System.out.println(word);
  48.             word = collatz(n / 2, word);
  49.         }
  50.         else
  51.         {
  52.             word = word+" "+(3*n+1);
  53.             //System.out.println(word);
  54.             word = collatz(3*n + 1, word);
  55.         }
  56.         return word;
  57.     }
  58.     public static void main(String[] args)
  59.     {
  60.         int n = Integer.parseInt(args[0])-1;
  61.         int placeholder_n = n;
  62.         int placeholder_length = 0;
  63.         String word = "";
  64.         while (n > 1)
  65.         {
  66.             word = collatz(n, String.format("%s", n));
  67.             //DEBUG System.out.println(word);
  68.             int z = strip(word);
  69.             if (z > placeholder_length)
  70.             {
  71.                 placeholder_length = z;
  72.                 placeholder_n = n;
  73.             }
  74.             n -= 1;
  75.         }
  76.         //DEBUG System.out.println(word);
  77.         System.out.println("Value of n for which n < N # of recursive ca"+
  78.                             "lls for collatz(n) is maximized: "+
  79.                             placeholder_n);
  80.         System.out.println("Value of # of recursive calls: "+
  81.                             placeholder_length);
  82.     }
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment