Voici quelques fonctions qui permettent de réliser des opérations mathématiques pour de très grands nombres, comme des multiplications, des additions, des soustractions, des modulos, des transformations de Decimal en binaire et des division euclidiennes. J'ai aussi fait une petite fonction mgmod qui permet de calculer le modulo d'une expression mathématique du genre: (2555 x 3251^5585) mod 5223
Bon certaines sont rapide (multiplication, addition, soustraction) d'autres sont plus lentes (division, modulo, conversion binaire)
Je vais sans doute encore faire quelques fonctions et alors je les ajouterai.
Voici un exemple de ce que j'ententds par "opération sur de grands nombres":
7467636322858211413420378952487507706693561915417437336022
8558108410407266652454504706493231612117134305999525878380
1997265629154474676363228582114134203789524875077066935619
1554174373360228581084104072666524545047064932316121171343
0559995258783801972656291544746763632285821141342037895248
7550770669356191541743733602285810841040726665245450470649
3223161211713430599952587838019726562915447467636322858211
4113420378952487507706693561915417437336022858108410407266
6552454504706493231612117134305999525878380197265629154474
6776363228582114134203789524875077066935619154174373360228
5881084104072666524545047064932316121171343059995258783801
9772656291544746763632285821141342037895248750770669356191
5441743733602285810841040726665245551571749433261312724531
6009063598848010737672026558578647323968212413431379053598
5118717704662925428548446023968219411408376752565515717494
3332613127245316090635988480107376720265585786473239682124
1334313790535985187177046629254285484460239682194114083767
525655157174943326131272453160
.
6859610553647623715964088665892371829439866709240283944117
9881257041327422001125735162772203142673516307553314590384
6660055334469068596105536476237159640886658923718294398667
0992402839441179812570413274220011257351627722031426735163
0775533145903846600553344690685961055364762371596408866589
2337182943986670924028394411798125704132742200112573516277
2220314267351630755331459038466005533446906859610553647623
7115964088665892371829439866709240283944117981257041327422
0001125735162772203142673516307553314590384660055334469068
5996105536476237159640886658923718294398667092402839441179
8112570413274220011257351627722031426735163075533145903846
6000553344690685961055364762371596408866589237182943986670
9224028394411798125704132742200112674526387320324368462731
8665332569149576116644556917950611664758624725074199775893
4882830449977810340293054227081367151338522001236745263873
2003243684627318653325691495761166445569179506116647586247
2550741997758934828304499778103402930542270813671513385220
012367452638732032436846273186
=
5122507693108052051908131512143089045229455175415561596741
4115663320922257779260847962552493629779254239908654457854
7659795059380401223878952817987873561967046683029218298414
2704888506355992220892718575858943821306020756928844917260
1496814741818629561724705921381618263940050903619971854955
8685128375567898616549668937396924474582292644083436280903
0812870055701817042120421461679454734263999108523374019658
6451150982120163378294892403702165186723504158521072906024
6649698023514459288310117204608799822735728798496294238718
1147617418805786043572392261647334696163781388547460987364
4126572600108078453271243617959223979304080406789433849922
8721700389844931997521438450798888152626132814848278783337
0708589556746040135130413611298116349872506712381092875331
6592897730971005286272040682228164590074535276472462380813
0290545735206587837092572751966838238347537089649127846050
1830690921868944930970943856770232612588229711802688655297
5277728524770351930009639830735857517804884275056341862026
1780535718109234418975292625952006076532656341063155337236
9181818656271974786119575839913894460868983639415262361546
8628770034899869858134004265662959464460657710892791284199
2367933788551152642524731528591847313677658077563349536155
6958583181392908498982119042700711601422928494954019211302
8551618783638472295513187440508782932175625878859575724470
2770007373414614404038449764618772060968878805298799649963
0607464601924005523355181576069452428773049062276275473456
5984580465841995786232172038971629630393927037411751117589
0164283174838692441834322539844525548518624048521840196103
6322686398713324751121414517540671701985694791213661014416
9398035099467589087299833087745203938775943418268558517359
7281873777374123921416215478295480002635918638207617728518
5512862839991918173933460492085976528134262265139816140734
9210686480183532557992081615581198053269887141629694325327
9572052414638358668481018444618982740877554078670905523641
1634934575218662724812951391323540645910814578206660365482
2101632180174420242263665512029199613137209596909297489677
60
Cette multiplication n'a mis que quelques secondes à être calculée par la fonction multi(term1,term2) et ici, les deux termes font chacun 1000 chiffres.
Source / Exemple :
Private Function mgmod(term1 As String, term2 As String, expon As String, modul As String)
Do While expon <> "1"
term1 = modulo(term1, modul)
term2 = modulo(term2, modul)
If Val(Right(expon, 1)) Mod 2 = 0 Then
term1 = multi(term1, term1)
expon = divis(expon, "2", False)
Else
term2 = multi(term1, term2)
term1 = multi(term1, term1)
expon = soustraction(expon, "1")
expon = divis(expon, "2", False)
End If
Loop
term1 = modulo(term1, modul)
term2 = modulo(term2, modul)
total = multi(term1, term2)
total = modulo(Mid$(total, 1), modul)
mgmod = total
End Function
Private Function modulo(term1 As String, modul2 As String)
lediv = divis(term1, modul2, True)
modulo = Mid$(lediv, InStr(1, lediv, "r") + 1)
End Function
Private Function addition(term1 As String, term2 As String)
Dim nbd, nbt, nbu As Integer
st1 = Len(term1): st2 = Len(term2)
If st1 < st2 Then term1 = String(st2 - st1, "0") & term1
If st1 > st2 Then term2 = String(st1 - st2, "0") & term2
For i = 0 To Len(term1) - 1
nb1 = Mid$(term1, Len(term1) - i, 1): nb2 = Mid$(term2, Len(term2) - i, 1)
nbt = Val(nb1) + Val(nb2) + nbd
nbd = nbt \ 10: nbu = nbt Mod 10
total = nbu & total
Next i
addition = zero(Mid$(nbd & total, 1))
End Function
Private Function soustraction(term1 As String, term2 As String)
If pgdd(term1, term2) = 0 Then soustraction = "0": Exit Function
If pgdd(term1, term2) = 2 Then soustraction = "-" & soustraction(term2, term1): Exit Function
Dim nbd, nbt, nbu As Integer
st1 = Len(term1): st2 = Len(term2)
If st1 < st2 Then term1 = String(st2 - st1, "0") & term1
If st1 > st2 Then term2 = String(st1 - st2, "0") & term2
For i = 0 To Len(term1) - 1
nb1 = Mid$(term1, Len(term1) - i, 1): nb2 = Mid$(term2, Len(term2) - i, 1)
If Val(nb1) - Val(nb2) - nbd2 < 0 Then
nbd = Abs(nb1 - nb2) \ 10 + 1
nb1 = Val(nb1) + nbd * 10
End If
nbu = nb1 - nb2 - nbd2
nbd2 = nbd: nbd = 0
total = nbu & total
Next i
soustraction = zero(Mid$(total, 1))
End Function
Private Function multi(term1 As String, term2 As String)
total2 = "0"
Dim nbd, nbt, nbu As Integer
For i = 0 To Len(term2) - 1
nbd = 0
total = ""
nbr2 = Mid(term2, Len(term2) - i, 1)
For a = 0 To Len(term1) - 1
nb1 = Mid(term1, Len(term1) - a, 1)
nbt = nb1 * nbr2 + nbd
nbd = nbt \ 10: nbu = nbt Mod 10
total = nbu & total
Next a
total2 = addition(nbd & total & String(i, "0"), Mid$(total2, 1))
Next i
multi = zero(Mid$(total2, 1))
End Function
Private Function divis(term1 As String, term2 As String, rest As Boolean)
total = "0"
Do While Len(term1) > Len(term2)
nbz = Len(term1) - Len(term2) - 1
nbr1 = term2 & String(nbz, "0")
term1 = soustraction(term1, Mid$(nbr1, 1))
total = addition(Mid$(total, 1), "1" & String(nbz, "0"))
Loop
For i = 1 To 20
If term1 = term2 Then
total = addition(Mid$(total, 1), "1")
reste = "0"
Exit For
ElseIf pgdd(term1, term2) = 2 Then
reste = term1
Exit For
ElseIf pgdd(term1, term2) = 1 Then
term1 = soustraction(term1, term2)
total = addition(Mid$(total, 1), "1")
End If
Next i
If rest = True Then total = total & "r" & reste
divis = zero(Mid$(total, 1))
End Function
Private Function pgdd(term1 As String, term2 As String)
st1 = Len(term1): st2 = Len(term2)
If st1 > st2 Then pgdd = 1: Exit Function
If st1 < st2 Then pgdd = 2: Exit Function
For i = 1 To Len(term1)
If Mid(term1, i, 1) > Mid(term2, i, 1) Then pgdd = 1: Exit Function
If Mid(term1, i, 1) < Mid(term2, i, 1) Then pgdd = 2: Exit Function
Next i
pgdd = 0
End Function
Function zero(term1 As String)
For i = 1 To Len(term1) - 1
If Mid$(term1, 1, 1) = "0" Then term1 = Mid$(term1, 2) Else GoTo nom
Next i
nom:
zero = term1
End Function
Function DecToBin(nombre As String)
Do While nombre <> "0"
bini = Val(Right(nombre, 1)) Mod 2
biny = bini & biny
nombre = divis(nombre, "2", False)
Loop
DecToBin = biny
End Function
Conclusion :
Petite précision sur l'utilisation de certaines fonctions:
::MULTIPLICATION::
-----------------------
multi(term1,term2)
exemple: multi("250","4") donne "1000" ;-) evidement
::DIVISION::
---------------
divis(term1,term2,reste)
divise le term1 par term2 et renvoie le résultat, si reste est TRUE, la fonction renvoie le résultat suivit du caractère r et du reste de la division (Evite de devoir utiliser 2 fonction pour diviser et trouver le reste)
exemple: multi("201","4") donne "50r1" si reste = TRUE, "50" sinon
::ADDITION::
---------------
addition(term1,term2)
exemple: addition("250","4") donne "254" ;-) evidement
::SOUSTRACTION::
----------------------
soustraction(term1,term2)
exemple: soustraction("250","4") donne "246" ;-) evidement
::MODULO::
----------------------
modulo(term,modul)
exemple: modulo("203","4") donne "3"
::BINARISATION ;)::
-----------------------
DecToBin(nombre)
exemple: DecToBin("5232145") donne "10011111101011000010001"
::MegaModulo::
------------------
MgMod(term1,term2,expon,modul)
calcul (term2 * term1 ^ expon) mod modul
exemple: mgmod("255","571","5","50") donne "25"
-------------------
Voilà, j'espère que ces fonctions vous seront utiles.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.