incrementing in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sampaloc
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 6:22 am

incrementing in php

Post by sampaloc »

How can I create an application that will display perfect numbers from 1-100? The output should be 1 3 6 10 15 21 28.... Notice that the difference between the numbers are 2 3 4 5 6 7....
<?php
$one=0;
$two=1;

for ($x=0;$x<=45;$x++){

echo $nxt." ";
$cur=$one;
$nxt=$two;
$sum=$cur+$nxt;

$two=$sum;
}
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: incrementing in php

Post by VladSun »

Sounds like a school programming problem ;)

One way is to use recursion and another way - to do it iteratively ...
You have:

x(0) = 0
x(n) = x(n-1) + n

So, implement it in a function
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: incrementing in php

Post by Mordred »

These are not perfect numbers.
My take:

Code: Select all

echo "6, 28";
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: incrementing in php

Post by onion2k »

Code: Select all

<?php
    while ($x<100) { echo $x = $x + ++$y; }
 
sampaloc
Forum Newbie
Posts: 3
Joined: Fri Apr 25, 2008 6:22 am

Re: incrementing in php

Post by sampaloc »

i got it! thanx!
Post Reply