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!
Change
9 replies to this topic
#1
OFFLINE
Posted 17 January 2012 - 01:24 AM
OFFLINE
#2
OFFLINE
Posted 17 January 2012 - 01:32 AM
OFFLINE
A good starting place would be the maths behind it:
http://en.wikipedia.org/wiki/Pascal's_triangle
Sounds complex, though...good luck.
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
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
#3
OFFLINE
Posted 17 January 2012 - 01:44 AM
OFFLINE
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
Posted 17 January 2012 - 02:45 AM
OFFLINE
I solved it with my beginner's level in PHP and no experience. haha this is fun! 
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
<?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
- the_reaper and Saint like this
- Like This
#5
OFFLINE
Posted 17 January 2012 - 03:09 AM
OFFLINE
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
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
#6
OFFLINE
Posted 17 January 2012 - 03:16 AM
OFFLINE
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
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
Posted 17 January 2012 - 03:21 AM
OFFLINE
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
Posted 17 January 2012 - 10:02 AM
OFFLINE
#9
OFFLINE
Posted 17 January 2012 - 10:17 AM
OFFLINE
#10
OFFLINE
Posted 15 February 2012 - 01:58 AM
OFFLINE





This topic is locked
Back to top











