ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

PowerShell: Factorial Calculation (Iterative)

PowerShell

Goal -- WPM

Ready
Exercise Algorithm Area
1function Calculate-Factorial {
2param(
3[long]$Number
4)
5
6if ($Number -lt 0) {
7Write-Error "Factorial is not defined for negative numbers."
8return $null
9}
10
11if ($Number -eq 0) {
12return 1
13}
14
15$factorial = 1
16for ($i = 1; $i -le $Number; $i++) {
17$factorial *= $i
18}
19
20return $factorial
21}
22
23function Is-Negative {
24param(
25[long]$Value
26)
27return $Value -lt 0
28}
29
30function Is-Zero {
31param(
32[long]$Value
33)
34return $Value -eq 0
35}
36
37function Get-NumberRange {
38param(
39[long]$Start,
40[long]$End
41)
42$range = @()
43for ($i = $Start; $i -le $End; $i++) {
44$range += $i
45}
46return $range
47}
48
49function Multiply-Numbers {
50param(
51[long[]]$Numbers
52)
53$product = 1
54foreach ($num in $Numbers) {
55$product *= $num
56}
57return $product
58}
Algorithm description viewbox

PowerShell: Factorial Calculation (Iterative)

Algorithm description:

This function computes the factorial of a non-negative integer. The factorial of a number is the product of all positive integers less than or equal to that number. It's a fundamental concept in combinatorics and probability. This implementation uses an iterative approach.

Algorithm explanation:

The `Calculate-Factorial` function computes the factorial of a given non-negative integer iteratively. It first handles edge cases: if the input `$Number` is negative, it writes an error and returns null. If `$Number` is 0, it correctly returns 1 (0! = 1). For positive numbers, it initializes `$factorial` to 1. A `for` loop then iterates from 1 up to `$Number`, multiplying `$factorial` by the current loop variable `$i` in each step. The time complexity is O(n) because the loop runs `n` times. The space complexity is O(1) as it uses a fixed amount of memory. The helper functions `Is-Negative`, `Is-Zero`, `Get-NumberRange`, and `Multiply-Numbers` demonstrate modularity and provide basic arithmetic operations. The correctness is maintained by the invariant that `$factorial` holds the product of numbers from 1 up to the current loop iteration.

Pseudocode:

FUNCTION Calculate-Factorial(Number):
  IF Number < 0:
    THROW ERROR "Factorial is not defined for negative numbers."
    RETURN null
  IF Number == 0:
    RETURN 1
  SET factorial = 1
  FOR i FROM 1 TO Number:
    SET factorial = factorial * i
  RETURN factorial
END FUNCTION

FUNCTION Is-Negative(Value):
  RETURN Value < 0
END FUNCTION

FUNCTION Is-Zero(Value):
  RETURN Value == 0
END FUNCTION

FUNCTION Get-NumberRange(Start, End):
  CREATE empty array range
  FOR i FROM Start TO End:
    ADD i to range
  RETURN range
END FUNCTION

FUNCTION Multiply-Numbers(Numbers):
  SET product = 1
  FOR EACH num IN Numbers:
    SET product = product * num
  RETURN product
END FUNCTION