title: Karatsuba multiplication java recursion code not working?
body: <p>I am trying to multiply two numbers using karatsuba multiplication. My java code is not working. I have used string as parameters and arguments so that we can multiply two n digit numbers (n is even). Also, I don't want to use long or BigInteger. Please help me to figure out my code mistake.</p>
  
  <pre><code>class karat{
  
  public static String karatsuba(String first, String second){
  
      if(first.length() &lt;= 1 || second.length() &lt;= 1)
          return String.valueOf(Long.parseLong(first)*Long.parseLong(second));
  
      String a = karatsuba(first.substring(0, first.length()/2), second.substring(0, second.length()/2));
  
      String b = karatsuba(first.substring(first.length() - first.length()/2, first.length()), second.substring(second.length() - second.length()/2, second.length()));
  
      String c = karatsuba(String.valueOf(Long.parseLong(first.substring(0, first.length()/2)) + Long.parseLong(first.substring(first.length() - first.length()/2, first.length()))), String.valueOf(Long.parseLong(second.substring(0, second.length()/2)) + Long.parseLong(second.substring(second.length() - second.length()/2, second.length()))));
  
      String d = String.valueOf(Long.parseLong(c) - Long.parseLong(b) - Long.parseLong(a));
  
      return String.valueOf(((int)Math.pow(10, first.length()))*(Long.parseLong(a)) + (((int)Math.pow(10, first.length()/2))*Long.parseLong(d)) + (Long.parseLong(c)));
  }
  
  public static void main(String[] args){
          String result = karatsuba("1234", "5678");
          System.out.println(result); }
  }
  </code></pre>
  
  <p>Can you also please refine my code.</p>
  
  <p>Numbers passed for multiplication - 1234 and 5678</p>
  
  <p>Output is - 6655870 (Incorrect)</p>
  
  <p>Output should be - 7006652 (Correct)</p>
  
  <p>Thank you </p>