Class Factorial

java.lang.Object
topics.foundation.factorial.Factorial

public class Factorial extends Object

Factorial Computation

Evaluates the factorial of a given integer using a recursive mathematical strategy. This class provides both a robust implementation with strict boundary validation and an unsafe variant to demonstrate recursive vulnerability.

Complexity

  • Time Complexity: O(N) - Requires N consecutive recursive calls to reach the foundational base case.
  • Space Complexity: O(N) - Utilizes N discrete stack frames during the recursive descent, scaling linearly with the input.
Author:
vicegd
  • Constructor Details

    • Factorial

      public Factorial()
  • Method Details

    • computeSafe

      public long computeSafe(int target)
      Computes the factorial recursively with strict negative boundary validation.
      Parameters:
      target - The target integer for the factorial calculation.
      Returns:
      The computed mathematical factorial, or -1 if the input violates geometric constraints (i.e., is negative).
    • computeUnsafe

      public long computeUnsafe(int target)
      Computes the factorial recursively without validating boundaries.

      Structural Warning: Supplying a negative integer to this method will entirely bypass the base case, triggering an infinite recursive loop and culminating in a StackOverflowError.

      Parameters:
      target - The target integer for the factorial calculation.
      Returns:
      The computed mathematical factorial.