How to multiply large numbers with all digits in the output in R?

R ProgrammingServer Side ProgrammingProgramming

To multiply large numbers with all digits in the output, we can use mul.bigz function of gmp package. For example, if we have two vectors say x and y each containing numbers of large size then the multiplication of these numbers that will return all the digits of the multiplication can be done by using the command mul.bigz(x,y).

Example1

Loading gmp package and multiplying vectors containing large number values −

library(gmp)
x1<-sample(1111111111:2000000000000,50,replace=TRUE)
x1

Output

[1] 1.293832e+12 2.536972e+11 3.335227e+11 3.226438e+11 1.136448e+12
[6] 1.325300e+12 1.246996e+12 7.080295e+11 1.449619e+12 1.975834e+12
[11] 1.156376e+12 1.511857e+11 1.513911e+12 1.917348e+12 2.271347e+11
[16] 2.282941e+11 3.337803e+10 3.172004e+11 2.763222e+11 3.303886e+11
[21] 6.027465e+11 1.193693e+12 1.150794e+12 1.606291e+12 1.650506e+11
[26] 6.523897e+11 1.465895e+11 2.352293e+11 1.963742e+12 4.443070e+11
[31] 1.007327e+12 1.869703e+12 7.239362e+10 1.095902e+12 1.424147e+12
[36] 9.770790e+11 1.932433e+12 9.026232e+10 2.368810e+11 1.134507e+12
[41] 1.124247e+12 4.035505e+11 3.874052e+11 1.406390e+12 9.311580e+11
[46] 1.839350e+12 8.464039e+11 1.594256e+12 1.957315e+12 1.893819e+12
y1<-sample(1111111111:2000000000000,50,replace=TRUE)
y1
[1] 9.757425e+11 1.239017e+12 1.927748e+12 1.103209e+12 1.252230e+12
[6] 1.913173e+12 2.805219e+11 1.616006e+12 5.303620e+11 1.772930e+12
[11] 5.785048e+11 1.941363e+12 9.501223e+11 1.194540e+11 9.843814e+11
[16] 1.275164e+12 1.149460e+12 4.931133e+11 2.878916e+11 9.617385e+11
[21] 8.967935e+10 2.457275e+11 1.983314e+12 1.423445e+12 1.460684e+12
[26] 8.755605e+11 1.049124e+10 8.055053e+11 1.892869e+12 1.332796e+12
[31] 1.786657e+11 1.891837e+12 1.313989e+12 5.162383e+11 7.123930e+11
[36] 1.056097e+12 1.002861e+12 1.596295e+12 8.812744e+11 1.822768e+12
[41] 1.986071e+12 8.144542e+11 1.313296e+12 1.620869e+12 1.089260e+12
[46] 3.866624e+11 3.544274e+10 1.084693e+12 7.736200e+11 1.877018e+12

mul.bigz(x1,y1)

Big Integer ('bigz') object of length 50 −

[1] 1262446842630801588182744 314335263114643191308025
[3] 642947682842452511840295 355943440341646141283918
[5] 1423093232364797810527056 2535528390293637035755604
[7] 349809716738243739838950 1144179721857914606449920
[9] 768823090887235726818380 3503014875764875234341320
[11] 668969310756954173204964 293506305139251500087165
[13] 1438400321537589823512060 229034861133298648495137
[15] 223587218906467123271286 291112568955671946722508
[17] 38366694886332866751264 156415710483362775931498
[19] 79550851780737264040110 317747464761895523642170
[21] 54053916388740069983216 293323055941642114974812
[23] 2282386213646481329573654 2286467890398288112173213
[25] 241086633841183470615700 571206608578182798317112
[27] 1537904707923988857285 189478455160811542797552
[29] 3717105891067599244132614 592170438410277921759253
[31] 179974758886936119715152 3537173034917116273935748
[33] 95124429522919713206664 565746451368620014494732
[35] 1014552600906136853771796 1031890578442049614902030
[37] 1937962067406571980502803 144085305439484188977162
[39] 208757163619377106848154 2067942795859405778109039
[41] 2232833800223648463028000 328673404564488209660349
[43] 508777582753809846486000 2279572731830085975246096
[45] 1014273211890090760161484 711207747512107582798215
[47] 29998871689014859842094 1729279092817166762068120
[49] 1514217632310567242102048 3554730826730585234031496

Example2

x2<-sample(10000000000000:99999999999999,20,replace=TRUE)
x2

Output

[1] 6.398159e+13 3.449662e+13 8.759618e+13 7.827480e+13 9.839172e+13
[6] 3.358731e+13 8.155870e+13 7.192655e+13 3.195451e+13 2.746856e+13
[11] 9.679501e+13 1.096879e+13 5.691945e+13 9.353371e+13 7.356300e+13
[16] 6.404821e+13 5.270617e+13 9.270720e+13 2.331605e+13 1.808134e+13
y2<-sample(10000000000000:99999999999999,20,replace=TRUE)
y2
[1] 1.578880e+13 5.392305e+13 1.986629e+13 3.337975e+13 3.774220e+13
[6] 7.179982e+13 7.803195e+13 9.259463e+13 7.186874e+13 8.144816e+13
[11] 5.213669e+13 6.158151e+13 1.950020e+13 4.154611e+13 1.073097e+13
[16] 3.751478e+13 2.246565e+13 7.609600e+13 3.265019e+13 4.636295e+13

mul.bigz(x2,y2)

Big Integer ('bigz') object of length 20 −

[1] 1010192180202107970994971432 1860163031405667083998113108
[3] 1740211585288594421359825344 2612793315213298928893124768
[5] 3713520183073253035371431712 2411562809935191309538846932
[7] 6364184340808390699280865988 6660011976326566517283305006
[9] 2296529992728176200215691276 2237263799718018254172544399
[11] 5046571255171509579300643832 675474862453600877730576637
[13] 1109940803643771324345467256 3885961688721650122562301624
[15] 789402715998041502331341836 2402754200327592234158069949
[17] 1184078490689530317843364890 7054647206647305451800626298
[19] 761273516615824233808304301 838304372728305526321584927
raja
Published on 06-Mar-2021 13:47:23
Advertisements