Multiplizieren von 127 & 128 mittels der Methode von Karazuba

https://www.stacklounge.de/3942/multiplizieren-von-127-128-mittels-der-methode-von-karazuba

by Kajus

HTML

<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>

JavaScript

var x = 127;
var y = 128;
var result = karatsubaMulti(127,128);
console.log(x+' · '+y+' = '+result);


/**
 * Karatsuba Multiplication
 * @param  {Number} x - first number
 * @param  {Number} y - second number
 * @return {Number} Multiply of x and y
 * Credits: https://gist.github.com/haocong/c2d9b2169d28eb15a94d
 */
 
function karatsubaMulti(x, y) 
{

  let n = Math.min(('' + x).length, ('' + y).length);

  if(n == 1)
    return x * y;

  let tenpowhalfn = Math.pow(10, parseInt(n / 2));
  let tenpown = Math.pow(10, 2 * parseInt(n / 2));

  let a = parseInt(x / tenpowhalfn);
  let b = x % tenpowhalfn;
  let c = parseInt(y / tenpowhalfn);
  let d = y % tenpowhalfn;

  let caller = arguments.callee;

  return tenpown * caller(a, c) + tenpowhalfn * (caller(a, d) + caller(b, c)) + caller(b, d);
}