daily pastebin goal
62%
SHARE
TWEET

Russian Peasant Algorithm vs Slow Multiplication

CryptoJones Jul 31st, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static int RussianPeasantAlgorithm(int a, int b){
  2.             int x = a;
  3.             int y = b;
  4.             int z = 0;
  5.             while (x > 0){
  6.                     if (x % 2 == 1){ z = z + y;}
  7.                 y = y << 1;
  8.                 x = x >> 1;
  9.             }
  10.             return z;
  11.         }
  12.  
  13.         public static int NormalMultiplication(int a, int b){
  14.             int x = a;
  15.             int y = b;
  16.             int z = 0;
  17.             while (x > 0){
  18.                 z = z + y;
  19.                 x = x - 1;
  20.             }
  21.             return z;
  22.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top