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.