# What is the fastest way to split a number into parts?

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?

• You're aware of Mod[]'s partner Quotient[], aren't you? For that matter, have you seen QuotientRemainder[]? Nov 24, 2012 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)]]]];


Timing:

0.344


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

• 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]. Nov 24, 2012 at 12:27
• Quotient[] and QuotientRemainder[] are two different functions, see... Nov 24, 2012 at 13:11
• Oh sorry, I mistyped it Nov 24, 2012 at 13:22