Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

(OT?) n(n-1)/2

by mandog (Curate)
on Mar 03, 2002 at 04:39 UTC ( #148938=perlquestion: print w/replies, xml ) Need Help??
mandog has asked for the wisdom of the Perl Monks concerning the following question:

I'm teaching programming to an unschooled 15 year old kid with a 3 hour attention span.

We're up to: "Add all the numbers between 1 and 1000 (use a loop)"

jeffa's node on Guass's methodis quite interesting. Does anyone have a series of questions to led the kid about half of the way to the formula?

email: mandog

Replies are listed 'Best First'.
Re: (OT?) n(n-1)/2
by wog (Curate) on Mar 03, 2002 at 04:46 UTC
    How about:
    • What is 1 + 1000?
    • What is 2 + 999?
    • What is 3 + 998?
    • How many pairs of numbers adding up to 1001 are there from 1 to 1000? Are there any leftover numbers?
    • What, then, is the sum of all the numbers from 1 to 1000?
    • What happens if we replace 1000 with 100, 999 with 99, etc.?
    • What happens if we replace 1000 with 1001, 999 with 1000, etc.?
Re: (OT?) n(n-1)/2
by tilly (Archbishop) on Mar 03, 2002 at 05:03 UTC
    Try to get the kid in question to come up with the following diagram and understand it:
    X o o o o o o x X o o o o o x x X o o o o x x x X o o o x x x x X o o x x x x x X o x x x x x x X
    The number of x's (either capitalization is your answer. The number of points in the square is n*n. The square is evenly divided between x's and o's with the exception of the n X's on the diagonal. Therefore the number of x's is (n*n-n)/2 (half the number off of the diagonal) plus n (the number on the diagonal). That factors as follows:
    (n*n - n)/2 + n = n*n/2 - n/2 + n = n*n/2 + n/2 = n*(n+1)/2
    IIRC this explanation appears in Polya's excellent book, "How to solve it!" (I wouldn't bet too heavily on that particular memory though.)

    Incidentally if the kid shows a mathematical inclination (lack of formal schooling notwithstanding), then I can highly recommend picking up The Mathematical Experience by Davis and Hersh. It is a hodgepodge of different topics at different levels. While they are mostly written at a level that can be understood by laypeople, by and large the topics are genuine research topics of interests to professional mathematicians. And what is communicated is truly, as best as is possible without years of training, the range of human experience within mathematics.

Re: (OT?) n(n-1)/2
by Zaxo (Archbishop) on Mar 03, 2002 at 04:55 UTC
    O - - - - ...
    O O - - - ...
    O O O - - ...
    O O O O - ...
    O O O O O ...
    Visual is good :)

    After Compline,

Re: (OT?) n(n-1)/2
by Juerd (Abbot) on Mar 03, 2002 at 10:29 UTC

    If you're just doing this for Perl education, don't bother with useless mathematical optimizations. In real life, you can often sacrifice a bit of speed for ease of coding.

    my $a = 0; $a += $_ for 1..1000; print "Answer: $a\n";

    If this isn't about Perl programming, or about programming at all, you're posting in the wrong place :)

    P.S. With Perl Golf, of course mathematic principles are a lot more important: $foo && ($foo % 9 || 9) is shorter than substr($foo, 0, 1) + substr($foo, 1, 1) (and do it again if the answer's >9)

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq

      Observe magic for the perl puzzle of the day, based on the math puzzle:
      #!/usr/bin/perl -w use strict; # my $a = 0; ## notice this is deleted! $a += $_ for 1..1000; print "Answer: $a\n";
      Points to ponder:
      1. Why does this even compile? It's use strict!
      2. Why no warnings? We're using undef in math!
      3. How come it still gets the same result? We didn't initialize the variable!
      4. Bonus question: can you tell why we removed all uses of $a as a casual variable from the third edition of Learning Perl?
      Hmm. {grin}

      -- Randal L. Schwartz, Perl hacker

        The questions are far too easy :)
        1. $a is a global that can be used without declaring it because sorting would otherwise be a pain in the but.
        2. += is a "don't care" operator. $a = $a + $_ would have generated a "use of uninitialized value in ..."-warning.
        3. See 1 and 2
        4. See 1 - you probably tried to avoid confusion, which is a good thing. Perhaps I should have done so to, but I could really not care less for a three-liner.

        ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
        Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
        -- vs lbh hfrq OFQ pnrfne ;)
            - Whreq

        1., 2. see perlman strict, chapter 'strict vars'
        as in a previuos thread already mentioned :-)
        $a and $b arre exempted from the strict vars pragma due to their special role in the sort function
        3. undef equals to '0'
        4. dunno, didn't read it yet :-)

        Have a nice day
        All decision is left to your taste

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://148938]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-07-23 05:59 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.