Even or odd

From Rosetta Code
Revision as of 02:08, 15 November 2011 by rosettacode>Glennj (add Ruby)
Even or odd is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

bc

There are no bitwise operations, so this solution compares a remainder with zero. Calculation of i % 2 only works when scale = 0.

<lang bc>i = -3

/* Assumes that i is an integer. */ scale = 0 if (i % 2 == 0) "i is even " if (i % 2) "i is odd "</lang>

C

Test by bitwise and'ing 1, works for any builtin integer type as long as it's 2's compliment (it's always so nowadays): <lang c>if (x & 1) {

   /* x is odd */

} else {

   /* or not */

}</lang> If using long integer type from GMP (mpz_t), there are provided macros: <lang c>mpz_t x; ... if (mpz_even_p(x)) { /* x is even */ } if (mpz_odd_p(x)) { /* x is odd */ }</lang> The macros evaluate x more than once, so it should not be something with side effects.

Common Lisp

Standard predicates: <lang lisp>(if (evenp some-var) (do-even-stuff)) (if (oddp some-other-var) (do-odd-stuff))</lang>

Factor

The math vocabulary provides even? and odd? predicates. This example runs at the listener, which already uses the math vocabulary.

( scratchpad ) 20 even? .
t
( scratchpad ) 35 even? .
f
( scratchpad ) 20 odd? .
f
( scratchpad ) 35 odd? .
t

Haskell

even and odd functions are already included in the standard Prelude.

<lang haskell>Prelude> even 5 False Prelude> even 42 True Prelude> odd 5 True Prelude> odd 42 False</lang>

J

Modulo:

<lang j> 2 | 2 3 5 7 0 1 1 1

  2|2 3 5 7 + (2^89x)-1

1 0 0 0</lang>

Remainder:

<lang j> (= <.&.-:) 2 3 5 7 1 0 0 0

  (= <.&.-:) 2 3 5 7+(2^89x)-1

0 1 1 1</lang>

Last bit in bit representation: <lang j> {:"1@#: 2 3 5 7 0 1 1 1

  {:"1@#: 2 3 5 7+(2^89x)-1

1 0 0 0</lang>

Bitwise and:

<lang j> 1 (17 b.) 2 3 5 7 0 1 1 1</lang>

Note: as a general rule, the simplest expressions in J should be preferred over more complex approaches.

Java

Bitwise and: <lang java>public static boolean isEven(int i){

   return (i & 1) == 0;

}</lang> Modulo: <lang java>public static boolean isEven(int i){

   return (i % 2) == 0;

}</lang> Arbitrary precision bitwise: <lang java>public static boolean isEven(BigInteger i){

   return i.and(BigInteger.ONE).equals(BigInteger.ZERO);

}</lang> Arbitrary precision bit test (even works for negative numbers because of the way BigInteger represents the bits of numbers): <lang java>public static boolean isEven(BigInteger i){

   return !i.testBit(0);

}</lang> Arbitrary precision modulo: <lang java>public static boolean isEven(BigInteger i){

   return i.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO);

}</lang>

OCaml

Modulo:

<lang ocaml>let is_even d =

 (d mod 2) = 0

let is_odd d =

 (d mod 2) <> 0</lang>

Bitwise and:

<lang ocaml>let is_even d =

 (d land 1) = 0

let is_odd d =

 (d land 1) <> 0</lang>

Pike

<lang Pike>> int i = 73; > (i&1); Result: 1 > i%2; Result: 1</lang>

Python

<lang python>>>> def is_odd(i): return bool(i & 1)

>>> def is_even(i): return not is_odd(i)

>>> [(j, is_odd(j)) for j in range(10)] [(0, False), (1, True), (2, False), (3, True), (4, False), (5, True), (6, False), (7, True), (8, False), (9, True)] >>> [(j, is_even(j)) for j in range(10)] [(0, True), (1, False), (2, True), (3, False), (4, True), (5, False), (6, True), (7, False), (8, True), (9, False)] >>> </lang>

Ruby

<lang ruby>print "evens: " p -5.upto(5).select {|n| n.even?} print "odds: " p -5.upto(5).select {|n| n.odd?}</lang>

outputs

evens: [-4, -2, 0, 2, 4]
odds: [-5, -3, -1, 1, 3, 5]

Tcl

<lang tcl>package require Tcl 8.5

  1. Bitwise test is the most efficient

proc tcl::mathfunc::isOdd x { expr {$x & 1} } proc tcl::mathfunc::isEven x { expr {!($x & 1)} }

puts " # O E" puts 24:[expr isOdd(24)],[expr isEven(24)] puts 49:[expr isOdd(49)],[expr isEven(49)]</lang> Output:

 # O E
24:0,1
49:1,0