Even or odd: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Python)
m (→‎{{header|Java}}: Note about BigInteger bit testing)
Line 47: Line 47:
return (i.and(BigInteger.ONE)).equals(BigInteger.ZERO);
return (i.and(BigInteger.ONE)).equals(BigInteger.ZERO);
}</lang>
}</lang>
Arbitrary precision bit test:
Arbitrary precision bit test (even works for negative numbers because of the way <code>BigInteger</code> represents the bits of numbers):
<lang java>public static boolean isEven(BigInteger i){
<lang java>public static boolean isEven(BigInteger i){
return !(i.testBit(0));
return !(i.testBit(0));

Revision as of 03:53, 14 November 2011

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 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>

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

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>

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>