Jump to content

Change
HostCell.net Shared Hosting plan

Important Announcement!

Welcome to Hostcell 2.0! If you are an existing member and have trouble logging in please reset your password! If you are still having trouble, contact us on admin@hostcell.net and we can reset the password for you!


Pascal's triangle function


9 replies to this topic

#1
OFFLINE   BAngel

    Advanced Member

  • Members
  • 33 posts
Has anyone programmed a Pascal's triangle using a PHP function? I'm attending a PHP course and that's our homework for this week and don't have any idea where to start! any help with be very grateful!

HostCell.net Shared Hosting plan

#2
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts
A good starting place would be the maths behind it:
http://en.wikipedia.org/wiki/Pascal's_triangle

Sounds complex, though...good luck.
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image

#3
OFFLINE   BAngel

    Advanced Member

  • Members
  • 33 posts
Yeah, I've seen the construction there and you're right... VERY COMPLEX! everybody says that my teaches is out of her mind with that homework!

#4
OFFLINE   loremar

    Advanced Member

  • Members
  • 373 posts
I solved it with my beginner's level in PHP and no experience. haha this is fun! :D

<?php
 
$level = 10;
 
$triangle = array();
$triangle[0] = array(1);
 
for($i = 1; $i < $level; $i++)
{
$triangle[$i] = array();
$prev = 0;
 
foreach($triangle[$i-1] as $key => $value)
{
$triangle[$i][$key] = $prev + $value;
$prev = $value;
}
 
$triangle[$i][$i] = 1;
 
foreach($triangle[$i] as $key => $value)
{
print "$value ";
}
 
print '</br>';
}
?>


output:

1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

#5
OFFLINE   the_reaper

    Advanced Member

  • Members
  • 481 posts
Well done, loremar!
Half evil, half nerd, I'm the best of both worlds.
Don't fear The Reaper.
Need help with the JS SDK? Try here - App Page
Like reading? Try my blog on for size
Hostcell Review
Posted Image

#6
OFFLINE   HCN-SeriesN

    Advanced Member

  • Administrators
  • 1,221 posts
Pretty nice for a biginner coder :;
Regards,

SeriesN-HostCell Admin!

TOS | Privacy Policy | Hosting Plans | Client Area

"How terrible is wisdom when it holds no benefit for the wise?" - Louis Cypher

#7
OFFLINE   loremar

    Advanced Member

  • Members
  • 373 posts
Thanks, studying Lua now made me a little confused with syntax in PHP. They're similar in a way, that's why it's confusing and Lua starts with index 1 in arrays. Anyways, I already have lots of experience in C/C++, so I really don't find it very hard.

#8
OFFLINE   HCN-Northern

    Advanced Member

  • Support Team
  • 2,107 posts
ah lua... aint them horrible wow(world of warcraft) addons built around that?
Posted Image

#9
OFFLINE   loremar

    Advanced Member

  • Members
  • 373 posts

View PostHCN-Northern, on 17 January 2012 - 10:02 AM, said:

ah lua... aint them horrible wow(world of warcraft) addons built around that?

From what I see, yeah.

But the most important thing about lua they say is it speeds the development process.

#10
OFFLINE   BAngel

    Advanced Member

  • Members
  • 33 posts
Wow! thanks Loremar