in reply to The Big Test

Or here's a good one. "How do I tell if a number is even or odd?" I am not going to touch that one.

I'm surprised we havent seen at least 4 of these by now... it is, after all, a basic computer science question

Replies are listed 'Best First'.
RE: RE: The Big Test
by japhy (Canon) on Oct 17, 2000 at 19:06 UTC
    It's even simpler than computer science. It's something a human should know how to do! How do you figure out of a number is even? It can be divided by two evenly! Then, all the person needs to know is that mathemetics (and Perl) offer the modulus operation (Perl uses the % character, like C and C++) to return the remainder when a number is divided by another. With these two pieces of information:
    • X is even if it is evenly divisible by 2
    • X % Y returns the remainder when X is divided by Y
    any human that can find patterns should be able to tell how to determine whether a number is even or odd.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
      Well if Perl were to do it like this human does, the test would be:
      if ($number =~ /\D/) { print "$number is not an integer\n"; } elsif ($number =~ /[02468]$/) { print "$number is even\n"; else { print "$number is odd\n"; }
      Honestly, that is *exactly* how I do it! :-)
        Actually, I prefer to test a number for even-ness using the same concept: Check the last digit, er... bit:
        print "Odd" if $_ & 1; print "Even" if not $_ & 1;
        Yes, a human tells if a number is even by checking the last digit. But why does that work? Because we all (I think) learned some really nifty tricks when we were little:
        • A number is divisible by 2 if it ends in an even digit (0, 2, 4, 6, or 8)
        • A number is divisible by 3 if the sum of its digits is 3, 6, or 9 (and you keep summing the digits until you get to a one-digit number)
        • A number is divisible by 4 if its last two digits are divisible by 4 (recursive, yes, but it takes a little extra brain power to determine if a two-digit number is divisible by 4)
        • A number is divisible by 5 if its last digit is a 0 or a 5
        • A number is divisible by 6 if it's divisible by 2 and 3
        • and so on...
        So there's a connection between the last digit of a number and its remainder modulo 2. That doesn't discount the fact that even-ness is determined via the remainder of the number modulo 2, it's just that the last digit modulo 2 is a shortcut.

        $_="goto+F.print+chop;\n=yhpaj";F1:eval

        One I used to see regularly when in my second Basic class (*sigh*) was (in perl idiom)  if ($num/2 == int($num/2)) {print "$num even!\n":}

        That still gives me the willies, though it is techically correct(ish) and is more generally applicable than terminal digit inspection =) Most people these days never got taught what the mod function really does. Basic math logic skills are passed off till college.

        One thing I remember from Basic was that true was -1 and false was 0, and that all logical ops returned one of those two values. We used to do this (sorta) in graphics programs to get smoothly, randomly, changing colors and positions:

        my @color= (127,127,127); #256x256x256 color palette my @location= (50,50); #200x200 screen for(;;) { $color[0] += (int(rand*3)==1) - (int(rand*3)==1); $color[1] += (int(rand*2)==1) - (int(rand*4)==1); $color[2] += (int(rand*4)==1) - (int(rand*2)==1); $location[0] += (int(rand*5)) - 2; $location[1] += (int(rand*5)) - 2; @color = map {$_ % 256} @color; @location = map {$_ % 200} @location; DrawPixel(@location,@color); }

        We did megacomplex things with math logic because it was blazingly fast vs. if (){}; =) Seems like a forgotten era now that I do web stuff.

        --
        $you = new YOU;
        honk() if $you->love(perl)

      It's something a human should know how to do! How do you figure out of a number is even? It can be divided by two evenly!
      japhy, you highly overestimate the knowledge of the populus. As a counter example, notice the popularity of WWF, including the number of people that swear it's real.

      -- Randal L. Schwartz, Perl hacker

        Right. There are people who do not know how to find patterns, and they should not be programming.

        $_="goto+F.print+chop;\n=yhpaj";F1:eval