# Recursive function to solve the Tower of Hanoi puzzle function Invoke-TowerOfHanoi { param( [int]$Disks, [string]$Source = "A", [string]$Target = "C", [string]$Auxiliary = "B" ) if ($Disks -eq 1) { Write-Output "Move disk 1 from $Source to $Target" return } Invoke-TowerOfHanoi -Disks ($Disks - 1) -Source $Source -Target $Auxiliary -Auxiliary $Target Write-Output "Move disk $Disks from $Source to $Target" Invoke-TowerOfHanoi -Disks ($Disks - 1) -Source $Auxiliary -Target $Target -Auxiliary $Source } # Generate Fibonacci sequence using memoization $script:FibCache = @{} function Get-Fibonacci { param([int]$n) if ($script:FibCache.ContainsKey($n)) { return $script:FibCache[$n] } if ($n -le 1) { return $n } $result = (Get-Fibonacci ($n - 1)) + (Get-Fibonacci ($n - 2)) $script:FibCache[$n] = $result return $result } # Check if a number is prime using optimized trial division function Test-Prime { param([long]$Number) if ($Number -lt 2) { return $false } if ($Number -eq 2) { return $true } if ($Number % 2 -eq 0) { return $false } $sqrt = [Math]::Sqrt($Number) for ($i = 3; $i -le $sqrt; $i += 2) { if ($Number % $i -eq 0) { return $false } } return $true } # Binary search implementation function Search-Binary { param( [array]$SortedArray, [int]$Target ) $left = 0 $right = $SortedArray.Length - 1 while ($left -le $right) { $mid = [Math]::Floor(($left + $right) / 2) if ($SortedArray[$mid] -eq $Target) { return $mid } if ($SortedArray[$mid] -lt $Target) { $left = $mid + 1 } else { $right = $mid - 1 } } return -1 } # QuickSort algorithm implementation function Invoke-QuickSort { param([array]$Array) if ($Array.Count -le 1) { return $Array } $pivot = $Array[0] $left = @($Array | Where-Object { $_ -lt $pivot }) $middle = @($Array | Where-Object { $_ -eq $pivot }) $right = @($Array | Where-Object { $_ -gt $pivot }) return (Invoke-QuickSort $left) + $middle + (Invoke-QuickSort $right) } # Calculate the Collatz sequence (3n+1 problem) function Get-CollatzSequence { param([long]$StartNumber) $sequence = [System.Collections.ArrayList]::new() $current = $StartNumber while ($current -ne 1) { [void]$sequence.Add($current) if ($current % 2 -eq 0) { $current = $current / 2 } else { $current = 3 * $current + 1 } } [void]$sequence.Add(1) return $sequence } # Generate all permutations of an array function Get-Permutations { param([array]$Items) if ($Items.Count -le 1) { return ,$Items } $result = @() for ($i = 0; $i -lt $Items.Count; $i++) { $current = $Items[$i] $remaining = $Items[0..($i-1)] + $Items[($i+1)..($Items.Count-1)] foreach ($perm in (Get-Permutations $remaining)) { $result += ,(@($current) + $perm) } } return $result } # Calculate greatest common divisor using Euclidean algorithm function Get-GCD { param([long]$a, [long]$b) while ($b -ne 0) { $temp = $b $b = $a % $b $a = $temp } return $a } # Solve a maze using depth-first search function Solve-Maze { param( [char[,]]$Maze, [int]$StartX, [int]$StartY, [int]$EndX, [int]$EndY ) $visited = @{} $stack = [System.Collections.Stack]::new() $stack.Push(@($StartX, $StartY, @())) while ($stack.Count -gt 0) { $current = $stack.Pop() $x, $y, $path = $current if ($x -eq $EndX -and $y -eq $EndY) { return $path + @(@($x, $y)) } $key = "$x,$y" if ($visited[$key]) { continue } $visited[$key] = $true foreach ($dir in @(@(0,1), @(1,0), @(0,-1), @(-1,0))) { $nx = $x + $dir[0]; $ny = $y + $dir[1] if ($Maze[$nx,$ny] -ne '#') { $stack.Push(@($nx, $ny, ($path + @(@($x, $y))))) } } } return $null } # Recursive function to solve the Tower of Hanoi puzzle function Invoke-TowerOfHanoi { param( [int]$Disks, [string]$Source = "A", [string]$Target = "C", [string]$Auxiliary = "B" ) if ($Disks -eq 1) { Write-Output "Move disk 1 from $Source to $Target" return } Invoke-TowerOfHanoi -Disks ($Disks - 1) -Source $Source -Target $Auxiliary -Auxiliary $Target Write-Output "Move disk $Disks from $Source to $Target" Invoke-TowerOfHanoi -Disks ($Disks - 1) -Source $Auxiliary -Target $Target -Auxiliary $Source } # Generate Fibonacci sequence using memoization $script:FibCache = @{} function Get-Fibonacci { param([int]$n) if ($script:FibCache.ContainsKey($n)) { return $script:FibCache[$n] } if ($n -le 1) { return $n } $result = (Get-Fibonacci ($n - 1)) + (Get-Fibonacci ($n - 2)) $script:FibCache[$n] = $result return $result }

Secure, Integrate & Automate

Helping organizations modernize infrastructure, automate workflows and increase security.