for (size_t i = 0; i < factors.size(); i++) { if (i % 2 == 1) odd.push_back(factors[i]); else even.push_back(factors[i]); }
// 递归分别求奇偶项的离散傅里叶变换 odd = fft_algorithm(odd, inverse); even = fft_algorithm(even, inverse); size_t n = factors.size(); std::vector<std::complex<double>> result;
for (size_t i = 0; i < n; i++) { std::complex<double> w(cos(2 * PI * i / n), sin(2 * PI * i / n)); if (inverse) w = std::conj(w); result.push_back(even[i % (n / 2)] + w * odd[i % (n / 2)]); }
// 用两数填满长度为n的vector中,即得到最高次数n-1的多项式系数向量,高次项不足处补0 for (size_t i = 0; i < len; i++) a.push_back(digits[i]); for (size_t i = 0; i < number.len; i++) b.push_back(number.digits[i]);
while (a.size() < n) a.push_back(0); while (b.size() < n) b.push_back(0);
// 离散傅里叶变换 a = fft(a); b = fft(b);
// 时域相乘,结果存到a中 for (size_t i = 0; i < n; i++) { a[i] *= b[i]; }
// 离散傅里叶逆变换 a = ifft(a);
// 将变换结果转换为BigInteger BigInteger result("0", number.len + len); int carry = 0; for (size_t i = 0; i < number.len + len; i++) { int temp = (int)(a[i].real() + 0.5) + carry; result.digits[i] = temp % 10; carry = temp / 10; }