res.sign = u.sign;
for (i = 1; i < v; i += 1) {
- res *= u;
+ res = karatsuba(res, u);
}
return res;
*
*/
template < typename N, typename E >
-number< N, E > pot_dyc(const number< N, E > &x, const number< N, E > &y)
+number< N, E > pot_dyc_n(const number< N, E > &x, const number< N, E > &y)
{
assert(y.sign == positive);
//std::cout << "pot(" << x << ", " << y << ")\n";
std::cout << "y es 1 => FIN pot(" << x << ", " << y << ")\n";
return x;
}
- number< N, E > res = pot_dyc(x, y.dividido_dos());
+ number< N, E > res = pot_dyc_n(x, y.dividido_dos());
//std::cout << "y.dividido_dos() = " << y.dividido_dos() << "\n";
//std::cout << "res = " << res << "\n";
- res *= res;
+ res = naif(res, res);
//std::cout << "res = " << res << "\n";
if (y.es_impar())
{
//std::cout << y << " es IMPAR => ";
- res *= x; // Multiplico por el x que falta
+ res = naif(res, x); // Multiplico por el x que falta
+ //std::cout << "res = " << res << "\n";
+ }
+ //std::cout << "FIN pot(" << x << ", " << y << ")\n\n";
+ return res;
+}
+
+/* Idem que pot_dyc_n(), pero usa karatsuba() para las multiplicaciones. */
+template < typename N, typename E >
+number< N, E > pot_dyc_k(const number< N, E > &x, const number< N, E > &y)
+{
+ assert(y.sign == positive);
+ //std::cout << "pot(" << x << ", " << y << ")\n";
+ if (y == number< N, E >(1))
+ {
+ //std::cout << "y es 1 => FIN pot(" << x << ", " << y << ")\n";
+ return x;
+ }
+ number< N, E > res = pot_dyc_k(x, y.dividido_dos());
+ //std::cout << "y.dividido_dos() = " << y.dividido_dos() << "\n";
+ //std::cout << "res = " << res << "\n";
+ res = karatsuba(res, res);
+ //std::cout << "res = " << res << "\n";
+ if (y.es_impar())
+ {
+ //std::cout << y << " es IMPAR => ";
+ res = karatsuba(res, x);
//std::cout << "res = " << res << "\n";
}
//std::cout << "FIN pot(" << x << ", " << y << ")\n\n";