mut_recurs.go 528 B

123456789101112131415161718192021222324252627282930313233
  1. // mut_recurs.go
  2. package main
  3. import (
  4. "fmt"
  5. )
  6. func main() {
  7. fmt.Printf("%d is even: is %t\n", 16, even(16)) // 16 is even: is true
  8. fmt.Printf("%d is odd: is %t\n", 17, odd(17)) // 17 is odd: is true
  9. fmt.Printf("%d is odd: is %t\n", 18, odd(18)) // 18 is odd: is false
  10. }
  11. func even(nr int) bool {
  12. if nr == 0 {
  13. return true
  14. }
  15. return odd(RevSign(nr) - 1)
  16. }
  17. func odd(nr int) bool {
  18. if nr == 0 {
  19. return false
  20. }
  21. return even(RevSign(nr) - 1)
  22. }
  23. func RevSign(nr int) int {
  24. if nr < 0 {
  25. return -nr
  26. }
  27. return nr
  28. }