Created
January 26, 2022 19:46
-
-
Save ozansulukpinar/b697eb3a6009032c9903b34a688d2e7a to your computer and use it in GitHub Desktop.
Find multiplication of two numbers rapidly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program { | |
public static void Main() { | |
int result = RussianPeasantMultiplication(121, 398); | |
Console.WriteLine(result); | |
} | |
private static int RussianPeasantMultiplication(int number1, int number2) { | |
int multiple = 0; | |
multiple = AddToList(number1, number2, multiple); | |
while (number1 != 1) { | |
number1 /= 2; | |
number2 *= 2; | |
multiple = AddToList(number1, number2, multiple); | |
} | |
return multiple; | |
} | |
private static int AddToList(int number1, int number2, int multiple) { | |
if (number1 % 2 != 0) | |
multiple += number2; | |
return multiple; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment