0

i have 2 BigInteger one is constant which is the divisor i use below code to do the division but it takes so long when the dividend is so large . am so confuse coz i do other staff to the dividend even thaw is big but is much faster . Note that the while loop could round 23000K time

i tried to replace / with - but still the while loop will take so long

round = 1;

while (MaigicNumber >= divisor) // This may take up to 23000 times coz the Maigic

{ 

    BigInteger jv = BigInteger.Remainder(MaigicNumber, divisor);  
    BigInteger VVJ = jv;

    MaigicNumber = MaigicNumber - jv;
    MaigicNumber = MaigicNumber / divisor;
    PlusArray.Insert(round, VVJ);
    round++;

}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • [this post](https://stackoverflow.com/questions/1168451/is-shifting-bits-faster-than-multiplying-and-dividing-in-java-net) says that the compiler has probably already optimized your division procedure and a conversion to bit shifting will probably not be much faster – Mong Zhu Jun 14 '19 at 06:47
  • did you try to measure which of the lines inside the loop takes actually the most time to perform? – Mong Zhu Jun 14 '19 at 06:49
  • 2
    You could use [BigInteger.DivRem](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.divrem?view=netframework-4.8) instead of separate calculations of remainder and division. I.e. `MaigicNumber = BigInteger.DivRem(MaigicNumber, divisor, out VVJ)` – heijp06 Jun 14 '19 at 06:53
  • @MongZhu you mean this is the fastest way the compiler could go to do only division ? but for the same bigInteger i do covert it to binary or * to get it back and it goes 1000 time faster , why only in division it's take so long – zezo alharazi Jun 14 '19 at 07:07
  • @PetervanderHeijden that does't change match still slow – zezo alharazi Jun 14 '19 at 07:07
  • Is [this exp/log trick](https://stackoverflow.com/a/11859314/1997232) faster? – Sinatr Jun 14 '19 at 07:15
  • "ou mean this is the fastest way" I don't :) I can only assume that the compiler already optimized it as said in the post. We still don't know whether it is the division that needs the most time. – Mong Zhu Jun 14 '19 at 07:33
  • 1
    I probably don't understand this, but what are you doing exactly? You're saying your implementing division? BigInteger already does division, what am I missing here? My confusion stems from "I use below code to do the division", why not just `MaigicNumber / divisor` ? – Lasse V. Karlsen Jun 14 '19 at 07:51
  • How expensive is PlusArray.Insert() (ie does removing that call speed up the loop significantly)? – tolanj Jun 14 '19 at 08:34
  • PlusArry is saving the Reminder so i can generate the MaigicNumber again , anyway i don't think this is what slow it , coz i remove it and still slow – zezo alharazi Jun 14 '19 at 22:31
  • @LasseVågsætherKarlsen yes i do the division but its very slow when it in the while loop so am trying to find faster way – zezo alharazi Jun 14 '19 at 22:32

0 Answers0