Calculate MDAS Factorial of given number in Java
The following article explores MDAS (Multiplication, Division, Addition, Subtraction) in relation to factorials, a hierarchy of arithmetic operations. It aims to empower mathematicians with a deeper understanding of MDAS factorials, bridging theory with practice and enabling confidence in complex mathematical landscapes. MDAS is crucial for consistency, clarity, correctness, and efficiency in computations, ensuring accurate results in fields like engineering, science, and finance.
Example:
Input: n = 4
Output: 7
Explanation: The MDAS factorial function iterates from 4 to 1, performing operations like multiplication, division, addition, and subtraction, resulting in 7 for n = 4.
Using Optimized Approach
Algorithm
Step1: Check the input N if it's less than or equal to 0.
Step 2: Initialize "factorial" with N value.
Step 3: Set "operatorIndex" to 0 to track the current operator.
Step 4: Iterate from N-1 to 1 based on the "operatorIndex" value.
Step 5: Switch between cases based on the "operatorIndex" value.
Step 6: Update "operatorIndex" by taking the modulus of the current value incremented by 1 with 4.
Step 7: Return the final "factorial" value after loop.
Here is an implementation to calculate the MDAS factorial of a given number using an optimized approach :
FileName:MDASFactorial.java
public class MDASFactorial { public static void main(String[] args) { int N = 4; // Set N to 10 int result = calculateMDASFactorial(N); System.out.println("MDAS Factorial of " + N + " is: " + result); } static int calculateMDASFactorial(int N) { if (N <= 0) return 0; if (N == 1) return 1; int factorial = N; int operatorIndex = 0; // To keep track of the current operator in the pattern for (int i = N - 1; i >= 1; i--) { switch (operatorIndex) { case 0: // Multiplication factorial *= i; break; case 1: // Division factorial /= i; break; case 2: // Addition factorial += i; break; case 3: // Subtraction factorial -= i; break; } operatorIndex = (operatorIndex + 1) % 4; // Cycle through operators } return factorial; } }
Output:
MDAS Factorial of 4 is: 7
Complexity analysis: The above approach's time complexity is O(N) because it iterates through the numbers from N-1 to 1 once, and its space complexity is O(1) because the memory usage remains constant regardless of the input size N.
Using Iterative Approach
Algorithm
Step 1: Initialize variable "result" with input integer "N".
Step 2: Use for loop from "N-1" to 1 with variable "i".
Step 3: Calculate the operation index based on loop position.
Step 4: Perform operations (multiplication, division, addition, subtraction) between "result" and "i."
Step 5: Return the final result after loop.
Step 6: Initialize variable "N" with the given value.
Here is an implementation to calculate the MDAS factorial of a given number using an iterative approach :
FileName:Count.java
public class Count { // MDAS factorial function public static long MDAS_factorial(int N) { long result = N; for (int i = N - 1; i >= 1; i--) { int operationIndex = (N - i - 1) % 4; // Determine the operation index based on the position switch (operationIndex) { case 0: // Multiplication result *= i; break; case 1: // Division result /= i; break; case 2: // Addition result += i; break; case 3: // Subtraction result -= i; break; } } return result; } public static void main(String[] args) { int N = 4; System.out.println("MDAS Factorial of " + N + " is: " + MDAS_factorial(N)); } }
Output:
MDAS Factorial of 4 is: 7
Complexity analysis: The time complexity of the above approach is O(N) because the MDAS_factorial function contains a loop that iterates from N-1 down to 1, performing a constant number of operations per iteration, and the space complexity is O(1) because the amount of memory used by the algorithm remains constant, regardless of the input size N.