How to resolve the algorithm Left factorials step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Left factorials step by step in the PowerShell programming language

Table of Contents

Problem Statement

Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:

(It may not be visually obvious, but the last example uses an upside-down exclamation mark.)

This Rosetta Code task will be using this formula   (factorial sums)   for   left factorial:

Display the left factorials for:

Display the length (in decimal digits) of the left factorials for:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Left factorials step by step in the PowerShell programming language

Source code in the powershell programming language

function left-factorial ([BigInt]$n) {
    [BigInt]$k, [BigInt]$fact = ([BigInt]::Zero), ([BigInt]::One)
    [BigInt]$lfact = ([BigInt]::Zero)
    while($k -lt $n){        
        if($k -gt ([BigInt]::Zero)) {
            $fact = [BigInt]::Multiply($fact, $k)
            $lfact = [BigInt]::Add($lfact, $fact)
        } else {
            $lfact = ([BigInt]::One)
        }
        $k = [BigInt]::Add($k, [BigInt]::One)
    }
    $lfact
}
0..9 | foreach{
    "!$_ = $(left-factorial $_)"
}
for($i = 10; $i -le 110; $i += 10) {
    "!$i = $(left-factorial $i)"
}
for($i = 1000; $i -le 10000; $i += 1000) {
    $digits = [BigInt]::Log10($(left-factorial $i)) 
    $digits = [Math]::Floor($digits) + 1
    if($digits -gt 1) {"!$i has $digits digits"}
    else {"!$i has $digits digit"}
}


  

You may also check:How to resolve the algorithm CUSIP step by step in the 11l programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Clojure programming language
You may also check:How to resolve the algorithm Reflection/List properties step by step in the Raku programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the C++ programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Lua programming language