CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
commit 6278f53bdbaeeceadc011423970667d5c3fe330d
parent 18d66af70521246955ea10a66dea0976e875fd25
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Thu,  2 May 2024 00:05:11 -0300

Remove comments and redundant code

Diffstat:
Mguias/3/ej11.cpp | 27+++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/guias/3/ej11.cpp b/guias/3/ej11.cpp
@@ -1,4 +1,4 @@
-// Fast multiplication of large integers
+// Fast multiplication of large integers using Karatsuba algorithm
 // https://en.wikipedia.org/wiki/Karatsuba_algorithm
 // https://www.youtube.com/watch?v=k_j5TAPQf7k
 // https://www.youtube.com/watch?v=yWI2K4jOjFQ
@@ -6,6 +6,10 @@
 #include <iostream>
 #include <cmath>
 
+long max(long x, long y) {
+    return (x > y) ? x : y;
+}
+
 unsigned int size_base10(long n) {
     return (abs(n) <= 9) ? 1 : (1 + size_base10(n/10));
 }
@@ -15,27 +19,22 @@ long shift_right_by_base10(long n, unsigned int m) {
 }
 
 long karatsuba(long x, long y) {
-    if((x < 10) || (y < 10)) {
-        std::cout << x*y << std::endl;
-        return x*y; // fall back to traditional multiplication
-    }
+    if((x < 10) || (y < 10))
+        return x*y; // fall back to traditional multiplicatio
 
-    unsigned int n1 = size_base10(x);
-    unsigned int n2 = size_base10(y);
+    unsigned int n = max(size_base10(x), size_base10(y));
 
-    long a = shift_right_by_base10(x, (n1/2));
-    long b = (x % (long)pow(10, n1/2));
+    long a = shift_right_by_base10(x, (n/2));
+    long b = (x % (long)pow(10, n/2));
 
-    long c = shift_right_by_base10(y, (n2/2));
-    long d = (y % (long)pow(10, n2/2));
+    long c = shift_right_by_base10(y, (n/2));
+    long d = (y % (long)pow(10, n/2));
 
     long ac = karatsuba(a, c);
     long bd = karatsuba(b, d);
     long ad_plus_bc = (karatsuba(a+b, c+d)-ac-bd);
 
-    long res = ((ac*pow(10, (n2/2)*2)) + (ad_plus_bc*pow(10, n2/2)) + bd);
-    std::cout << res << std::endl;
-    return res;
+    return ((ac*pow(10, (n/2)*2)) + (ad_plus_bc*pow(10, n/2)) + bd);
 }
 
 int main (void) {