Advertisement
damoncard

RussianMultiplication

Nov 21st, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.26 KB | None | 0 0
  1. package tools
  2.  
  3. import "strconv"
  4.  
  5. /*  Russian Multiplication represeting in demical value calculate by
  6.     Create one variable to keep carry out
  7.     First value divide by 2 while..
  8.     Second value multiply by 2
  9.     If first is odd *Except one* : carry out += before-multiply second value
  10.     Example:    6 * 6 : no carry out
  11.             3 * 12 : carry out + 12
  12.             1 * 24 : no carry out
  13.     Return 24 + 12 = 36
  14. */
  15. func DecimalMul(first, second int) int {
  16.     // Create variable to keep carry out
  17.     var result int = 0
  18.     // First always reduce to one
  19.     for first != 1 {
  20.         if first%2 != 0 {
  21.             // Get carry out
  22.             result += second
  23.         }
  24.         // Divide and Multiply
  25.         first /= 2
  26.         second *= 2
  27.     }
  28.     // Current Second value + Carry out
  29.     return second + result
  30. }
  31.  
  32. /* Russian Multiplication represeting in binary value calculate by
  33.     *Input : 2 Strings
  34.     Create one variable to keep carry out
  35.     First value delete last elemnt out while..
  36.     Second value add one 0 in
  37.     If last element of first value is 1 *Except when length is 1* : carry out += before-multiply second value
  38.     *In my case: carry out of mine, I turn it to decimal, plus them, and turn it back to binary
  39.     Example :   110 * 110 : no carry out
  40.             11 * 1100 : carry out = 1100
  41.             1 * 11000 : no carry out
  42.     Return 11000 + 1100 = 100100
  43. */
  44. func BinaryMul(first, second string) string {
  45.     // Create variable to keep carry out
  46.     var result string = "0"
  47.     // When length of first = 1 means first = "1"
  48.     for len(first) > 1 {
  49.         // Check if last element of first is 1
  50.         if first[len(first)-1:] == "1" {
  51.             // Add carry out by sumBinary Method
  52.             result = sumBinary(result, second)
  53.         }
  54.         // Delete last element out
  55.         first = first[:len(first)-1]
  56.         // Add 0 in
  57.         second += "0"
  58.     }
  59.     // Add carry out and return
  60.     return sumBinary(result, second)
  61. }
  62.  
  63. /*  Add carry out to current second value
  64.     Turn first and second value to decimal
  65.     Plus them
  66.     Turn them back to binary and return
  67.     Example :   "11" + "11"
  68.             Turn to decimal
  69.             3 + 3 = 6
  70.             Turn back to binary
  71.             110
  72.     Return "110"
  73. */
  74. func sumBinary(first, second string) string {
  75.     // ParseInt(string, Current bit, Format of int) -> value in decimal, err
  76.     f, _ := strconv.ParseInt(first, 2, 64)
  77.     s, _ := strconv.ParseInt(second, 2, 64)
  78.     // FormatInt(value, base) -> string
  79.     return strconv.FormatInt(f+s, 2)
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement