mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-15 19:21:04 +01:00
Add list-pi.ps1
This commit is contained in:
parent
57d9a73c52
commit
ae52401e98
73
Scripts/list-pi.ps1
Normal file
73
Scripts/list-pi.ps1
Normal file
@ -0,0 +1,73 @@
|
||||
<#
|
||||
.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 (10000 by default)
|
||||
.EXAMPLE
|
||||
PS> ./list-pi
|
||||
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342...
|
||||
.LINK
|
||||
https://github.com/fleschutz/PowerShell
|
||||
.NOTES
|
||||
Author: Markus Fleschutz | License: CC0
|
||||
#>
|
||||
|
||||
param([int]$Digits = 10000)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
List-Pi $Digits
|
||||
exit 0 # success
|
||||
} catch {
|
||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||
exit 1
|
||||
}
|
Loading…
Reference in New Issue
Block a user