1
$\begingroup$

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:

4.703

0.36

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

Is there any faster way?

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

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)]]]];

Timing:

0.344

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

|improve this answer|||||
$\endgroup$
  • $\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$ – J. M.'s technical difficulties Nov 24 '12 at 12:27
  • $\begingroup$ Quotient[] and QuotientRemainder[] are two different functions, see... $\endgroup$ – J. M.'s technical difficulties Nov 24 '12 at 13:11
  • $\begingroup$ Oh sorry, I mistyped it $\endgroup$ – Mohsen Afshin 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.