Task

Test whether an integer is even or odd.

There is more than one way to solve this task:

 Use the even and odd predicates, if the language provides them.
 Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even, or equals 1 iff i is odd.
 Divide i by 2. The remainder equals 0 iff i is even. The remainder equals +1 or -1 iff i is odd.
 Use modular congruences:
 i ≡ 0 (mod 2) iff i is even.
 i ≡ 1 (mod 2) iff i is odd.


 
; 1. Check the least significant bit.
(define (even? i)
   (if (eq? (band i 1) 0) #t #f))
(define (odd? i)
   (if (eq? (band i 1) 1) #t #f))
 
(print (if (even? 12345678987654321) "even" "odd")) ; ==> odd
(print (if (odd? 12345678987654321) "odd" "even"))  ; ==> odd
(print (if (even? 1234567898765432) "even" "odd"))  ; ==> even
(print (if (odd? 1234567898765432) "odd" "even"))   ; ==> even
 
; 2. Divide i by 2. The remainder equals 0 iff i is even.
(define (even? i)
   (if (eq? (remainder i 2) 0) #t #f))
(define (odd? i)
   (if (eq? (remainder i 2) 1) #t #f))
 
(print (if (even? 12345678987654321) "even" "odd")) ; ==> odd
(print (if (odd? 12345678987654321) "odd" "even"))  ; ==> odd
(print (if (even? 1234567898765432) "even" "odd"))  ; ==> even
(print (if (odd? 1234567898765432) "odd" "even"))   ; ==> even
 
; 3. Use modular congruences. Same as 2.
(define (even? i)
   (if (eq? (mod i 2) 0) #t #f))
(define (odd? i)
   (if (eq? (mod i 2) 1) #t #f))
 
(print (if (even? 12345678987654321) "even" "odd")) ; ==> odd
(print (if (odd? 12345678987654321) "odd" "even"))  ; ==> odd
(print (if (even? 1234567898765432) "even" "odd"))  ; ==> even
(print (if (odd? 1234567898765432) "odd" "even"))   ; ==> even
 
 