Skip to content

Instantly share code, notes, and snippets.

@ReneSac
Created February 23, 2014 01:05
Show Gist options
  • Save ReneSac/cc7b4281eb2b277db80e to your computer and use it in GitHub Desktop.
Save ReneSac/cc7b4281eb2b277db80e to your computer and use it in GitHub Desktop.
proc `*%` *(x, y: BigInt): BigInt =
## Multiply two BigInts treating them as positive.
## The result is aways a positive BigNum.
# TODO: avoid seq initialization by doing the first multiplication round
# separatedly before.
declareSL(x, y, shorter, longer, tmp)
result = newBigInt(longer.len + shorter.len - 1)
for i, a in shorter:
result[i + longer.len - 1] = tmp.carry
for j, b in longer:
tmp = a * b + tmp.carry + result[i+j]
result[i+j] = tmp.value
if tmp.carry != 0:
result.add(tmp.carry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment