Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1.  
  2. public class Main {
  3.    
  4.     //needed some counter variables for the amount of times I call my three different algorithms
  5.     private static int amountOfOriginalCalls = 0;
  6.     private static int secondFastestCalling = 0;
  7.     private static int fastestCalling = 0;
  8.  
  9.     //these variables are for the 3n+1 algorithm
  10.     private static int amountOfOriginalCallsThreeN = 0;
  11.     private static int secondFastestCallingThreeN = 0;
  12.     private static int fastestCallingThreeN = 0;
  13.    
  14.     public static void main(String[] args) {
  15.         //-------------Start g(n)= 1+g(n/2) or 1+g(n-1) code---------------
  16.         //first lets call the original algorithm
  17.         for(int n = 1; n<10000; n++){
  18.             originalAlgorithm(n);  
  19.         }
  20.         System.out.println("The amount of times the 'Original Algorithm' method was called is: " + amountOfOriginalCalls);
  21.         //next call the second one
  22.         for(int n = 1; n<10000; n++){
  23.             secondFastestCalling(n);
  24.         }
  25.         System.out.println("The amount of times the 'Second Fastest' method was called is: " + secondFastestCalling);
  26.         //lastly called the fastest one
  27.         for(int n = 1; n<10000; n++){
  28.             fastestCalling(n);
  29.         }
  30.         System.out.println("The amount of times the 'Fastest' method was called is: " + fastestCalling);   
  31.         //-------------End g(n)= 1+g(n/2) or 1+g(n-1) code---------------
  32.        
  33.         //-------------Start g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
  34.         //first lets call the original algorithm
  35.         for(int n = 1; n<10000; n++){
  36.             originalAlgorithmThreeN(n);
  37.         }
  38.         System.out.println("The amount of times the 'Original Algorithm w/ 3n+1' method was called is: " + amountOfOriginalCallsThreeN);
  39.         //next call the second one
  40.         for(int n = 1; n<10000; n++){
  41.             secondFastestCallingThreeN(n);
  42.         }
  43.         System.out.println("The amount of times the 'Second Fastest w/ 3n+1' method was called is: " + secondFastestCallingThreeN);
  44.         //lastly called the fastest one
  45.         for(int n = 1; n<10000; n++){
  46.             fastestCallingThreeN(n);
  47.         }
  48.         System.out.println("The amount of times the 'Fastest w/ 3n+1' method was called is: " + fastestCallingThreeN); 
  49.         //-------------End g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
  50.     }
  51.    
  52.     //-------------Start g(n)= 1+g(n/2) or 1+g(n-1) code---------------
  53.     //this original algorithm is a little round about, having two recursive methods to make the call for the math
  54.     public static int originalAlgorithm(int n){
  55.         amountOfOriginalCalls++;
  56.         if( n == 1) return 1;
  57.         int time = 1 + originalAlgorithm(computeVal(n));
  58.         return n;
  59.     }
  60.     //this is part of the original algorithm that does the math. It does the computation saving it in n, then returns n to the method above
  61.     public static int computeVal(int n){
  62.         amountOfOriginalCalls++;
  63.         if((n%2)==0) {n=n/2;} else { n=n-1;}
  64.         return n;
  65.     }
  66.     //this is the second fastest way to do it, just looping inside of itself making the returns do the math for us
  67.     public static int secondFastestCalling(int n){
  68.         secondFastestCalling++;
  69.         if(n==1)return 1;
  70.         if((n%2)==0) {return 1+secondFastestCalling(n/2);} else { return 1+secondFastestCalling(n-1);}
  71.     }
  72.     //this is the fastest method. It combines the odd with the even call so that it get to skip a step basically
  73.     public static int fastestCalling(int n){
  74.         fastestCalling++;
  75.         if(n==1)return 1;
  76.         if((n%2)==0) {return 1+fastestCalling(n/2);} else {return 1+fastestCalling((n-1)/2);}
  77.     }
  78.     //-------------End g(n)= 1+g(n/2) or 1+g(n-1) code---------------
  79.    
  80.     //-------------Start g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
  81.     //this original algorithm is a little round about, having two recursive methods to make the call for the math
  82.     public static int originalAlgorithmThreeN(int n){
  83.         amountOfOriginalCallsThreeN++;
  84.         if( n == 1) return 1;
  85.         int time = 1 + originalAlgorithmThreeN(computeVal(n));
  86.         return n;
  87.     }
  88.     //this is part of the original algorithm that does the math. It does the computation saving it in n, then returns n to the method above
  89.     public static int computeValThreeN(int n){
  90.         amountOfOriginalCallsThreeN++;
  91.         if((n%2)==0) {n=n/2;} else { n=(3*n)+1;}
  92.         return n;
  93.     }
  94.     //this is the second fastest way to do it, just looping inside of itself making the returns do the math for us
  95.     public static int secondFastestCallingThreeN(int n){
  96.         secondFastestCallingThreeN++;
  97.         if(n==1)return 1;
  98.         if((n%2)==0) {return 1+secondFastestCallingThreeN(n/2);} else { return 1+secondFastestCallingThreeN((3*n)+1);}
  99.     }
  100.     //this is the fastest method. It combines the odd with the even call so that it get to skip a step basically
  101.     public static int fastestCallingThreeN(int n){
  102.         fastestCallingThreeN++;
  103.         if(n==1)return 1;
  104.         if((n%2)==0) {return 1+fastestCallingThreeN(n/2);} else {return 1+fastestCallingThreeN(((3*n)+1)/2);}
  105.     }
  106.     //-------------End g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement