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

  1. <# 
  2. .SYNOPSIS 
  3.     This script divides big integers using .NET Framework. 
  4. .DESCRIPTION 
  5.     This script reimplements an MSDN Sample script using powershell. The 
  6.     script creates a dividor and an arry of dividends, then performs  
  7.     division operations several ways. 
  8. .NOTES 
  9.     File Name  : Divide-BigInteger.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12.                  .NET Framework 4.0 or higher 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.divide.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled7.ps1' 
  20.     Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880 
  21.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  22.     Results: 
  23.        Using Divide method:     7 
  24.        Using Division operator: 7 
  25.        Using DivRem method:     7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137 
  26.  
  27.     Dividend: 90,612,345,123,875,509,091,827,560,007,100,099 
  28.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  29.     Results: 
  30.        Using Divide method:     0 
  31.        Using Division operator: 0 
  32.        Using DivRem method:     0, remainder 90,612,345,123,875,509,091,827,560,007,100,099 
  33.      
  34.     Dividend: 1 
  35.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  36.     Results: 
  37.        Using Divide method:     0 
  38.        Using Division operator: 0 
  39.        Using DivRem method:     0, remainder 1 
  40.      
  41.     Dividend: 19,807,040,619,342,712,359,383,728,129 
  42.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  43.     Results: 
  44.        Using Divide method:     0 
  45.        Using Division operator: 0 
  46.        Using DivRem method:     0, remainder 19,807,040,619,342,712,359,383,728,129 
  47.   
  48.     Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250 
  49.     Divisor:  85,070,591,730,234,615,847,396,907,784,232,501,249 
  50.     Results:  
  51.        Using Divide method:     1 
  52.        Using Division operator: 1 
  53.        Using DivRem method:     1, remainder 1 
  54.   
  55. #> 
  56.  
  57. # Add namespace 
  58. $r = [system.Reflection.Assembly]::LoadWithPartialName("System.Numerics"
  59.  
  60. #Create a big integer divisor and an array of dividends 
  61. $divisor = [system.numerics.BigInteger]::pow([Int64]::MaxValue, 2) 
  62. $dividends = @() 
  63. $dividends += [system.numerics.BigInteger]::Multiply(([system.numerics.BigInteger] [system.Single]::MaxValue), 2) 
  64. $dividends += [system.numerics.BigInteger]::Parse("90612345123875509091827560007100099")  
  65. $dividends += [system.numerics.BigInteger]::One 
  66. $dividends += [system.numerics.BigInteger]::Multiply([Int32]::MaxValue, [Int64]::MaxValue) 
  67. $dividends += $divisor + [system.numerics.BigInteger]::One  
  68.   
  69. #Divide each dividend by divisor in three different ways 
  70. foreach ($dividend in $dividends) { 
  71.     "Dividend: {0:N0}" -f $dividend 
  72.     "Divisor:  {0:N0}" -f $divisor 
  73.     "Results:" 
  74.     "   Using Divide method:     {0:N0}" -f  [system.Numerics.BigInteger]::Divide($dividend, $divisor
  75.     "   Using Division operator: {0:N0}" -f ($dividend/$divisor
  76.     $remainder = [system.Numerics.BigInteger]::Zero 
  77.     $quotient = [system.numerics.BigInteger]::DivRem($dividend, $divisor, [ref] $remainder
  78.     "   Using DivRem method:     {0:N0}, remainder {1:N0}" -f $quotient, $remainder 
  79.     "" 
  80. }         

Wednesday, 4 August 2010

Get-MultiplyBigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements this MSDN Sample of  
  4.     multiplying a big integer. 
  5. .DESCRIPTION 
  6.     This script first tries and fails to multiple a pair of large integers. The 
  7.     script catches the error and then used BigInteger.Multiply to multiply  
  8.     the two big itegers. 
  9. .NOTES 
  10.     File Name  : Get-MultiplyBigInteger.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.multiply.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .Get-MultiplyBigInteger.ps1 
  20.     Too big for long, try biginteger 
  21.     12,193,263,111,263,526,900 
  22. #> 
  23. # Add System.Numerics namespace 
  24. $r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics"
  25.  
  26. # Two big numbers 
  27. $number1 = 1234567890 
  28. $number2 = 9876543210 
  29.  
  30. # Try normal [long] then catch error and do biginteger 
  31. try 
  32.    [long] $product = $number1 * $number2 
  33. catch  
  34. "Too big for long, try biginteger" 
  35.    $product = New-Object System.Numerics.BigInteger 
  36.    $product = [System.Numerics.BigInteger]::Multiply($number1, $number2
  37.    $product.ToString("N0"
  38. }    

Sunday, 1 August 2010

Get-BigIntegerProperties.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays dynamic properties of a BigInteger  
  4. .DESCRIPTION 
  5.     This script demonstates the properties on an instance of BigInteger 
  6. .NOTES 
  7.     File Name  : Get-BigIntegerProperties.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10.                  .NET Framework 4    
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger_properties.aspx 
  16. .EXAMPLE 
  17.     PSH [c:\foo]: .\Get-BigIntegerProperties.ps1 
  18.     Big Integer from 4096: 
  19.  
  20.     IsPowerOfTwo : True 
  21.     IsZero       : False 
  22.     IsOne        : False 
  23.     IsEven       : True 
  24.     Sign         : 1 
  25. #> 
  26.  
  27. # Add the .NET Version 4 System.Numerics.DLL 
  28. Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll" 
  29.  
  30. # Create a big integer then display it's key properties 
  31. $BigInt = New-object System.Numerics.BigInteger 4096 
  32. "Big Integer from 4096:" 
  33. $BigInt | fl * 

Saturday, 31 July 2010

New-BigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and displays a BigInteger.  
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN sample. 
  6. .NOTES 
  7.     File Name  : New-BigInteger.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10.                  .NET Framework 4    
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/07/new-bigintegerps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx  
  16. .EXAMPLE 
  17.     PSH [c:\foo]: .\Get-BigInteger.ps1 
  18.     Big Integer from 179032.6541: 
  19.     179,032 
  20.     Big Integer from 1234934157136952: 
  21.     1,234,934,157,136,952 
  22. #> 
  23.  
  24. # Add the .NET Version 4 System.Numerics.DLL 
  25. Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll" 
  26.  
  27. # Create first big integer then display it nicely 
  28. $BigIntFromDouble = New-Object System.Numerics.BigInteger 179032.6541 
  29. "Big Integer from 179032.6541:" 
  30. $BigIntFromDouble.ToString("N0"
  31.  
  32. #Create second big integer then display it nicely 
  33. $BigIntFromInt64 = New-object System.Numerics.BigInteger 1234934157136952 
  34. "Big Integer from 1234934157136952:" 
  35. $Bigintfromint64.tostring("N0"