📄 bignumvb.bi
字号:
Attribute VB_Name = "bignumVB"
DefInt A-B, D-Z
DefLng C
' **************************************************************************
'Subject: Include file for large-integer arithmetic Basic library.
'Author : Sjoerd.J.Schaper - vspickelen@zonnet.nl
'URL : http://largeint.sourceforge.net/index.html
'Date : 12-05-2005
'Code : Visual Basic-callable FreeBasic 0.15b compiled dll.
' `[..] there is a bound to the number of symbols which the computer
' can observe at one moment. If he wishes to observe more, he must use
' successive operations.' Alan Turing, On computable numbers [..], 1936.
'
' **************************************************************************
' Global constants
' **************************************************************************
Global Const ui = 171, uj = 5119 ' number array n(ui, uj) upper bounds
Global Const LB = 15, MB = 32768, MH = 16384 ' binary storage base
Global Const M1 = 32767, M2 = 1073709056 ' masks MB - 1, (MB - 1)* MB
Global Const L10 = 0.221461873 ' 1/ 10log(MB)
Global Const LD = 8, MD = 100000000 ' decimal in/out base, MY < MB
Global Const MY = 10000&, Fx = 2147483629 ' fixed limit
Global Const t0 = -1, t1 = -2 ' pointers to swap-columns
Global Const t2 = -3, t3 = -4
'Maximal number length = uj / L10, approximately 9 * uj / 2.
'
' **************************************************************************
' Initialization and assignment
' **************************************************************************
'The LargeInt class is initialized with an 'Init(size%, name$)' call at
'module level. You have to declare constant indices for referencing the
'large integers you're planning to use ('CONST p = 0, q = 1, ..'), so that
'each number is associated with its own distinct pointer. These pointers
'are passed to the procedures. Don't call with same pointers ('Squ p, p'),
'and beware of duplicate references, for they will crash your program.
'
'In the following prototypes,
'the letters p, q, r, d, m denote 32-bit pointers to large integers;
'variables a, c, k, sw are 32-bit signed integers;
'g is a number string; f is any string.
'
Declare Function Init Lib "bignumVB" Alias "Init@8" (ByVal k As Long, _
ByVal f As String) As Integer
'Initialize the number-arrays and open the primetable.
'To be called with dummy k and f, return value = -1.
'
Declare Sub Letf Lib "bignumVB" Alias "Letf@8" (ByVal p As Long, _
ByVal c As Long)
'Assign long integer c to large integer p.
'
Declare Sub Pwr10 Lib "bignumVB" Alias "Pwr10@8" (ByVal p As Long, _
ByVal k As Long)
'Assign 10^ k to large integer p.
'
Declare Sub Pwr2 Lib "bignumVB" Alias "Pwr2@8" (ByVal p As Long, _
ByVal k As Long)
'Assign 2^ k to large integer p.
'
Declare Sub Rndf Lib "bignumVB" Alias "Rndf@8" (ByVal p As Long, _
ByVal k As Long)
'Assign to p a random positive number with length Abs(k).
'
Declare Sub Term Lib "bignumVB" Alias "Term@0" ()
'Close the primetable.
'
' **************************************************************************
' Conversion functions
' **************************************************************************
Declare Function DecLSB Lib "bignumVB" Alias "DecLSB@4" (ByVal p As Long) _
As Integer
'Return the four rightmost digits of large integer p.
'
Declare Function Decf Lib "bignumVB" Alias "Decf@4" (ByVal p As Long) _
As Integer
'Convert large integer p to base MD in array ln(),
'the bytelength of p is returned. Call before CnvSt.
'
Declare Sub CnvSt Lib "bignumVB" Alias "CnvSt@4" (ByVal g As String)
'Convert base MD number to string. First call k = Decf(p),
'create stringbuffer g = STRING$(k, "0") and pass to CnvSt.
'
Declare Sub Ratdec Lib "bignumVB" Alias "Ratdec@12" (ByVal g As String, _
ByVal p As Long, ByVal q As Long)
'Return the decimal expansion of rational p / q in [0, 1) in string g.
'
Declare Sub Readst Lib "bignumVB" Alias "Readst@8" (ByVal p As Long, _
ByVal g As String)
'Convert decimal number string g to large integer p.
'Except for a prefixed minus sign, there's no check
'on non-digit characters in the string.
'
' **************************************************************************
' Obtaining output
' **************************************************************************
'Use the printfunctions in "PrintFun.bas" or write your own.
'
' **************************************************************************
' Basic arithmetic functions
' **************************************************************************
Declare Sub Add Lib "bignumVB" Alias "Add@8" (ByVal p As Long, _
ByVal q As Long)
'Add p and q, the result is in p.
'
Declare Sub Chs Lib "bignumVB" Alias "Chs@4" (ByVal p As Long)
'Change the sign of number p.
'
Declare Sub Dcr Lib "bignumVB" Alias "Dcr@8" (ByVal p As Long, _
ByVal a As Long)
'Decrease large integer p with small positive a.
'
Declare Sub Divd Lib "bignumVB" Alias "Divd@12" (ByVal p As Long, _
ByVal d As Long, ByVal q As Long)
'Divide p by d. The quotient is in q, the remainder in p.
'
Declare Sub Inc Lib "bignumVB" Alias "Inc@8" (ByVal p As Long, _
ByVal a As Long)
'Increase large integer p with small positive a.
'
Declare Sub Mult Lib "bignumVB" Alias "Mult@12" (ByVal p As Long, _
ByVal q As Long, ByVal r As Long)
'Multiply p and q. The result is in p by default,
' 'Swp p, r' puts it in r.
'
Declare Sub Squ Lib "bignumVB" Alias "Squ@8" (ByVal p As Long, _
ByVal q As Long)
'Return the square of p in q.
'
Declare Sub Subt Lib "bignumVB" Alias "Subt@8" (ByVal p As Long, _
ByVal q As Long)
'Subtract q from p, the result is in p.
'To retain p, use 'Subt q, p: Chs q'.
'
' **************************************************************************
' Comparison and copying
' **************************************************************************
Declare Function Cmp Lib "bignumVB" Alias "Cmp@8" (ByVal p As Long, _
ByVal q As Long) As Integer
'Compare returns -1 if p < q, 0 if p = q, or 1 if p > q.
'
Declare Function Isf Lib "bignumVB" Alias "Isf@8" (ByVal p As Long, _
ByVal a As Long) As Integer
'Boolean: check if large integer p equals one-word value a.
'
Declare Function Ismin1 Lib "bignumVB" Alias "Ismin1@8" (ByVal p As Long, _
ByVal m As Long) As Integer
'Boolean: check if p = m - 1
'
Declare Sub Copyf Lib "bignumVB" Alias "Copyf@8" (ByVal p As Long, _
ByVal q As Long)
'Copy number p to q.
'
Declare Sub Swp Lib "bignumVB" Alias "Swp@8" (ByVal p As Long, _
ByVal q As Long)
'Exchange numbers p and q.
'
' **************************************************************************
' Bit manipulation
' **************************************************************************
Declare Sub Andf Lib "bignumVB" Alias "Andf@8" (ByVal p As Long, _
ByVal q As Long)
'Bitwise logical p AND q, the result is in p.
'
Declare Sub Lsft Lib "bignumVB" Alias "Lsft@8" (ByVal p As Long, _
ByVal k As Long)
'Large integer p is shifted left by k bits.
'
Declare Sub Rsft Lib "bignumVB" Alias "Rsft@8" (ByVal p As Long, _
ByVal k As Long)
'Large integer p is shifted right by k bits.
'
' **************************************************************************
' Modular arithmetic functions
' **************************************************************************
Declare Sub Modbal Lib "bignumVB" Alias "Modbal@8" (ByVal p As Long, _
ByVal m As Long)
'Balanced residue p modulo m: Abs(p) <= m \ 2.
'
Declare Sub Moddiv Lib "bignumVB" Alias "Moddiv@8" (ByVal p As Long, _
ByVal m As Long)
'Compute positive residue p modulo m.
'
Declare Sub Modmlt Lib "bignumVB" Alias "Modmlt@12" (ByVal p As Long, _
ByVal q As Long, ByVal m As Long)
'Multiply p and q, then reduce modulo m. The result is in p.
'
Declare Sub Modpwr Lib "bignumVB" Alias "Modpwr@12" (ByVal p As Long, _
ByVal q As Long, ByVal m As Long)
'Modular exponentiation: base p, exponent q, modulus m,
'the result is in p. This doubles as power function:
'first put 'Letf m, 0' to switch off modulo reduction.
'
Declare Sub Modsqu Lib "bignumVB" Alias "Modsqu@8" (ByVal p As Long, _
ByVal m As Long)
'Square p, then reduce modulo m.
'
' **************************************************************************
' Number theoretic functions
' **************************************************************************
Declare Sub Euclid Lib "bignumVB" Alias "Euclid@12" (ByVal p As Long, _
ByVal q As Long, ByVal d As Long)
'The extended Euclidean algorithm computes
'p^ -1 Mod q in p, q/ gcd in q and gcd(p, q) in d.
'
Declare Sub Fctl Lib "bignumVB" Alias "Fctl@8" (ByVal p As Long, _
ByVal a As Long)
'Computes factorial a(a-1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -