Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

    Default 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. #4

    Default Re: Determining Odd and Even Numbers


    Thanks everyone!


    Jimstone77@aol.com Guest

  5. #5

    Default 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. #6

    Default 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. #7

    Default 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. #8

    Default RE: Determining Odd and Even Numbers


    Hi Jenda,

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

    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
    For additional commands, 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

Similar Threads

  1. Determining Even or Odd
    By brianism in forum Coldfusion - Advanced Techniques
    Replies: 5
    Last Post: October 3rd, 08:23 PM
  2. Determining an age
    By Joshua in forum PHP Development
    Replies: 8
    Last Post: March 7th, 02:00 PM
  3. Determining OS in director
    By ceeacs in forum Macromedia Director Basics
    Replies: 4
    Last Post: March 2nd, 12:38 PM
  4. Numbers not acting like numbers
    By D'Anne in forum PERL Miscellaneous
    Replies: 3
    Last Post: October 2nd, 09:18 AM
  5. Finding Numbers in between two numbers
    By Balaji in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 30th, 07:36 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139