Advertisement
earlution

Factorial - iterative & recursive

Nov 9th, 2021 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Write a description of class FactorialIterativeRecursive here.
  5.  *
  6.  * @author (Ruari Mears)
  7.  * @version (1.1.3)
  8.  */
  9. public class FactorialIterativeRecursive
  10. {
  11.     /**
  12.      * Uses nanoTime() to count execution time for each approach.
  13.      */
  14.     public static void main(String[] args) {
  15.         Scanner scanner = new Scanner(System.in);
  16.         int input;
  17.         int method;
  18.         long begin;
  19.         long end;
  20.         long timeElapsed;
  21.  
  22.         System.out.println("Calculating factorial.");
  23.         System.out.print("Enter a number: ");
  24.         input = Integer.parseInt(scanner.nextLine());  
  25.         System.out.println("How do you want to calculate factorial of " + String.valueOf(input) + ":");
  26.         System.out.println("1. Iteratively - using for");
  27.         System.out.println("2. Iteratively - using while");
  28.         System.out.println("3. Recursively.");
  29.         System.out.print("Enter a number: ");
  30.         method = Integer.parseInt(scanner.nextLine());
  31.  
  32.         switch (method) {
  33.             case 1:
  34.                 begin = System.nanoTime();
  35.                 System.out.println("The result is: " + getFactorialIterationFor(input));
  36.                 end = System.nanoTime();
  37.                 timeElapsed = end - begin;
  38.                 System.out.println("The iterative (for) approach took "
  39.                     + String.valueOf(timeElapsed) + "ns");
  40.                 break;
  41.             case 2:
  42.                 begin = System.nanoTime();
  43.                 System.out.println("The result is: " + getFactorialIterationWhile(input));
  44.                 end = System.nanoTime();
  45.                 timeElapsed = end - begin;
  46.                 System.out.println("The iterative (while) approach took "
  47.                     + String.valueOf(timeElapsed) + "ns");
  48.                 break;
  49.             case 3:
  50.                 begin = System.nanoTime();
  51.                 System.out.println("The result is: " + getFactorialRecursion(input));
  52.                 end = System.nanoTime();
  53.                 timeElapsed = end - begin;
  54.                 System.out.println("The recursive approach took "
  55.                     + String.valueOf(timeElapsed) + "ns");
  56.                 break;
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Implementation of iterative (for) factorial from:
  62.      * https://stackabuse.com/calculate-factorial-with-java-iterative-and-recursive/
  63.      */
  64.     public static int getFactorialIterationFor(int n)
  65.     {
  66.         int result = 1;
  67.  
  68.         if (n > 1) {
  69.             for (int i = 1; i <= n; i++) {
  70.                 result = result * i;
  71.             }
  72.             return result;
  73.         } else {
  74.             System.out.println("n has to be positive");
  75.             return result;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * Implementation of iterative (while) factorial from:
  81.      * https://stackabuse.com/calculate-factorial-with-java-iterative-and-recursive/
  82.      */
  83.     public static int getFactorialIterationWhile(int n) {
  84.         int result = 1;
  85.         while (n > 1) {
  86.             result = result * n;
  87.             n -= 1;
  88.         }
  89.         return result;
  90.     }
  91.  
  92.     /**
  93.      * Implementation of recursive factorial from:
  94.      * https://stackabuse.com/calculate-factorial-with-java-iterative-and-recursive/
  95.      */
  96.     public static int getFactorialRecursion(int n) {
  97.         if (n <= 1) {
  98.             return 1;
  99.         } else {
  100.             return n * getFactorialRecursion(n-1);
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement