# Determining Odd and Even Numbers - PERL Beginners

Does anyone know a simple way to determine if a number is odd or even? For example 220 would come out even while 221 would come out as an odd number....

1. ## Determining Odd and Even Numbers

Does anyone know a simple way to determine if a number is odd or even? For
example 220 would come out even while 221 would come out as an odd number.

Jimstone77@aol.com Guest

2. ## Re: Determining Odd and Even Numbers

On 3 Aug 2004, at 13:26, com wrote:

Use the modulus operator. If \$foo % 2 has remainder 1, then it is odd,
if it has remainder 0, then it is even.

--
David Dorward
<http://dorward.me.uk/>
<http://blog.dorward.me.uk/>

David Guest

3. ## Re: Determining Odd and Even Numbers

com said:

perl -e '(\$number % 2)?print "even":print "odd"; print "\n";'

% is the modulus operator, and returns the remainder from a division of
its left operand by its right.

221 % 2 = 1 which is true if tested inside an if statement or whatever.
220 % 2 = 0 which is false in an if statement.

Hope that helps.
--
Kind regards,
Hal Ashburner
Harald Guest

4. ## Re: Determining Odd and Even Numbers

Thanks everyone!

Jimstone77@aol.com Guest

5. ## RE: Determining Odd and Even Numbers

Well,

All the above said answers are perfectly fine, but just to make it more
efficient what we can do is, we can avoid the if statement or ?:
operator.

This is because whenever we go for a decision statement, it takes some
extra processing time.

A solution to avoid using if or ?: is as follows:

#!/usr/bin/perl
use strict;

my string = ("Even", "Odd");
print \$string[\$ARGV[0]%2];

Well it might look like the savings in time is less, but if you consider
that this code is inside a loop which will run for several iterations,
you are sacing lot of time.... Hence the program becomes more efficient.

Also if suppose you need to invoke a function depending upon whether a
number is Odd or Even, you may simply put the reference of the functions
inside the array (which in our example is having "Odd", Even"). So you
may simply invoke the function by saying &\$array[\$ARGV[0]%2]....

Well it also has lots more advantages, you may just kickstart your
creative horse to run, and use this logic to the maximum extent.....

Hope it helps ;)

With Best regards,
R. Kamal Raj Guptha.

-----Original Message-----
From: com [mailto:com]
Sent: Tuesday, August 03, 2004 6:24 PM
To: org
Subject: Re: Determining Odd and Even Numbers

Thanks everyone!

Confidentiality Notice

The information contained in this electronic message and any attachments tothis message are intended
for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
you are not the intended recipient, please notify the sender at Wipro com immediately
and destroy all copies of this message and any attachments.
Kamal Guest

6. ## RE: Determining Odd and Even Numbers

From: <com>

Whenever we index an array it takes some extra processing time as
well.

Before you say something like this you should test it:

use Benchmark;

sub withIf {
my \$number_is = '';
for (1..100000) {
if (\$_ % 2) {
\$number_is = 'Odd';
} else {
\$number_is = 'Even';
}
}
}

sub withQm {
my \$number_is = '';
for (1..100000) {
\$number_is = (\$_ % 2) ? 'Odd' : 'Even';
}
}

sub withIfAnd {
my \$number_is = '';
for (1..100000) {
if (\$_ & 1) {
\$number_is = 'Odd';
} else {
\$number_is = 'Even';
}
}
}

sub withQmAnd {
my \$number_is = '';
for (1..100000) {
\$number_is = (\$_ & 1) ? 'Odd' : 'Even';
}
}

my what = qw(Even Odd);
sub withAry {
my \$number_is = '';
for (1..100000) {
\$number_is = \$what[\$_ % 2];
}
}

sub withAryAnd {
my \$number_is = '';
for (1..100000) {
\$number_is = \$what[\$_ & 1];
}
}

timethese 100, {
withIf => \&withIf,
withQm => \&withQm,
withIfAnd => \&withIfAnd,
withQmAnd => \&withQmAnd,
withAry => \&withAry,
withAryAnd => \&withAryAnd,
}

Benchmark: timing 100 iterations of withAry, withAryAnd, withIf,
withIfAnd, withQm, withQmAnd...
withAry: 6 wallclock secs ( 5.67 usr + 0.00 sys = 5.67 CPU)
17.63/s (n=100)
withAryAnd: 5 wallclock secs ( 5.20 usr + 0.02 sys = 5.22 CPU)
19.16/s (n=100)
withIf: 6 wallclock secs ( 6.09 usr + 0.01 sys = 6.11 CPU)
16.37/s (n=100)
withIfAnd: 6 wallclock secs ( 5.77 usr + 0.00 sys = 5.77 CPU)
17.35/s (n=100)
withQm: 5 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU)
19.81/s (n=100)
withQmAnd: 5 wallclock secs ( 4.73 usr + 0.00 sys = 4.73 CPU)
21.12/s (n=100)

(Using Perl v5.8.0 ActiveState build 805 on Win2k server)

So it seems the ?: is actually fastest ;-)

Jenda
===== cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Jenda Guest

7. ## Re: Determining Odd and Even Numbers

On Aug 3, David Dorward said:

>
>Use the modulus operator. If \$foo % 2 has remainder 1, then it is odd,
>if it has remainder 0, then it is even.[/ref]

While I find this a silly question, I'll chime in with the "computer
scientist"'s response.

Using the bitwise & operator is faster than the modulus operator.

my \$odd = (\$num & 1);

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Jeff Guest

8. ## RE: Determining Odd and Even Numbers

Hi Jenda,

That was amazing..... It was a good learning for me, and I never knew

Sorry for that...

My reasoning was absolutely logical and nothing systematical....

Thanks for the great reasoning which was real real systematical.

But the same script in my environment gives me the following result:

Benchmark: timing 100 iterations of withAry, withAryAnd, withIf,
withIfAnd, withQm, withQmAnd...
withAry: 18 wallclock secs (17.60 usr + 0.01 sys = 17.61 CPU)
5.68/s (n=100)
withAryAnd: 17 wallclock secs (17.37 usr + 0.01 sys = 17.38 CPU)
5.75/s (n=100)
withIf: 20 wallclock secs (19.34 usr + 0.00 sys = 19.34 CPU)
5.17/s (n=100)
withIfAnd: 19 wallclock secs (19.07 usr + 0.00 sys = 19.07 CPU)
5.24/s (n=100)
withQm: 16 wallclock secs (16.42 usr + 0.01 sys = 16.43 CPU)
6.09/s (n=100)
withQmAnd: 17 wallclock secs (16.10 usr + 0.00 sys = 16.10 CPU)
6.21/s (n=100)

Well..... This is HP-UX 9000/785.........

Now I really wonder how consistent the Benchmark gives the result
properly..... bcoz different runs gave different results...

Fine anyhow I should admit that using array is slower than (or as fast
as) using ?:, but it is faster than using "if...else" both in your
environment and in mine (even for several runs).

Anyways thanks for letting me know a good Perl Module.... Benchmark.pm

With Best regards,
R. Kamal Raj Guptha.

-----Original Message-----
From: Jenda Krynicky [mailto:cz]
Sent: Tuesday, August 03, 2004 8:00 PM
To: R. Kamal Raj Guptha (WT01 - TELECOM & INTER-NETWORKING SOLUTIONS);
org
Subject: RE: Determining Odd and Even Numbers

From: <com>

Whenever we index an array it takes some extra processing time as
well.

Before you say something like this you should test it:

use Benchmark;

sub withIf {
my \$number_is = '';
for (1..100000) {
if (\$_ % 2) {
\$number_is = 'Odd';
} else {
\$number_is = 'Even';
}
}
}

sub withQm {
my \$number_is = '';
for (1..100000) {
\$number_is = (\$_ % 2) ? 'Odd' : 'Even';
}
}

sub withIfAnd {
my \$number_is = '';
for (1..100000) {
if (\$_ & 1) {
\$number_is = 'Odd';
} else {
\$number_is = 'Even';
}
}
}

sub withQmAnd {
my \$number_is = '';
for (1..100000) {
\$number_is = (\$_ & 1) ? 'Odd' : 'Even';
}
}

my what = qw(Even Odd);
sub withAry {
my \$number_is = '';
for (1..100000) {
\$number_is = \$what[\$_ % 2];
}
}

sub withAryAnd {
my \$number_is = '';
for (1..100000) {
\$number_is = \$what[\$_ & 1];
}
}

timethese 100, {
withIf => \&withIf,
withQm => \&withQm,
withIfAnd => \&withIfAnd,
withQmAnd => \&withQmAnd,
withAry => \&withAry,
withAryAnd => \&withAryAnd,
}

Benchmark: timing 100 iterations of withAry, withAryAnd, withIf,
withIfAnd, withQm, withQmAnd...
withAry: 6 wallclock secs ( 5.67 usr + 0.00 sys = 5.67 CPU)
17.63/s (n=100)
withAryAnd: 5 wallclock secs ( 5.20 usr + 0.02 sys = 5.22 CPU)
19.16/s (n=100)
withIf: 6 wallclock secs ( 6.09 usr + 0.01 sys = 6.11 CPU)
16.37/s (n=100)
withIfAnd: 6 wallclock secs ( 5.77 usr + 0.00 sys = 5.77 CPU)
17.35/s (n=100)
withQm: 5 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU)
19.81/s (n=100)
withQmAnd: 5 wallclock secs ( 4.73 usr + 0.00 sys = 4.73 CPU)
21.12/s (n=100)

(Using Perl v5.8.0 ActiveState build 805 on Win2k server)

So it seems the ?: is actually fastest ;-)

Jenda
===== cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

--
To unsubscribe, e-mail: org
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Confidentiality Notice

The information contained in this electronic message and any attachments tothis message are intended
for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
you are not the intended recipient, please notify the sender at Wipro com immediately
and destroy all copies of this message and any attachments.
Kamal Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•