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

Karatsuba algorithm splitting number in 3 strings

Im trying to code the Karatsuba algorithm with some changes. Instead of splitting each number into 2 strings, I want to split it into 3. For example: Common Karatsuba -> first number would goes into A and B. Second number would goes into C and D. …
vik440
  • 11
  • 2
1
vote
0 answers

How to implement Karatsuba multiplication such that it can handle odd numbers and non-equal lengths of X and Y?

I tried two methods. The first was to pad X and Y with 0s so they both become of even and equal length, for example: X = 123 , Y = 45678 becomes: X = 000123 , Y = 045678 a = 0 , b = 123 , c = 45 , d = 678 but the problem with this implementation is…
1
vote
1 answer

Karatsuba multiplication in Java

I am trying to do the Karatsuba multiplication here. The below code works for numbers with two digits (eg 78 * 34) but gives wrong results for numbers with digits greater than 2 (eg 5678 * 1234). I have attached the debugging logs. After a certain…
Haritha
  • 391
  • 1
  • 9
1
vote
1 answer

Multiply strings-Leetcode using Karatsuba algorithm with Python

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3" Output: "6" Example 2: Input: num1 = "123", num2 = "456" Output:…
kirti purohit
  • 401
  • 1
  • 4
  • 18
1
vote
1 answer

Similar algorithm implementation producing different results

I'm trying to understand the Karatsuba multiplication algorithm. I've written the following code: def karatsuba_multiply(x, y): # split x and y len_x = len(str(x)) len_y = len(str(y)) if len_x == 1 or len_y == 1: return x*y …
Shan S
  • 658
  • 5
  • 18
1
vote
1 answer

Recursive Karatsuba Algorithm giving Imprecise Answers

I've been trying to implement the Karatsuba multiplication algorithm in C++ for two large numbers of the same length. My code is behaving properly for smaller numbers, such as 3456*1492, but fails with large numbers such as those with 64-digits. It…
Arnav Garg
  • 33
  • 1
  • 7
1
vote
1 answer

JavaScript implementation of Karatsuba multiplication algorithm

Hi I am trying to implement karatsuba algorithm in Javascript. As of now the algorithm works fine for certain cases like when the length of integer is 4 or 8.When the length of integer is 6 it prints out wrong result Eg: 3141*2718=>8537238 (correct…
1
vote
1 answer

karatsuba implementation in c

I am trying to implement kartsuba's algorithm in c, I followed the pseudo code on Wikipedia, but the problem is I don't always get the correct result. here is the implementation: long long karatsuba(char* a,char* b){ if (atoi(a)<10 || atoi(b)<10) …
LonelyDaoist
  • 665
  • 8
  • 22
1
vote
1 answer

Karatsuba algorithm implementation: works for small ns, breaks for bigger ns

I'm working on an implementation of the Karatsuba algorithm of multiplying numbers, but unlike most implementations using Strings as the primary data structure instead of BigNumbers or longs. I've written a recursive solution to the problem that…
Jon Doe
  • 167
  • 1
  • 11
1
vote
1 answer

Karatsuba - polynomials multiplication with CUDA

I'm using CUDA for the iterative Karatsuba algorithm and I would like to ask, why is one line computed always different. First, I implemented this function, which computed the result always correctly: __global__ void kernel_res_main(TYPE *A, TYPE…
user8005765
1
vote
1 answer

Iterative Karatsuba algorithm parallelized and vectorized using OpenACC in C++

I'm trying to parallelize iterative version of Karatsuba algorithm using OpenACC in C++. I would like to ask how can I vectorize inner for loop. My compiler shows my this message about that loop: 526, Complex loop carried dependence of result->…
user8005765
1
vote
1 answer

Karatsuba algorithm for multiplication of polynomials

I am trying to implement the recursive Karatsuba algorithm for multiplication of two polynomials (of the same degree). My code still doesn't work for polynomials with degree greater than 1. Can anyone explain to me what I am doing wrong in my…
user8005765
1
vote
1 answer

Karatsuba Algorithm Overflow

I've been studying Karatsuba's algorithm on Wikipedia and I stopped at this section that confused me ..Why is there an overflow in this algorithm , I don't understand the steps he made to resolve this issue .here is a screenshot of my problem
1
vote
0 answers

runtime error:- maximum recursion depth exceeded in comparison

I know these kind of questions have already been asked but I'm stuck with this long integer multiplication problem. for large input size. import math import sys def karatsuba(num1,num2): y = 10 if num1
ankuselfie
  • 65
  • 1
  • 1
  • 11
1
vote
0 answers

Karatsuba algorithm in ARM NEON implementation seems to have an error

I found many copies of the same implementation of the Karatsuba algorithm for ARM NEON on the internet, including some science papers, eg http://conradoplg.cryptoland.net/files/2010/12/mocrysen13.pdf It looks like all these copies have the same…