Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- //needed some counter variables for the amount of times I call my three different algorithms
- private static int amountOfOriginalCalls = 0;
- private static int secondFastestCalling = 0;
- private static int fastestCalling = 0;
- //these variables are for the 3n+1 algorithm
- private static int amountOfOriginalCallsThreeN = 0;
- private static int secondFastestCallingThreeN = 0;
- private static int fastestCallingThreeN = 0;
- public static void main(String[] args) {
- //-------------Start g(n)= 1+g(n/2) or 1+g(n-1) code---------------
- //first lets call the original algorithm
- for(int n = 1; n<10000; n++){
- originalAlgorithm(n);
- }
- System.out.println("The amount of times the 'Original Algorithm' method was called is: " + amountOfOriginalCalls);
- //next call the second one
- for(int n = 1; n<10000; n++){
- secondFastestCalling(n);
- }
- System.out.println("The amount of times the 'Second Fastest' method was called is: " + secondFastestCalling);
- //lastly called the fastest one
- for(int n = 1; n<10000; n++){
- fastestCalling(n);
- }
- System.out.println("The amount of times the 'Fastest' method was called is: " + fastestCalling);
- //-------------End g(n)= 1+g(n/2) or 1+g(n-1) code---------------
- //-------------Start g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
- //first lets call the original algorithm
- for(int n = 1; n<10000; n++){
- originalAlgorithmThreeN(n);
- }
- System.out.println("The amount of times the 'Original Algorithm w/ 3n+1' method was called is: " + amountOfOriginalCallsThreeN);
- //next call the second one
- for(int n = 1; n<10000; n++){
- secondFastestCallingThreeN(n);
- }
- System.out.println("The amount of times the 'Second Fastest w/ 3n+1' method was called is: " + secondFastestCallingThreeN);
- //lastly called the fastest one
- for(int n = 1; n<10000; n++){
- fastestCallingThreeN(n);
- }
- System.out.println("The amount of times the 'Fastest w/ 3n+1' method was called is: " + fastestCallingThreeN);
- //-------------End g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
- }
- //-------------Start g(n)= 1+g(n/2) or 1+g(n-1) code---------------
- //this original algorithm is a little round about, having two recursive methods to make the call for the math
- public static int originalAlgorithm(int n){
- amountOfOriginalCalls++;
- if( n == 1) return 1;
- int time = 1 + originalAlgorithm(computeVal(n));
- return n;
- }
- //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
- public static int computeVal(int n){
- amountOfOriginalCalls++;
- if((n%2)==0) {n=n/2;} else { n=n-1;}
- return n;
- }
- //this is the second fastest way to do it, just looping inside of itself making the returns do the math for us
- public static int secondFastestCalling(int n){
- secondFastestCalling++;
- if(n==1)return 1;
- if((n%2)==0) {return 1+secondFastestCalling(n/2);} else { return 1+secondFastestCalling(n-1);}
- }
- //this is the fastest method. It combines the odd with the even call so that it get to skip a step basically
- public static int fastestCalling(int n){
- fastestCalling++;
- if(n==1)return 1;
- if((n%2)==0) {return 1+fastestCalling(n/2);} else {return 1+fastestCalling((n-1)/2);}
- }
- //-------------End g(n)= 1+g(n/2) or 1+g(n-1) code---------------
- //-------------Start g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
- //this original algorithm is a little round about, having two recursive methods to make the call for the math
- public static int originalAlgorithmThreeN(int n){
- amountOfOriginalCallsThreeN++;
- if( n == 1) return 1;
- int time = 1 + originalAlgorithmThreeN(computeVal(n));
- return n;
- }
- //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
- public static int computeValThreeN(int n){
- amountOfOriginalCallsThreeN++;
- if((n%2)==0) {n=n/2;} else { n=(3*n)+1;}
- return n;
- }
- //this is the second fastest way to do it, just looping inside of itself making the returns do the math for us
- public static int secondFastestCallingThreeN(int n){
- secondFastestCallingThreeN++;
- if(n==1)return 1;
- if((n%2)==0) {return 1+secondFastestCallingThreeN(n/2);} else { return 1+secondFastestCallingThreeN((3*n)+1);}
- }
- //this is the fastest method. It combines the odd with the even call so that it get to skip a step basically
- public static int fastestCallingThreeN(int n){
- fastestCallingThreeN++;
- if(n==1)return 1;
- if((n%2)==0) {return 1+fastestCallingThreeN(n/2);} else {return 1+fastestCallingThreeN(((3*n)+1)/2);}
- }
- //-------------End g(n)= 1+g(n/2) or 1+g(3n+1) code---------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement