You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

math.go 867B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package common
  2. // Abs returns the absolute value of x.
  3. func Abs(x int64) int64 {
  4. y := x >> 63
  5. return (x ^ y) - y
  6. }
  7. // Max returns the highest of the two given ints.
  8. func Max(x, y int) int {
  9. if x >= y {
  10. return x
  11. }
  12. return y
  13. }
  14. // Min returns the lowest of the two given ints.
  15. func Min(x, y int) int {
  16. if x <= y {
  17. return x
  18. }
  19. return y
  20. }
  21. // GCD finds the greatest common divisor (GCD) via Euclidean algorithm
  22. // Source: https://siongui.github.io/2017/06/03/go-find-lcm-by-gcd
  23. func GCD(a, b int64) int64 {
  24. for b != 0 {
  25. t := b
  26. b = a % b
  27. a = t
  28. }
  29. return a
  30. }
  31. // LCM finds the Least Common Multiple (LCM) via GCD
  32. // Source: https://siongui.github.io/2017/06/03/go-find-lcm-by-gcd
  33. func LCM(a, b int64, integers ...int64) int64 {
  34. result := a * b / GCD(a, b)
  35. for i := 0; i < len(integers); i++ {
  36. result = LCM(result, integers[i])
  37. }
  38. return result
  39. }