Page 1 of 1

incrementing in php

Posted: Fri Apr 25, 2008 6:29 am
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;
}
?>

Re: incrementing in php

Posted: Fri Apr 25, 2008 7:01 am
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

Re: incrementing in php

Posted: Fri Apr 25, 2008 8:47 am
by Mordred
These are not perfect numbers.
My take:

Code: Select all

echo "6, 28";

Re: incrementing in php

Posted: Fri Apr 25, 2008 8:53 am
by onion2k

Code: Select all

<?php
    while ($x<100) { echo $x = $x + ++$y; }
 

Re: incrementing in php

Posted: Fri Apr 25, 2008 8:41 pm
by sampaloc
i got it! thanx!