<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0"><channel><title>IntX Wiki &amp; Documentation Rss Feed</title><link>http://www.codeplex.com/IntX/Wiki/View.aspx?title=Home</link><description>IntX Wiki Rss Description</description><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=39</link><description>&lt;div class="wikidoc"&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;NuGet&lt;/h3&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;nuget.org&amp;#47;packages&amp;#47;IntX"&gt;&lt;img src="http://i3.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=319975" alt="Install-Package&amp;#32;IntX" title="Install-Package&amp;#32;IntX" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h3&gt;Bits of History&lt;/h3&gt;
I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;
&lt;h3&gt;Code Example&lt;/h3&gt;
Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;
&lt;h3&gt;FHT and Calculations Precision&lt;/h3&gt;
Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;
&lt;h3&gt;Internal Representation and ToString() Performance&lt;/h3&gt;
For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;
&lt;h3&gt;Future Plans&lt;/h3&gt;
I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;
&lt;h3&gt;System.Numerics.BigInteger in .NET 4.0&lt;/h3&gt;
System.Numerics.BigInteger class was introduced in .NET 4.0 so I was interested in performance of this solution. I did some tests (&lt;a href="https://github.com/devoyster/Oyster.Examples/tree/master/Oyster.Examples.IntXTest" class="externalLink"&gt;grab test code from GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;) and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;
&lt;h3&gt;Comparing IntX Performance to GMP&lt;/h3&gt;
While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end up with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Sun, 08 Jan 2012 18:12:22 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20120108061222P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=38</link><description>&lt;div class="wikidoc"&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;NuGet&lt;/h3&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;nuget.org&amp;#47;packages&amp;#47;IntX"&gt;&lt;img src="http://i3.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=319975" alt="Install-Package&amp;#32;IntX" title="Install-Package&amp;#32;IntX" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h3&gt;Bits of History&lt;/h3&gt;
I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;
&lt;h3&gt;Code Example&lt;/h3&gt;
Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;
&lt;h3&gt;FHT and Calculations Precision&lt;/h3&gt;
Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;
&lt;h3&gt;Internal Representation and ToString() Performance&lt;/h3&gt;
For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;
&lt;h3&gt;Future Plans&lt;/h3&gt;
I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;
&lt;h3&gt;System.Numerics.BigInteger in .NET 4.0&lt;/h3&gt;
System.Numerics.BigInteger class was introduced in .NET 4.0 so I was interested in performance of this solution. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;
&lt;h3&gt;Comparing IntX Performance to GMP&lt;/h3&gt;
While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end up with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 30 Dec 2011 14:21:44 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111230022144P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=37</link><description>&lt;div class="wikidoc"&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;h3&gt;NuGet&lt;/h3&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;nuget.org&amp;#47;packages&amp;#47;IntX"&gt;&lt;img src="http://i3.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=319975" alt="Install-Package&amp;#32;IntX" title="Install-Package&amp;#32;IntX" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h3&gt;Bits of History&lt;/h3&gt;
I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;
&lt;h3&gt;Code Example&lt;/h3&gt;
Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;
&lt;h3&gt;FHT and Calculations Precision&lt;/h3&gt;
Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;
&lt;h3&gt;Internal Representation and ToString() Performance&lt;/h3&gt;
For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;
&lt;h3&gt;Future Plans&lt;/h3&gt;
I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;
&lt;h3&gt;System.Numerics.BigInteger in .NET 4.0&lt;/h3&gt;
System.Numerics.BigInteger class was introduced in .NET 4.0 so I was interested in performance of this solution. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;
&lt;h3&gt;Comparing IntX Performance to GMP&lt;/h3&gt;
While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end up with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 30 Dec 2011 14:20:55 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111230022055P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=36</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;&lt;a href="http://nuget.org/packages/IntX" class="externalLink"&gt;Download IntX 1.0.0.0 from NuGet&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;System.Numerics.BigInteger class was introduced in .NET 4.0 so I was interested in performance of this solution. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end up with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 30 Dec 2011 09:39:38 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111230093938A</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=35</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;&lt;a href="http://nuget.org/packages/IntX" class="externalLink"&gt;Download IntX 1.0.0.0 from NuGet&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;System.Numerics.BigInteger class was introduced in .NET 4.0 so I was interested in performance of this solution. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 30 Dec 2011 09:37:04 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111230093704A</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=34</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;&lt;a href="http://nuget.org/packages/IntX" class="externalLink"&gt;Download IntX 1.0.0.0 from NuGet&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Tue, 27 Dec 2011 21:06:32 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111227090632P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=33</link><description>&lt;div class="wikidoc"&gt;&lt;h2&gt;&lt;a href="http://nuget.org/packages/IntX" class="externalLink"&gt;Download from NuGet&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Tue, 27 Dec 2011 20:57:58 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111227085758P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=32</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;Project sources are now &lt;a href="https://github.com/devoyster/IntXLib" class="externalLink"&gt;hosted on GitHub&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and binaries are &lt;a href="http://nuget.org/packages/IntX" class="externalLink"&gt;distributed via NuGet&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=27428"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="http://www.codeplex.com/Download?ProjectName=IntX&amp;DownloadId=102346"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 23 Dec 2011 12:30:23 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20111223123023P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=31</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Internal Representation and ToString() Performance&lt;/b&gt;&lt;br /&gt;For a really huge integer numbers (like 42 in power 1048576 above) ToString() call can take a very long time to execute. This is because internally IntX big integer is stored as 2^32-base number in uint[] array and to generate decimal string output it should be converted from 2^32 base to decimal base. Such digits storage approach was chosen intentionally - it makes ToString() slower but uses memory efficiently and makes primitive operations on digits faster than power of 10-base storage (which would make ToString() working faster) and I think that usually computations are used more often than ToString().&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=102346';"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Tue, 11 May 2010 14:06:30 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100511020630P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=30</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=102346';"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Fri, 22 Jan 2010 22:33:18 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100122103318P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=29</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in contest held by &lt;a href="http://gotdotnet.ru/" class="externalLink"&gt;GotDotNet.ru&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=102346';"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Thu, 21 Jan 2010 16:34:45 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100121043445P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=28</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=102346';"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Thu, 21 Jan 2010 14:43:02 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100121024302P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=27</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 8 times faster than IntX on certain operations. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP also has comparable performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works few times faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms. For tests I was using &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=102346';"&gt;libgmp-3.dll&lt;/a&gt; GMP 5.0.0 DLL built for Windows using MinGW; I also want to add that my performance comparing was primitive and its results are very approximate.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license); if you work in Windows you can build it with Cygwin/MinGW or use some GMP Windows-friendly port like &lt;a href="http://www.mpir.org/" class="externalLink"&gt;MPIR&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. However, if 8x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests; I was also using GMP DLL which comes with it) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Thu, 21 Jan 2010 14:42:18 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100121024218P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=26</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 5 times faster than IntX. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP has about the same performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works about 4x faster in GMP comparing to IntX. GMP also has support for big floating-point numbers as well as for many numeric algorithms.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 5x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests; I was also using GMP DLL which comes with it) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:54:54 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120055454P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=25</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 5 times faster than IntX. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP has about the same performance on huge integers multiplication since, as well as IntX, it implements fast multiplication algorithm using FFT; it&amp;#39;s also worth noticing that division for huge integers works about 4x faster in GMP comparing to IntX. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 5x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests; I was also using GMP DLL which comes with it) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:52:47 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120055247P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=24</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 5 times faster than IntX. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP is also faster on a huge integers since, as well as IntX, it implements fast multiplication algorithm using FFT. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 5x speed loss on some operations is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:47:41 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120054741P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=23</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that GMP is up to 5 times faster than IntX. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP is also faster on a huge integers since, as well as IntX, it implements fast multiplication algorithm using FFT. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 2-10x speed loss is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:47:03 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120054703P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=22</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that &lt;b&gt;GMP is 2-10 times faster than IntX&lt;/b&gt;. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP is also about 10x faster on a huge integers since, as well as IntX, it implements fast multiplication algorithm using FFT. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 2-10x speed loss is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource they are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:43:33 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120054333P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=21</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that &lt;b&gt;GMP is 2-10 times faster than IntX&lt;/b&gt;. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP is also about 10x faster on a huge integers since, as well as IntX, it implements fast multiplication algorithm using FFT. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 2-10x speed loss is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you.&lt;br /&gt;&lt;br /&gt;Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. But be careful with them - both wrappers I saw are releasing unmanaged resource the are referring (which is memory) only in Finalize() method override and not implementing IDisposable so you can end with unexpected OutOfMemoryException as I did.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:42:49 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120054249P</guid></item><item><title>Updated Wiki: Home</title><link>http://intx.codeplex.com/wikipage?version=20</link><description>&lt;div class="wikidoc"&gt;&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;IntX is an arbitrary precision integers library written in pure C&amp;#35; 2.0 with fast - O&amp;#40;N &amp;#42; log N&amp;#41; - multiplication&amp;#47;division algorithms implementation. It provides all the basic operations on integers like addition, multiplication, comparing, bitwise shifting etc. It also allows parsing numbers in base from 2 to 16 and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base&amp;#47;to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O&amp;#40;N &amp;#42; log N &amp;#42; log log N&amp;#41; time instead of classic O&amp;#40;N&amp;#94;2&amp;#41;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Bits of History&lt;/b&gt;&lt;br /&gt;I have written IntX basically because I like big numbers and had some free time. Initial implementation was standard - I was using standard big integers +, -, *, / algorithms from Khuth book. After library was written I&amp;#39;ve decided to participate in &lt;a href="http://contest2005.gotdotnet.ru/Request/Tools/UtilitiesLib/169728.aspx" class="externalLink"&gt;contest held by GotDotNet.ru site&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and received some replies which were saying that my library is too ... usual. Well, it was true, so I&amp;#39;ve decided to implement some more interesting algorithms in it.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve started with writing multiplication using &lt;a href="http://en.wikipedia.org/wiki/Discrete_Hartley_transform" class="externalLink"&gt;Fast Hartley Transform&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; so big integers multiplication time estimate became to be O(N * log N * log log N) (here N is amount of DWORDs in number representation) which was a bit better then O(N^2) with classic algorithm :) Then I saw fast algorithm for transforming from one base to another in Knuth book; it was based on fast multiplication so Parse()/ToString() started working faster - O(N * (log N)^2) instead of O(N^2). Finally division was also optimized (again, with the help of fast multiplication) - became as fast as multiplication.&lt;br /&gt;&lt;br /&gt;All this happened in 2005 year and in 2008 I&amp;#39;ve decided to publish this library on CodePlex - maybe it will be useful for someone out there (well, there is not so many similar libraries under .NET; also System.Numeric.BigInteger from .NET FW 3.5 was cancelled). Before publishing it on CodePlex I also made some cosmetic changes in code - used new .NET 2.0 features like generics (to minimize code duplication) and rewritten unit tests to use NUnit (they were previously written for MbUnit which is almost unknown and not used by community).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Code Example&lt;/b&gt;&lt;br /&gt;Here is the sample of code which uses IntX and calculates 42 in power 1048576 (which is 2^20):&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System.Diagnostics;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Calc()
    {
      Stopwatch sw = Stopwatch.StartNew();
      IntX.Pow(42, 1 &amp;lt;&amp;lt; 20);
      sw.Stop();
      Console.WriteLine(&lt;span style="color:#A31515;"&gt;&amp;quot;{0} ms&amp;quot;&lt;/span&gt;, sw.ElapsedMilliseconds);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Calc();
      
      IntX.GlobalSettings.MultiplyMode = MultiplyMode.Classic;
      Calc();
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;First Calc() call uses fast multiplication implementation (which is default), second - classic one. On my machine (Win XP Pro SP2, P4 2.8 GHz, 2 GB RAM) first call is &lt;b&gt;70 times&lt;/b&gt; faster than the second one (1 second against 68 seconds). Resulting number has 1,702,101 digits; you can get it here: &lt;a href="javascript:window.location.href='http://intx.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=27428';"&gt;42pow1048576.txt&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Another example is factorial calculation:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;"&gt;using&lt;/span&gt; Oyster.Math;
 
&lt;span style="color:Blue;"&gt;namespace&lt;/span&gt; IntxTestApp
{
  &lt;span style="color:Blue;"&gt;class&lt;/span&gt; Program
  {
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; IntX Factorial(IntX n)
    {
      &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (n &amp;lt; 0)
      {
        &lt;span style="color:Blue;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ArgumentException(&lt;span style="color:#A31515;"&gt;&amp;quot;Can&amp;#39;t calculate factorial for negative number.&amp;quot;&lt;/span&gt;, &lt;span style="color:#A31515;"&gt;&amp;quot;n&amp;quot;&lt;/span&gt;);
      }
      &lt;span style="color:Blue;"&gt;return&lt;/span&gt; n == 0 ? 1 : n * Factorial(n - 1);
    }
    
    &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Main()
    {
      Console.WriteLine(Factorial(10000));
    }
  }
}
&lt;/pre&gt;&lt;/div&gt;As you can see, IntX implements all the standard arithmetic operators so its usage is transparent for developer - like if you&amp;#39;re working with usual integer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;FHT and Calculations Precision&lt;/b&gt;&lt;br /&gt;Internally IntX library operates with floating-point numbers when multiplication using FHT is performed so at some point it stops working correctly and loses precision. Luckily, this unpleasant side-effects effects are starting to appear when integer size is about 2^28 bytes i.e. for really huge integers. Anyway, to catch such errors I have added FHT multiplication result validity check into code - it takes N last digits of each big integer, multiplies them using classic approach and then compares last N digits of classic result with last N digits of FHT result (so it&amp;#39;s kind of simplified CRC check). If any inconsistency is found then FhtMultiplicationException is thrown; this check can be disabled using global settings.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Future Plans&lt;/b&gt;&lt;br /&gt;I have no plans to further develop this library - I&amp;#39;m just sharing it with the community.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;System.Numerics.BigInteger in .NET 4.0&lt;/b&gt;&lt;br /&gt;According to the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx" class="externalLink"&gt;BCL team blog&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; MS is going to introduce System.Numerics.BigInteger class in .NET 4.0, this time for sure :) Once I saw this blog entity I was interested in performance of their solution - you can download preliminary version together with &lt;a href="http://code.msdn.microsoft.com/solverfoundation" class="externalLink"&gt;MS Solver Foundation&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (it&amp;#39;s for .NET 3.5), it&amp;#39;s called Microsoft.SolverFoundation.Common.BigInteger in there. I did some tests and it appears that BigInteger has performance in general comparable with IntX on standard operations but starts losing when FHT comes into play (when multiplying really big integers, for example).&lt;br /&gt;&lt;br /&gt;So internally System.Numerics.BigInteger seems to use standard arbitrary arithmetic algorithms and I am not worrying about IntX library since, due to its use of FHT, it can be times faster for really big integers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Comparing IntX Performance to GMP&lt;/b&gt;&lt;br /&gt;While IntX library is fast among other libraries written for .NET it can&amp;#39;t really compete with &lt;a href="http://gmplib.org/" class="externalLink"&gt;The GNU MP Library&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; - one of the fastest well-known Bignum libraries in the world. GNU MP (or GMP) is written in plain C and Assembly language so it compiles into optimized native code, it also uses fast calculation algorithms - that&amp;#39;s why it&amp;#39;s very fast. I have compared IntX performance to GMP on basic arithmetic operations and it appeared that &lt;b&gt;GMP is 2-10 times faster than IntX&lt;/b&gt;. Actually for me it is a good result - IntX written in managed code is still quite fast :) GMP is also about 10x faster on a huge integers since, as well as IntX, it implements fast multiplication algorithm using FFT. GMP also has support for big floating-point numbers.&lt;br /&gt;&lt;br /&gt;So if you need to perform a lot of operations with big integers and must get the fastest possible speed from them then I&amp;#39;d suggest you using &lt;a href="http://gmplib.org/" class="externalLink"&gt;GMP&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (distributed under LGPL license). However, if 2-10x speed loss is okay for your project and/or you don&amp;#39;t want to include unmanaged code into your application then IntX should be enough for you. Btw, using GMP from C# is quite easy because there are GMP C#-wrappers available &lt;a href="http://www.emilstefanov.net/Projects/GnuMpDotNet/" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (this one I was using for tests) and &lt;a href="http://gnumpnet.codeplex.com/" class="externalLink"&gt;there&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and maybe somewhere else as well :)&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>Oyster</author><pubDate>Wed, 20 Jan 2010 17:28:33 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20100120052833P</guid></item></channel></rss>