Hi,
maybe I drifted already away from the initial target*, but I programmed and (not very) fast version for doing Pow(a,b) with nicely big numbers...
But, it's not very fast, you have to wait around 30 seconds (with enabled debugger) for the calculation of 1538422836^10000...
Here is my code (with some additionally needed routines of my VNF project):
Code: Select all
; Version 0.5.0
; Define VNF - Vogels Number Format
#VNFDim=25000; ~225000 Ciphers ;)
Structure VNF
len.w
sign.w
num.l[#VNFDim]
EndStructure
; Signs
#VNFNegative=1
#VNFPositive=0
; Errortypes (len-field)
#VNFUndefined=-1
#VNFInfinitive=-2
#VNFOverflow=-3
#VNFNotImplemented=-4
; Dimensions
#VNFLen=9
#VNFMod=1000000000
Global VNF_Cache_A.VNF
Global VNF_Cache_B.VNF
Global VNF_Cache_C.VNF
; EndDefine
Procedure.s Number(*num.VNF)
Protected n
Protected t.s
If *num\len<0
Select *num\len
Case #VNFUndefined
t="Undefined"
Case #VNFInfinitive
t="Infinitive"
If *num\sign
t="Minus "+t
EndIf
Case #VNFOverflow
t="Overflow"
Case #VNFNotImplemented
t="Not Implemented"
EndSelect
Else
If *num\sign
t="-"
EndIf
n=*num\len
t+Str(*num\num[n])
While n>0
n-1
t+RSet(Str(*num\num[n]),#VNFLen,"0")
Wend
EndIf
ProcedureReturn t
EndProcedure
Procedure PutNumber(*s.s,*num.VNF)
Protected n,p
Protected t.s
*num\sign=#VNFPositive
t=PeekS(@*s)
n=Len(t)-#VNFLen
While n>0
*num\num[p]=Val(PeekS(@t+n,#VNFLen))
p+1
n-#VNFLen
Wend
n=Val(PeekS(@t,9+n))
If n<0
*num\sign=#VNFNegative
n=-n
EndIf
*num\num[p]=n
*num\len=p
EndProcedure
Procedure VNFNormalize(*a.VNF)
; Strap leading zeros (000.000.000)
If *a\len>=0
While (*a\len>=0) And (*a\num[*a\len]=0)
*a\len-1
Wend
; Value equal Zero? Positiv sign
If *a\len<0
*a\len=0
*a\sign=#VNFPositive
EndIf
EndIf
EndProcedure
Procedure VNFError(type,stype,*a.VNF)
If type>=0
type=stype
EndIf
Debug "ERROR "+Str(type)
*a\len=type
EndProcedure
Procedure VNFCopy(*a.VNF,*b.VNF)
Protected n
n=*a\len
*b\len=n
*b\sign=*a\sign
While n>=0
*b\num[n]=*a\num[n]
n-1
Wend
EndProcedure
Procedure VNFAbs(*a.VNF)
*a\sign=#VNFPositive
EndProcedure
Procedure.l VNFPositive(*a.VNF)
If *a\sign=#VNFPositive
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure VNFMuW(*a.VNF,b,*c.VNF)
Protected n
Protected m.q
n=*a\len
If n<0
VNFError(n,0,*c.VNF)
Else
If b=0
*c\len=0
*c\sign=#VNFPositive
*c\num[0]=0
Else
If b<0
b=-b
*c\sign=#VNFNegative-*a\sign
Else
*c\sign=*a\sign
EndIf
n=0
Repeat
m+*a\num[n] * b
*c\num[n]=m%#VNFMod
m/#VNFMod
n+1
Until n>*a\len
If m
*c\num[n]=m
Else
n-1
EndIf
*c\len=n
EndIf
EndIf
EndProcedure
Procedure VNFMul(*a.VNF,*b.VNF,*c.VNF)
Protected la
Protected lb
Protected n
Protected s.q
Protected z.q
If (*a\len<0) Or (*b\len<0)
VNFError(*a\len,*b\len,*c.VNF)
Else
*c\sign=(*a\sign + *b\sign)&1; because #VNFNegative=1
*c\len=*a\len+*b\len+1
For n=0 To *c\len
*c\num[n]=0
Next n
la=0
Repeat
lb=0
z=*b\num[lb]
Repeat
s=*a\num[la] * *b\num[lb] + *c\num[la+lb]
*c\num[la+lb]=s%#VNFMod
*c\num[la+lb+1]+s/#VNFMod
lb+1
Until lb>*b\len
la+1
Until la>*a\len
VNFNormalize(*c)
EndIf
EndProcedure
Procedure VNFSqr(*a.VNF)
If *a\len>=0
VNFMul(*a,*a,VNF_Cache_C)
VNFCopy(VNF_Cache_C,*a)
EndIf
EndProcedure
Procedure VNFPow(*a.VNF,b,*c.VNF)
Protected z=0,s,sign
If b<0
; a^b | b<0 not implemented for now...
VNFError(#VNFNotImplemented,0,*c.VNF)
ElseIf *a\len>=0
VNFCopy(*a,VNF_Cache_A)
PutNumber(@"1",VNF_Cache_B)
If VNFPositive(*a)=#False
VNFAbs(VNF_Cache_A)
If b&1 : VNF_Cache_B\Sign=#VNFNegative : EndIf
EndIf
While b>0
s=1<<z
If b&s
VNFMul(VNF_Cache_A,VNF_Cache_B,VNF_Cache_C); *1
VNFCopy(VNF_Cache_C,VNF_Cache_B); *2
b-s
EndIf
If b
VNFSqr(VNF_Cache_A)
z+1
EndIf
Wend
VNFCopy(VNF_Cache_B,*c)
EndIf
EndProcedure
x.VNF
y.VNF
z.VNF
time=-GetTickCount_()
PutNumber(@"2",x)
VNFPow(x,1000,z)
Debug "2^1000="+Number(z)
PutNumber(@"1538422836",x)
VNFPow(x,10000,z)
Debug "Lenght of 1538422836^10000: "+Str(Len(Number(z)))
time+GetTickCount_()
Debug ""
Debug "Calculation time was "+Str(time)+" ms"
For clever guys

... I didn't want to use (many of the) global variables VNF_Cache, but things, like VNFMul(VNF_Cache_A,*c,*c) instead of lines *1 andf *2 did not work - anyone sees why?
For very clever guys

... I didn't look into math books for creating the power function, so maybe there are possibilities to use a better approach...
_________________
*) if factorizing is the real target, forget it! RSA is based on the fact, that finding the dividers of huge numbers is a time consuming work...