Questions tagged [karatsuba]

Asymptotically fast multiplication algorithm for big integers. Understanding this algorithm and its implementations.

Karatsuba algorithm requires O(nlog₂3) single digit multiplication operations to multiply a pair of n-digit numbers and is therefore asymptotically faster than the common O(n²) multiplication algorithm.

It was proposed by Anatoly Karatsuba in a paper published in the Proceedings of the USSR Academy of Science in 1962.

A related tag is that covers both Schönhage–Strassen multiplication algorithm and Strassen matrix multiplication algorithm.

Useful links

92 questions
0
votes
2 answers

why this code is returning minus answer in karatsuba multiplication

"""karatsuba algo""" def fast(x,y): if len(str(x))==1 or len(str(y))==1: return x*y else: n = max(len(str(x)),len(str(y))) m = n//2 a = x//10**m b = x%10**m c = y//10**m d =…
0
votes
1 answer

How to recursively program a multiplication algorithms

I started a new course in algorithms. The professor is trying to create a multiplication algorithm with other rules. He divides the digits of the numbers we are trying to multiply into two groups for example. x = 3452 then a = 34, b = 52 (the same…
Alesso
  • 1
0
votes
0 answers

How to calculate O(n) of Karatsuba Algorithm?

Could you please explain to me how to properly calculate the O(n) of the Karatsuba Algorithm? I have read that it is equal to O(n^log_2(3)) but I still do not understand how to derive the value. First of all, I am a bit confused that the input…
Pasha
  • 1,768
  • 6
  • 22
  • 43
0
votes
1 answer

karatsuba algorithm implementation in python

Below is my python implementation for the Karatsuba multiplication algorithm. This code seems to work for most inputs, but starts failing after the digits grow too large. For example, with the 64 digit inputs x =…
Asif
  • 748
  • 3
  • 9
  • 32
0
votes
0 answers

Run timecomplexity of this code (karatsuba)

Can you please provide the time-complexity of this karatsuba algorithm which is run on 2 n-bit long numbers: function karatsuba(num1, num2) if (num1 < 10) or (num2 < 10) return num1*num2 /* calculates the size of the numbers…
pi2018
  • 347
  • 2
  • 11
0
votes
2 answers

Karatsuba RecursionError: maximum recursion depth exceeded while calling a Python object

I am trying to implement Karatsuba multiplication on Python. The inputs are two integers of length power of 2. They are of same length. def mult(x,y): if int(x) < 10 and int(y) <10: return int(x)*int(y) x_length = len(str(x))//2 …
MathsMy
  • 107
  • 3
0
votes
1 answer

Resolve RecursionError: maximum recursion depth exceeded while getting the str of an object

I'm not getting what is wrong in my code. How should I fix this recursion error in Karatsuba multiplication? def karatsuba(x,y): if len(str(x))==1 or len(str(y))==1: return int(x)*int(y) else: n=max(len(str(x)),len(str(y))) …
Muskan Kalra
  • 53
  • 1
  • 8
0
votes
1 answer

karatsuba's integer multiplication algorithm python

This code is not passing all the test cases, can somebody help? I only pass the straight forward test then it loses precision. import math import unittest class IntegerMultiplier: def multiply(self, x, y): if x < 10 or y < 10: …
0
votes
0 answers

Karatsuba algorithm for two polynomials of same degree

I'm trying to create a Karatsuba algorithm in python for two polynomials of the same degree which should return an array of the coefficients of the result. def karatsuba(a, b): n = len(a) aHigh = [] aLow = [] bHigh = [] bLow…
0
votes
1 answer

Karatsuba recursive code is not working correctly

I want to implement Karatsuba multiplication algorithm in python.But it is not working completely. The code is not working for the values of x or y greater than 999.For inputs below 1000,the program is showing correct result.It is also showing…
0
votes
1 answer

ArrayIndexOutOfBoundsException: 0 in Karatsuba Implementation

I'm implementing a Karatsuba algorithm, and am running into this exception. Some relevant code (I can post more if needed): From main: int degree = (input.nextInt() + 1); int A[] = new int[degree]; int B[] = new int[degree]; for(int i = 0; i <…
DanBan
  • 21
  • 5
0
votes
0 answers

Divide and conquer approach for Karatsuba algorithm

I'm trying to write Karatsuba Algorithm using the divide and conquer approach in Haskell. I did it with merge sort algorithm but can't figure it out here and it's a little bit embarrassing at this point. My main function looks like this: dz test end…
SuperM4n
  • 203
  • 4
  • 15
0
votes
1 answer

How to implement Karatsuba multiplication using bit manipulation

I'm implementing Karatsuba multiplication in Scala (my choice) for an online course. Considering the algorithm is meant to multiply large numbers, I chose the BigInt type which is backed by Java BigInteger. I'd like to implement the algorithm…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0
votes
1 answer

Implementing karatsuba recursion function in python class, errors

Previously I posted a question on this subject and it was answered quite well. Implementing merge sort function in python class, errors And yet there is still something that escapes me about recursion in a class. In the linked problem above, if I…
reka18
  • 7,440
  • 5
  • 16
  • 37
0
votes
1 answer

What's the best way to modify a bitwise Karatsuba-Algorithm to work with negative numbers?

I wrote this bitwise Karatsuba multiplication algorithm. It does not use strings or math.pow. It's just divide-and-conquer-recursion, bitwise operations and addition: def karatsuba(x,y): n = max(x.bit_length(), y.bit_length()) if n < 2: …
hardfork
  • 2,470
  • 1
  • 23
  • 43