Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Pascal triange...

by Abigail-II (Bishop)
on Jun 19, 2002 at 11:48 UTC ( #175631=note: print w/replies, xml ) Need Help??

in reply to Re: Pascal triange...
in thread Pascal's triangle...

No need to copy rows. Here's a much smaller function:
sub pascal { my @row; foreach (1 .. shift) { push @row => 1; $row [$_] += $row [$_ - 1] for reverse 1 .. @row - 2; print "@row\n"; } }


Replies are listed 'Best First'.
Re: Pascal triange...
by Abigail-II (Bishop) on Jun 19, 2002 at 17:04 UTC
    Here's a variation on the theme. The difference is that this version doesn't change @row using pushes. It's going to be sized right the first time.
    sub pascal { my @row = (0) x $_ [0]; $row [0] = 1; foreach (1 .. shift) { print "@row[0 .. $_ - 1]\n"; $row [$_] += $row [$_ - 1] for reverse 1 .. @row; } }


      Maybe we can add space padding to your function :)
      sub pascal { my $max = shift or return; my @row = (0) x $max; $row[0] = 1; foreach (1 .. $max) { print " " x ($max - $_),"@row[0 .. $_ - 1]\n"; $row[$_] += $row[$_ - 1] for reverse 1 .. @row; } }
Re: Re: Pascal triange...
by kiat (Vicar) on Jun 19, 2002 at 13:40 UTC
    Hello! Abigail-II,

    I like your smaller function (it's really elegant) but I don't understand how it works, even though it's only a few lines. Could you explain the parts to me?

    Thanks in anticipation :)

      It's not hard to see how it works. First thing to realize that one way of calculating the next line in the triangle is to take the previous line twice, shift one of the line one position to the right, and then add the elements piecewise. For instance:
          1  3  3  1
             1  3  3  1
          ------------- +
          1  4  6  4  1
      But if you look carefully, you see that to each element, we add the element to the left of it, except for the two elements on the far ends - which will both be one (a "new" 1 on the right, and the one of the left remains "as is").

      And that's how the program works. We first add a new element (with the push), then for each element, we add the preceding one. We have to work backwards of course, which is achieved by the reverse.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://175631]
LanX is not affraid of Trump ...
LanX ... me is scared of people voting for Trumps
[erix]: until good old Putin lobs a bomb on Berlin :)
[Your Mother]: “I’m afraid of Americans…”
[Your Mother]: “No one needs anyone / They don’t even just pretend…”
[erix]: they've stuck nuclear missiles in Kaliningrad - they can just reach Berlin :)
[LanX]: naaah! Putin wouldn't nuke all the Russians in Berlin
[Your Mother]: Putin is a bad guy but he’s not that kind of bad guy. (Who would launch.)
[erix]: fortunately, Putin seems to be ill -- or at least, they're building a hospital for him and his cronies in Moscow

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-01-20 00:52 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.