Code:
'My Library
'
' BIGINT
'
' A small library having support for so-called 'big integers',
' which are unsigned data types consisting of a maximum of
' 262144 bits = 32768 bytes.
' Compare this with a normal integer: 16 bits = 2 bytes!
'
' Actually, this version of BIGINT has support for:
' - String integers (max value = 32768x a 9)
' Have fun!
'
' -Neo Deus Ex Machina
'
' X
' XXX
' X X
' XXX XXX
'
' HAR-SoftWare
'
' PS: Due to the fact that this is a pure-qb big integer
' library, I think you will get out of string space when
' using large big integers (like 10000!)
DECLARE FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
DECLARE FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS
STRING)
DECLARE FUNCTION Fac$(n AS INTEGER)
CONST FALSE = 0
CONST TRUE = NOT FALSE
CONST LN.E = 2.718281828#
DEFINT A-Z
'$DYNAMIC
'$END_META
FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
'
' This functions adds two String ints to eachother and
' returns the answer in a string int
'
'declare stuff
DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
len1 = LEN(StrInt)
len2 = LEN(StrInt2)
'pick out maximum length of the strings
IF len1<len2 THEN
maxlen = len2
StrAns = SPACE$(len2 + 1)
ELSE
maxlen = len1
StrAns = SPACE$(len1 + 1)
END IF
'calculate StrInt + StrInt2
over% = 0
' PRINT maxlen
FOR c = 1 TO maxlen
IF c <= LEN(StrInt) THEN char1 = MID$(StrInt, len1 - c + 1, 1) ELSE char1 = "0"
IF c <= LEN(StrInt2) THEN char2 = MID$(StrInt2, len2 - c + 1, 1) ELSE char2 = "0"
val1 = VAL(char1)
val2 = VAL(char2)
' print val1,val2
ans$ = LTRIM$(STR$(val1 + val2 + over%))
MID$(StrAns, LEN(StrAns) - c + 1, 1) = RIGHT$(ans$, 1)
IF LEN(ans$) > 1 THEN
over% = VAL(LEFT$(ans$, 1))
ELSE
over% = 0
END IF
NEXT c
StrAns = LTRIM$(StrAns)
IF over% > 0 THEN StrAns = LTRIM$(STR$(over%)) + StrAns
SI.Add$ = StrAns
END FUNCTION
FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS STRING)
'
' This functions multiplies two String ints to eachother and
' returns the answer in a string int
'
'declare stuff
DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
DIM TempAns AS STRING
len1 = LEN(StrInt)
len2 = LEN(StrInt2)
'pick out maximum length of the strings
IF len1<len2 THEN
maxlen = len2
' StrAns = SPACE$(len2 + 1)
ELSE
maxlen = len1
' StrAns = SPACE$(len1 + 1)
END IF
StrAns = "0"
'calculate StrInt + StrInt2
over% = 0
' PRINT maxlen
FOR d = 1 TO len2
IF d>1 THEN
TempAns=STRING$(d-1,"0")
ELSE
TempAns=""
END IF
over% = 0
char2 = MID$(StrInt2, len2 - d + 1, 1)
val2 = VAL(char2)
FOR c = 1 TO len1
char1 = MID$(StrInt, len1 - c + 1, 1)
'print char1;" ";char2
val1 = VAL(char1)
ans$ = LTRIM$(STR$(val1 * val2 + over%))
'print ans$
TempAns = RIGHT$(ans$, 1) + TempAns
'print TempAns
IF LEN(ans$) > 1 THEN
over% = VAL(LEFT$(ans$, 1))
ELSE
over% = 0
END IF
NEXT c
IF over% > 0 THEN TempAns = LTRIM$(STR$(over%)) + TempAns
StrAns = SI.Add$(StrAns, TempAns)
'PRINT StrAns
NEXT d
StrAns = LTRIM$(StrAns)
SI.Multiply$ = StrAns
END FUNCTION
'---- Here is the function that can calculate factorials
FUNCTION Fac$(n AS INTEGER)
IF n <= 1 THEN Fac$ = "1": EXIT FUNCTION
ans$ = "1"
FOR C% = 2 TO n
ans$ = SI.Multiply$(ans$, LTRIM$(STR$(C%^)))
NEXT C%
Fac$ = ans$
END FUNCTION