Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7.  
  8. func main() {
  9.  
  10. num1 := 2000000000
  11. num2 := 2000000000
  12.  
  13. start := time.Now()
  14. a := naive(num1, num2)
  15. elapsed := time.Since(start)
  16. fmt.Printf("Product: %d, Elapsed Time: %d \n", a, elapsed)
  17.  
  18. start = time.Now()
  19. a = russian(num1, num2)
  20. elapsed = time.Since(start)
  21. fmt.Printf("Product: %d, Elapsed Time: %d \n", a, elapsed)
  22.  
  23. start = time.Now()
  24. a = recRussian(num1, num2)
  25. elapsed = time.Since(start)
  26. fmt.Printf("Product: %d, Elapsed Time: %d \n", a, elapsed)
  27. }
  28.  
  29. func naive(a, b int) int {
  30. x := a
  31. y := b
  32. z := 0
  33. for x > 0 {
  34. x = x - 1
  35. z = z + y
  36. }
  37. return z
  38. }
  39.  
  40. func russian(a, b int) int {
  41. x := a
  42. y := b
  43. z := 0
  44.  
  45. for x > 0 {
  46. if x%2 == 1 {
  47. z = z + y
  48. x = (x - 1) / 2
  49. } else {
  50. x = x / 2
  51. }
  52. y = y * 2
  53. }
  54. return z
  55. }
  56.  
  57. func recRussian(a, b int) int {
  58. if a == 0 {
  59. return 0
  60. }
  61. if a%2 == 0 {
  62. return 2 * recRussian(a/2, b)
  63. } else {
  64. return b + 2*recRussian((a-1)/2, b)
  65. }
  66. }
Add Comment
Please, Sign In to add comment