Advertisement
lineoff

karatsuba

Dec 24th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. static long karatsubaMethod(long x,long y)
  2.         {
  3.             long a, b, c, d,ac,bd;
  4.             int len = (int) Math.Floor(Math.Log10(x)+1);
  5.             len /= 2;
  6.             if (len <= 1)
  7.                 return x * y;
  8.  
  9.             a = x / (long)Math.Floor(Math.Pow(10, len));
  10.             b = x % (long)Math.Floor(Math.Pow(10, len));
  11.             c = y / (long)Math.Floor(Math.Pow(10, len));
  12.             d = y % (long)Math.Floor(Math.Pow(10, len));
  13.             ac = karatsubaMethod(a, c);
  14.             bd = karatsubaMethod(b, d);
  15.             return ac * (long)Math.Floor(Math.Pow(10, 2*len))
  16.                 +(ac+bd-karatsubaMethod(a-b,c-d))*(long)Math.Floor(Math.Pow(10, len))
  17.                 + bd;
  18.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement