- <#
- .SYNOPSIS
- This script divides big integers using .NET Framework.
- .DESCRIPTION
- This script reimplements an MSDN Sample script using powershell. The
- script creates a dividor and an arry of dividends, then performs
- division operations several ways.
- .NOTES
- File Name : Divide-BigInteger.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4.0 or higher
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.divide.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled7.ps1'
- Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 7
- Using Division operator: 7
- Using DivRem method: 7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137
- Dividend: 90,612,345,123,875,509,091,827,560,007,100,099
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 90,612,345,123,875,509,091,827,560,007,100,099
- Dividend: 1
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 1
- Dividend: 19,807,040,619,342,712,359,383,728,129
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 19,807,040,619,342,712,359,383,728,129
- Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 1
- Using Division operator: 1
- Using DivRem method: 1, remainder 1
- #>
- # Add namespace
- $r = [system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
- #Create a big integer divisor and an array of dividends
- $divisor = [system.numerics.BigInteger]::pow([Int64]::MaxValue, 2)
- $dividends = @()
- $dividends += [system.numerics.BigInteger]::Multiply(([system.numerics.BigInteger] [system.Single]::MaxValue), 2)
- $dividends += [system.numerics.BigInteger]::Parse("90612345123875509091827560007100099")
- $dividends += [system.numerics.BigInteger]::One
- $dividends += [system.numerics.BigInteger]::Multiply([Int32]::MaxValue, [Int64]::MaxValue)
- $dividends += $divisor + [system.numerics.BigInteger]::One
- #Divide each dividend by divisor in three different ways
- foreach ($dividend in $dividends) {
- "Dividend: {0:N0}" -f $dividend
- "Divisor: {0:N0}" -f $divisor
- "Results:"
- " Using Divide method: {0:N0}" -f [system.Numerics.BigInteger]::Divide($dividend, $divisor)
- " Using Division operator: {0:N0}" -f ($dividend/$divisor)
- $remainder = [system.Numerics.BigInteger]::Zero
- $quotient = [system.numerics.BigInteger]::DivRem($dividend, $divisor, [ref] $remainder)
- " Using DivRem method: {0:N0}, remainder {1:N0}" -f $quotient, $remainder
- ""
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label System.Numerics. Show all posts
Showing posts with label System.Numerics. Show all posts
Monday, 9 August 2010
Divide-BigInteger.ps1
Wednesday, 4 August 2010
Get-MultiplyBigInteger.ps1
- <#
- .SYNOPSIS
- This script re-implements this MSDN Sample of
- multiplying a big integer.
- .DESCRIPTION
- This script first tries and fails to multiple a pair of large integers. The
- script catches the error and then used BigInteger.Multiply to multiply
- the two big itegers.
- .NOTES
- File Name : Get-MultiplyBigInteger.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.multiply.aspx
- .EXAMPLE
- PSH [C:\foo]: .Get-MultiplyBigInteger.ps1
- Too big for long, try biginteger
- 12,193,263,111,263,526,900
- #>
- # Add System.Numerics namespace
- $r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
- # Two big numbers
- $number1 = 1234567890
- $number2 = 9876543210
- # Try normal [long] then catch error and do biginteger
- try
- {
- [long] $product = $number1 * $number2
- }
- catch
- { "Too big for long, try biginteger"
- $product = New-Object System.Numerics.BigInteger
- $product = [System.Numerics.BigInteger]::Multiply($number1, $number2)
- $product.ToString("N0")
- }
Sunday, 1 August 2010
Get-BigIntegerProperties.ps1
- <#
- .SYNOPSIS
- This script displays dynamic properties of a BigInteger
- .DESCRIPTION
- This script demonstates the properties on an instance of BigInteger
- .NOTES
- File Name : Get-BigIntegerProperties.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger_properties.aspx
- .EXAMPLE
- PSH [c:\foo]: .\Get-BigIntegerProperties.ps1
- Big Integer from 4096:
- IsPowerOfTwo : True
- IsZero : False
- IsOne : False
- IsEven : True
- Sign : 1
- #>
- # Add the .NET Version 4 System.Numerics.DLL
- Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll"
- # Create a big integer then display it's key properties
- $BigInt = New-object System.Numerics.BigInteger 4096
- "Big Integer from 4096:"
- $BigInt | fl *
Labels:
.Net 4.0,
BigInteger,
powershell,
PowerShell scripts,
System.Numerics
Saturday, 31 July 2010
New-BigInteger.ps1
- <#
- .SYNOPSIS
- This script creates and displays a BigInteger.
- .DESCRIPTION
- This script is a rewrite of an MSDN sample.
- .NOTES
- File Name : New-BigInteger.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/07/new-bigintegerps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
- .EXAMPLE
- PSH [c:\foo]: .\Get-BigInteger.ps1
- Big Integer from 179032.6541:
- 179,032
- Big Integer from 1234934157136952:
- 1,234,934,157,136,952
- #>
- # Add the .NET Version 4 System.Numerics.DLL
- Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll"
- # Create first big integer then display it nicely
- $BigIntFromDouble = New-Object System.Numerics.BigInteger 179032.6541
- "Big Integer from 179032.6541:"
- $BigIntFromDouble.ToString("N0")
- #Create second big integer then display it nicely
- $BigIntFromInt64 = New-object System.Numerics.BigInteger 1234934157136952
- "Big Integer from 1234934157136952:"
- $Bigintfromint64.tostring("N0")
Labels:
.Net 4.0,
BigInteger,
powershell,
PowerShell scripts,
System.Numerics
Subscribe to:
Posts (Atom)