Thursday, July 19, 2012

"Big Math"[0] : [BigInt] to the rescue!


 BigInt to the  rescue.  Only with .NET 4.0. From PS 3.0 Beta:


Normally:


${2^1023}=[math]::POW(2,1023)
${2^1023}
8.98846567431158E+307
${2^1024}=[math]::POW(2,1024)
${2^1024}
Infinity
${2^1023} * 1.99999999999999988
1.79769313486232E+308
$AMAX=${2^1023} * 1.99999999999999988
[Double]::MaxValue
1.79769313486232E+308
[Double]::MaxValue - $AMAX
0


...and thus integer factorial is limited to 170:



${n170!}=170..1 | % {rv result;$result = 1}{$result *= $_}{$result}
${n170!}
7.257415615308E+306
${n171!}=171..1 | % {rv result;$result = 1}{$result *= $_}{$result}
${n171!}
Infinity

However:


$a=[bigint]::POW(2,4322304)
$a.tobytearray().count
540289


and so!:


100000! (as in n!):



PS C:\ps1> measure-command {$a=100000..1 | % {rv result;$result = 1}{$result=[bigint]::multiply($result,$_)}{$result}}


Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 5
Milliseconds      : 360
Ticks             : 1253600537
TotalDays         : 0.0014509265474537
TotalHours        : 0.0348222371388889
TotalMinutes      : 2.08933422833333
TotalSeconds      : 125.3600537
TotalMilliseconds : 125360.0537


or simply:

PS C:\ps1> measure-command {$b=100000..1 | % {rv result;[bigint]$result = 1}{$result *= $_}{$result}}

Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 8
Milliseconds      : 944
Ticks             : 1289444108
TotalDays         : 0.00149241216203704
TotalHours        : 0.0358178918888889
TotalMinutes      : 2.14907351333333
TotalSeconds      : 128.9444108
TotalMilliseconds : 128944.4108



PS C:\ps1> ($a.ToByteArray()).count
189589
PS C:\ps1> ($b.ToByteArray()).count
189589


[bigint]| gm -static




   TypeName: System.Numerics.BigInteger


Name                  MemberType Definition
----                  ---------- ----------
Abs                   Method     static bigint Abs(bigint value)
Add                   Method     static bigint Add(bigint left, bigint right)
Compare               Method     static int Compare(bigint left, bigint right)
Divide                Method     static bigint Divide(bigint dividend, bigint divisor)
DivRem                Method     static bigint DivRem(bigint dividend, bigint divisor, [ref] bigint remainder)
Equals                Method     static bool Equals(System.Object objA, System.Object objB)
GreatestCommonDivisor Method     static bigint GreatestCommonDivisor(bigint left, bigint right)
Log                   Method     static double Log(bigint value), static double Log(bigint value, double baseValue)
Log10                 Method     static double Log10(bigint value)
Max                   Method     static bigint Max(bigint left, bigint right)
Min                   Method     static bigint Min(bigint left, bigint right)
ModPow                Method     static bigint ModPow(bigint value, bigint exponent, bigint modulus)
Multiply              Method     static bigint Multiply(bigint left, bigint right)
Negate                Method     static bigint Negate(bigint value)
Parse                 Method     static bigint Parse(string value), static bigint Parse(string value, System.Globalization.NumberStyles style),
Pow                   Method     static bigint Pow(bigint value, int exponent)
ReferenceEquals       Method     static bool ReferenceEquals(System.Object objA, System.Object objB)
Remainder             Method     static bigint Remainder(bigint dividend, bigint divisor)
Subtract              Method     static bigint Subtract(bigint left, bigint right)
TryParse              Method     static bool TryParse(string value, [ref] bigint result), static bool TryParse(string value, System.Globalizatio
MinusOne              Property   bigint MinusOne {get;}
One                   Property   bigint One {get;}
Zero                  Property   bigint Zero {get;}



No comments: