patrick.belon
patrick.belon

Reputation: 65

I need to divide very large numbers in java without using any external libraries (Homework)

I need to be able to do 1938757289258398239399949292934/14583949053930202 without using any external java functions or libraries. I have each digit of my numbers stored in a linked list. What else can I do?

Can someone help me divide an algorithm. I've been trying at it for a few hours now and i'm not getting anywhere.

Upvotes: 0

Views: 360

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198143

"External" Java libraries? (BigInteger is built-in, if you can use that.)

Otherwise, the simplest approach to division is probably binary, as follows. You know that log(a/b) = log(a) - log(b), so you have an estimate on the number of bits in the result. Let r be the result. In pseudocode,

for i = the most significant bit the result could have, iterating down to 0
  if (r + 2^i) * b <= a
    r += 2^i

Upvotes: 4

Related Questions