0

I am trying to multiply n digit number using karatsuba multiplication.

I am getting output for single digit number (Example 4 and 5 = 20).

But, when I multiply 1234 and 5678 I am getting error and no output result.

I have updated the code and error message. Please check.

The error is Click here

import java.math.BigInteger;

class karatsubamultiplication{

public static BigInteger karat(BigInteger number1, BigInteger number2){

    if(number1.bitLength() < 4 && number2.bitLength() < 4)
        return number1.multiply(number2);

    //Finding maximum length of two 
    //int m = Math.max(number1, number2);
    String first = number1.toString();
    String second  = number2.toString();

    BigInteger a = new BigInteger(first.substring(0,  first.length()/2));
    BigInteger b = new BigInteger(first.substring(first.length() - first.length()/2, first.length()));

    BigInteger c = new BigInteger(second.substring(0,  second.length()/2));
    BigInteger d = new BigInteger(second.substring(second.length() - second.length()/2, second.length()));

    BigInteger ac = karat(a, c);
    BigInteger bd = karat(b, d);

    BigInteger abcd = karat(a.add(b), c.add(d));

    BigInteger z = abcd.subtract(ac.add(bd));

    int m = String.valueOf(number1).length();

    BigInteger len = new BigInteger(String.valueOf(Math.pow(10, m)));

    return (ac.multiply(len)).add(z.multiply(len.divide(BigInteger.valueOf(2)))).add(bd);
}


public static void main(String[] args){

    BigInteger result = new BigInteger("0");

    BigInteger x = new BigInteger("1234");

    BigInteger y = new BigInteger("5678");

    result = karat(x, y);

    System.out.println(result);
       }
   }

Please correct the code. I have not checked whether two numbers are equal or not and length is odd or even.

Thank you.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sid
  • 29
  • 7
  • That's not the whole error, just the end of the stack trace. Please post the entire exception message. – meowgoesthedog Aug 31 '17 at 12:41
  • Looks to me like an endless recursion, hence the repeats of exactly the same message. The first error message is probably the important one, not the repetitive follow ons. – rossum Aug 31 '17 at 12:43
  • Ah sorry. But why didn't you first try to put in some **print** statements to understand what your code is doing? The time it took you to write this question ... might have uncovered the problem already?! – GhostCat Aug 31 '17 at 12:45
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the "edit" link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 31 '17 at 12:46
  • 1
    Then: an exception stack trace is **text**. So please add stack traces as well formatted *text* to your question - instead of dumping screen shots on us for no good reason. – GhostCat Aug 31 '17 at 12:46
  • Sorry for my unclear question. When I run the program from command line I am able to see this message only. Please feel free to delete this question because I am trying from 2 weeks to multiply numbers using karatsuba multiplication but still not successful. If you can please provide me with proper code. I don't want to use code that is already on net. – Sid Aug 31 '17 at 12:51
  • @Sid you need to show us the *top* of this error message, i.e. what *kind* of exception has been raised (`NullReference`? `Overflow`? `InvalidOperation`? `OutOfBounds`? `OutOfMemory`?) – meowgoesthedog Aug 31 '17 at 12:58
  • 2
    `BigInteger a = new BigInteger(first.substring(0, first.length() / 2));` – Joop Eggen Aug 31 '17 at 13:15
  • Do you expect/allow digits 8 and/or 9 to occur? – greybeard Aug 31 '17 at 13:15
  • 2
    Also 123456 * 7890 should maintain the same power of 10 when splitting: 1234*100+56, 78*100+90. – Joop Eggen Aug 31 '17 at 13:22
  • Next time I'll take care. – Sid Aug 31 '17 at 16:34
  • You simply divide your string too far: a single character string cannot be divided anymore, so at least one of the parts is an empty string. That is what the error message is telling you: It can't make a BigInteger out of an empty string. – Rudy Velthuis Sep 04 '17 at 09:03
  • Can you please provide mine correct code. – Sid Sep 04 '17 at 17:15

0 Answers0