What is the Karatsuba's algorithm for long integer multiplication?
- User
What is the Karatsuba's algorithm for long integer multiplication?
- Assistant
Karatsuba's algorithm is a fast multiplication algorithm developed by Anatolii Alexeevitch Karatsuba in 1960. It's a divide-and-conquer algorithm that significantly improves the efficiency of long integer multiplication compared to the classical method. The classical method has a time complexity of O(n^2), where n is the number of digits in the larger of the two numbers being multiplied. In contrast, Karatsuba's algorithm has a time complexity of approximately O(n^log2(3)), which is roughly O(n^1.585), making it faster for large integers.
Here's a basic overview of the Karatsuba's algorithm for multiplying two numbers x and y:
-
Split the input numbers x and y into two equal halves, a, b and c, d, respectively, such that x = a * 10^(n/2) + b and y = c * 10^(n/2) + d, where n is the number of digits in the larger number.
-
- User
could you show an example using go
- Assistant
Sure! Here is an example implementation of Karatsuba's algorithm for long integer multiplication in Go:
package main import ( "fmt" "math/big" ) func karatsuba(x, y *big.Int) *big.Int { // convert x and y to strings xStr := x.String() yStr := y.String() // check for the base case if len(xStr) == 1 || len(yStr) == 1 { // if x or y is a single digit, use simple multiplication mul := new(big.Int).Mul(x, y) return mul } // calculate the midpoint n := max(len(xStr), len(yStr)) mid := n / 2 // split x and y into halves xH, xL := split(x, mid) yH, yL := split(y, mid) // recursively calculate the three products p1 := karatsuba(xH, yH) p2 := karatsuba(xL, yL) p3 := karatsuba(add(xH, xL), add(yH, yL)) // calculate the final result using the three products result := sub(sub(p3, p1), p2) // shift the first product by 2*mid and the third product by mid p1Shift := shift(p1, 2*mid) resultShift := shift(result, mid) // add the three products to get the final result final := add(add(p1Shift, resultShift), p2) return final } // helper function to split a big integer into two halves func split(x *big.Int, n int) (*big.Int, *big.Int) { xStr := x.String() xH := new(big.Int) xL := new(big.Int) if len(xStr) <= n { xH.SetInt64(0) xL.SetString(xStr, 10) } else { xH.SetString(xStr[:len(xStr)-n], 10) xL.SetString(xStr[len(xStr)-n:], 10) } return xH, xL } // helper function to add two big integers func add(x, y *big.Int) *big.Int { add := new(big.Int).Add(x, y) return add } // helper function to subtract two big integers func sub(x, y *big.Int) *big.Int { sub := new(big.Int).Sub(x, y) return sub } // helper function to shift a big integer by n digits func shift(x *big.Int, n int) *big.Int { shift := new(big.Int).Lsh(x, uint(n)) return shift } // helper function to find the maximum of two integers func max(x, y int) int { if x > y { return x } return y } func main() { // test the algorithm with two large integers x := new(big.Int).SetString("3141592653589793238462643383279502884197169399375105820974944592", 10) y := new(big.Int).SetString("2718281828459045235360287471352662497757247093699959574966967627", 10) result := karatsuba(x, y) fmt.Println(result) }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)