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
0 answers

How can i use the stride trick in Karatsuba multiplication of polynomials?

Can someone explain what is the stride trick in general? How can I use it when implementing polynomial multiplication algorithms for which the polynomials are represented as coefficient arrays? How can stride trick make the implementation more…
esra
  • 201
  • 2
  • 8
0
votes
0 answers

Big integer multiplication with Karatsuba crashes with Segementation Fault because of Stack Overflow

So I'm developing a class "bignum" to operate with integers of any size. The thing is I made two different functions to multiply bignums. One is the standard multiplication and the other one is based on the karatsuba algorithm. I've got a small…
0
votes
0 answers

Why a recursion error occurs when implementing the Karachuba algorithm in python

Karachuba algorithm : https://en.wikipedia.org/wiki/Karatsuba_algorithm threshold = 4 def prod2(a, b): n = max(len(str(a)), len(str(b))) if a == 0 or b == 0: return elif n <= threshold: return a*b else: m =…
user9178840
0
votes
1 answer

std::make_unique(x) is giving me malloc(): invalid size (unsorted) error

I am trying to create a Bool Array class which takes a string of integers and converts them into bool arrays. Once I have 2 Bool Array variables I perform Karatsuba Multiplication. Below is where the actual error arises from. explicit…
Steveit
  • 51
  • 1
  • 9
0
votes
1 answer

Karatsuba multiplication in swift

I was trying to implement Karatsuba multiplication in swift. I wrote the below code and it is working fine for some smaller numbers but as the number gets bigger this code fails to give the correct answer. I have debugged in every possible way I can…
p gokul
  • 98
  • 7
0
votes
0 answers

Large Kartusba multiplication is failing. What is possibly going wrong?

I have implemented Karatsuba multiplication in Python. The code and pseudocode are as follow: def karatsuba(x, y): """ Input: two n-digit positive integers x and y. Output: the product x·y. Assumption: n is a power of 2. (NOT…
skad00sh
  • 171
  • 11
0
votes
1 answer

Coding Karatsuba multiplication

my code below seem to work well if either number's length is even, but if both length of number are odd(like in the pic), it outputs the wrong result. I have no idea where it went wrong or how to fix it. def karatsubaMulti(num1, num2): len1 =…
n.y
  • 25
  • 4
0
votes
0 answers

Wrong implementation of Karatsuba Multiplication

How can I implement the implementation of Karatsuba Multiplication? Inputs are given in a string. This is my code def multiply(x,y): x_len = len(x) y_len = len(y) lenn= max(x_len,y_len) len2=lenn//2 if x_len < 3 or y_len…
0
votes
1 answer

How to compare various multiplication algorithms over a range of numbers

While going through a MIT lecture in MITOpencourseware (6.006 lecture 12), I came across the mention of 4 multiplication algorithms (to multiply two n-digit numbers) - Ordinary naive approach with O(n^2) complexity Karatsuba algorithm -…
Anirban Chakraborty
  • 539
  • 1
  • 5
  • 15
0
votes
1 answer

Own implementation of Karatsuba algorithm

Hi guys I am trying to come up with my own implementation of Karatsuba multiplication algorithm where the base case is trivial multiplication when one number is a single digit. My code seems to be failing to yield the correct answer and I believe…
atr1
  • 25
  • 1
  • 4
0
votes
1 answer

Karatsuba Algorithm without BigInteger in Java, unexpected behaviour while recursion

So I want to run Karatsuba Algorithm without using class BigInteger in Java, so upon following the pseudo-code and this question, I came with the following code public static long recKaratsuba(long i1, long i2){ if(i1<10 || i2<10) { …
BrownBoi
  • 3
  • 3
0
votes
0 answers

Why karatsuba implemenation is giving wrong result

I made a program for BigInteger in which I implemented Addition Subtraction and Karatsuba but it is giving wrong result. After several debuting I am not able to figure out the problem. Here is my code:- // // Created by bothra on…
user13641306
0
votes
1 answer

Karatsuba Implementation C++

So I've decided to take a stab at implementing Karatsuba's algorithm in C++ (haven't used this language since my second coding class a life time ago so I'm very very rusty). Well anyhow, I believe that I've followed the pseudocode line by line but…
Michael
  • 49
  • 3
0
votes
2 answers

Karotsuba multiplication - cannot find the error

I am doing the Stanford's Algorithms MOOC and got stuck with Karatsuba multiplication algorithm programming assignment. Karatsuba multiplication is simply an algorithm for multiplication of two integer that is asymptotically faster than usual…
0
votes
0 answers

Karatsuba Algorithm in C++

I have implemented karatsuba multiplication algorithm in C++. I want to optimize it so that I can multiply two 64 digits numbers. Can someone help, please? Thank you :) int Karatsuba::count(long long int a) { ostringstream str1; str1 << a;…
Ayushi
  • 1