|
1 | 1 | package com.jwetherell.algorithms.mathematics;
|
2 | 2 |
|
| 3 | +import com.jwetherell.algorithms.numbers.Complex; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | + |
3 | 8 | public class Multiplication {
|
4 | 9 |
|
5 | 10 | public static final long multiplication(int a, int b) {
|
@@ -47,4 +52,78 @@ public static final long multiplyUsingLogs(int a, int b) {
|
47 | 52 | long result = Math.round(Math.pow(10, (Math.log10(absA) + Math.log10(absB))));
|
48 | 53 | return (a > 0 && b > 0 || a < 0 && b < 0) ? result : -result;
|
49 | 54 | }
|
| 55 | + |
| 56 | + public static String multiplyUsingFFT(String a, String b) { |
| 57 | + if (a.equals("0") || b.equals("0")) { |
| 58 | + return "0"; |
| 59 | + } |
| 60 | + boolean negative = false; |
| 61 | + if ((a.charAt(0) == '-' && b.charAt(0) != '-') || (a.charAt(0) != '-' && b.charAt(0) == '-')) { |
| 62 | + negative = true; |
| 63 | + } |
| 64 | + if (a.charAt(0) == '-') { |
| 65 | + a = a.substring(1); |
| 66 | + } |
| 67 | + if (b.charAt(0) == '-') { |
| 68 | + b = b.substring(1); |
| 69 | + } |
| 70 | + int size = 1; |
| 71 | + while (size < (a.length() + b.length())) { |
| 72 | + size *= 2; |
| 73 | + } |
| 74 | + Complex[] aCoefficients = new Complex[size]; |
| 75 | + Complex[] bCoefficients = new Complex[size]; |
| 76 | + for (int i = 0; i < size; i++) { |
| 77 | + aCoefficients[i] = new Complex(); |
| 78 | + bCoefficients[i] = new Complex(); |
| 79 | + } |
| 80 | + for (int i = 0; i < a.length(); i++) { |
| 81 | + aCoefficients[i] = new Complex((double) (Character.getNumericValue(a.charAt(a.length() - i - 1))), 0.0); |
| 82 | + } |
| 83 | + for (int i = 0; i < b.length(); i++) { |
| 84 | + bCoefficients[i] = new Complex((double) (Character.getNumericValue(b.charAt(b.length() - i - 1))), 0.0); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + FFT.FFT(aCoefficients); |
| 89 | + FFT.FFT(bCoefficients); |
| 90 | + |
| 91 | + |
| 92 | + for (int i = 0; i < size; i++) { |
| 93 | + aCoefficients[i] = aCoefficients[i].multiply(bCoefficients[i]); |
| 94 | + } |
| 95 | + for (int i = 0; i < size / 2; i++) { |
| 96 | + Complex temp = aCoefficients[i]; |
| 97 | + aCoefficients[i] = aCoefficients[size - i - 1]; |
| 98 | + aCoefficients[size - i - 1] = temp; |
| 99 | + } |
| 100 | + FFT.FFT(aCoefficients); |
| 101 | + |
| 102 | + ArrayList<Integer> res = new ArrayList<Integer>(); |
| 103 | + int pass = 0; |
| 104 | + for (int i = 0; i < size; i++) { |
| 105 | + res.add((int) (pass + Math.floor((aCoefficients[i].abs() + 1) / size))); |
| 106 | + if (res.get(i) >= 10) { |
| 107 | + pass = res.get(i) / 10; |
| 108 | + res.set(i, res.get(i) % 10); |
| 109 | + } else { |
| 110 | + pass = 0; |
| 111 | + } |
| 112 | + } |
| 113 | + Collections.reverse(res); |
| 114 | + StringBuilder result = new StringBuilder(); |
| 115 | + if (negative) { |
| 116 | + result.append('-'); |
| 117 | + } |
| 118 | + boolean startPrinting = false; |
| 119 | + for (Integer x : res) { |
| 120 | + if (x != 0) { |
| 121 | + startPrinting = true; |
| 122 | + } |
| 123 | + if (startPrinting) { |
| 124 | + result.append(x); |
| 125 | + } |
| 126 | + } |
| 127 | + return result.toString(); |
| 128 | + } |
50 | 129 | }
|
0 commit comments