TIntX是INTX任意精度整数库的Pascal端口,其中大约是O(N * log N)乘法/除法算法实现。它在整数上提供了所有基本的算术操作,比较,位移动等。它还允许在不同碱基中解析数字并将其转换为字符串,也可以在任何碱基中。该库的优点是其快速乘法,除法以及从基础/基础转换算法。该算法的所有快速版本均基于使用快速hartley变换的大整数的快速乘法,该变换为O(N * log N * log log N)时间而不是经典O(N^2) 。
建立状态
这是一个使用TIntX计算42中的代码示例(即2^20(1 shl 20)):
uses // including only the non-obvious
SysUtils, uIntX, uEnums;
procedure Calc ();
var
valA, valB: UInt32;
Delta: Double;
begin
valA := GetTickCount;
TIntX. Pow ( 42 , 1048576 );
valB := GetTickCount;
Delta := (valB - valA) / 1000 ;
ShowMessage(Format( ' time elapsed is %f seconds ' , [Delta]));
end ;
procedure TForm1.Button1Click (Sender: TObject);
begin
Calc();
TIntX.GlobalSettings.MultiplyMode := TMultiplyMode.mmClassic;
Calc();
end ; First 'Calc()' call uses fast multiplication implementation (which is default),
second, classic one. On my machine (Windows 10 Update 2, Intel Core i3 2.53 GHz,
6 GB RAM), Compiled with 64 bits, first call took 0.30 seconds while the second one
took 17.91 seconds.Resulting number has 1,702,101 digits.
我内部实现的其他一些功能是
IntegerSquareRoot (Integer SquareRoot)
Square
GCD (Greatest Common Divisor (HCF))
LCM (Least Common Multiple)
AbsoluteValue (Get Absolute Value of a Negative TIntX)
Bézouts Identity
InvMod (Modular Inverse)
Factorial
IntegerLogN (base, number) (Gets IntegerLog of a number using a specified base)
Ln (The natural logarithm)
Log10 (The base-10 logarithm)
LogN (Logarithm of a number for a specified base)
Random (Now Uses PcgRandom Instead of Mersemme Twister)
Modular Exponentiation (ModPow)
IsProbablyPrime (based on Miller Rabin Primality Test)
如您所见, TIntX使用operator overloading来实现所有标准算术运算符,因此其使用情况对开发人员来说是透明的,例如与通常的整数一起工作。
当使用FHT乘法(快速Hartley变换)乘法时,内部TIntX库将使用浮点数数字运行,因此在某些时候,它停止正常工作并丢失精度。幸运的是,当整数大小约为2^28字节,即对于非常巨大的整数时,这种令人不愉快的副作用效果就会开始出现。无论如何,为了捕获此类错误,添加了一些代码,FHT乘法结果有效性检查到代码中 - 它需要每个大整数的最后数字,使用经典方法将它们乘以它们,然后将经典结果的最后N数字与FHT结果的最后N数字进行比较(因此,它是简化的CRC检查)。如果发现任何不一致,则会抛出EFhtMultiplicationException ;可以使用全局设置禁用此检查。
对于非常巨大的整数数字(例如上面的Power 1048576中的42个), ToString()呼叫可能需要花费很多时间才能执行。这是因为,内部TIntX大整数存储在UInt32数组中的2^32基数,并且为了生成小数线输出,应将其从2^32碱基转换为小数底座。有意选择此类数字存储方法 - 它使ToString()较慢,但有效地使用内存,并使数字上的原始操作比10键存储的功率快(这会使ToString()工作速度更快),并且通常使用计算比ToString() 。
支持的编译器
FreePascal 3.0.0 and Above.
Delphi 2010 and Above.
安装库。
方法一:
在“软件包”文件夹中使用提供的软件包。
方法两个:
将库路径和子路径添加到您的项目搜索路径。
单位测试。
要运行单元测试,
对于FPC 3.0.0及以上
Simply compile and run "IntXLib.Tests" project in "FreePascal.Tests" Folder.
方法一(使用TestInsight和Dunitx)(首选)。
1). Download and Install TestInsight (and DunitX if not available).
2). Open Project Options of Unit Test (IntXLib.Tests.TestInsight) in "Delphi.Tests"
Folder.
3). To Use TestInsight, right-click on the project, then select
"Enable for TestInsight" or "TestInsight Project".
Save Project then Build and Run Test Project through TestInsight.
方法两个(使用Dunitx控制台跑者)。
1). Download and Install DunitX (if not available).
2). Open Project Options of Unit Test (IntXLib.Tests.TestInsight) in "Delphi.Tests"
Folder.
3). Save Project then Build and Run Test Project..
###执照
此“软件”是根据MIT License (MIT)许可的。
1MhFfW7tDuEHQSgie65uJcAfJgCNchGeKf0x6c1DC21aeC49A822A4f1E3bf07c623C2C1978a98345367-40 Special Thanks to first of all, (Andriy Kozachuk) for creating the
Original CSharp version, members of Delphi Developers Community on Google Plus,
Uncle Midnight,Ron4Fun for various support offered.