PowerShell/docs/list-pi.md

121 lines
2.7 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*list-pi.ps1*
================
2022-12-04 10:40:18 +01:00
This PowerShell script calculates and lists the digits of the mathematical constant PI.
2023-07-29 10:04:38 +02:00
Parameters
----------
2022-12-04 10:40:18 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./list-pi.ps1 [[-Digits] <Int32>] [<CommonParameters>]
2022-12-04 10:40:18 +01:00
-Digits <Int32>
Specifies the number of digits to list (1000 by default)
Required? false
Position? 1
Default value 1000
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2022-12-04 10:40:18 +01:00
```powershell
2023-08-06 21:36:33 +02:00
PS> ./list-pi.ps1
2022-12-04 10:40:18 +01:00
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342...
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-12-04 10:40:18 +01:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2022-12-04 10:40:18 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-12-04 10:40:18 +01:00
```powershell
<#
.SYNOPSIS
Lists PI
.DESCRIPTION
This PowerShell script calculates and lists the digits of the mathematical constant PI.
.PARAMETER digits
Specifies the number of digits to list (1000 by default)
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./list-pi.ps1
2022-12-04 10:40:18 +01:00
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([int]$Digits = 1000)
function List-Pi ( $Digits ) {
$Big = [bigint[]](0..10)
$ndigits = 0
$q = $t = $k = $Big[1]
$r = $Big[0]
$l = $n = $Big[3]
# calculate first digit
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
Write-Host "$($n)." -noNewline
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
while ($ndigits -lt $Digits) {
if ($Big[4] * $q + $r - $t -lt $n * $t) {
Write-Host "$n" -noNewline
$ndigits++
$nr = $Big[10] * ( $r - $n * $t )
$n = ( ( $Big[10] * ( 3 * $q + $r ) ) / $t ) - 10 * $n
$q *= $Big[10]
$r = $nr
} else {
$nr = ( $Big[2] * $q + $r ) * $l
$nn = ( $q * ( $Big[7] * $k + $Big[2] ) + $r * $l ) / ( $t * $l )
$q *= $k
$t *= $l
$l += $Big[2]
$k = $k + $Big[1]
$n = $nn
$r = $nr
}
}
Write-Host "... ($Digits digits)"
}
try {
List-Pi $Digits
exit 0 # success
} catch {
2023-05-26 12:20:18 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2022-12-04 10:40:18 +01:00
}
```
2023-12-07 20:24:45 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of list-pi.ps1 as of 12/07/2023 20:24:19)*