I have very large numbers (million digits).

number = A*10^partlen + B

As of now, I split the numbers by this way:

 TotalLen = IntegerLength[a]; 
 partlen = IntegerPart[TotalLen/2];
 Print[First[Timing[A = IntegerPart[number/(10^partlen )]]]];
 Print[First[Timing[B = Mod[number, 10^partlen ]]]]

And the timings for a 6 million digits number are:



Converting the number to strings and rebuilding the parts is even slower than this.

Is there any faster way?

  • 2
    $\begingroup$ You're aware of Mod[]'s partner Quotient[], aren't you? For that matter, have you seen QuotientRemainder[]? $\endgroup$ Nov 24 '12 at 10:32

Per J.M. suggestion, I've got a ten fold increase in speed:

TotalLen = IntegerLength[a]; 

RightLen = Quotient[TotalLen, 2]; 

Print[First[Timing[{A, B} = QuotientRemainder[a, (10^RightLen)]]]];



Although the time decreased but it is yet so large just for pre-processing.

  • $\begingroup$ You didn't totally take my suggestion. Try RightLen = Quotient[TotalLen, 2]; Also, Mathematica is perfectly capable of parallel assignment: {A, B} = QuotientRemainder[number, 10^RightLen]. $\endgroup$ Nov 24 '12 at 12:27
  • $\begingroup$ Quotient[] and QuotientRemainder[] are two different functions, see... $\endgroup$ Nov 24 '12 at 13:11
  • $\begingroup$ Oh sorry, I mistyped it $\endgroup$ Nov 24 '12 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.