JacksonSoccerBoy Posted September 22, 2008 Share Posted September 22, 2008 Im trying to create a website that will find the sum of all integers up to the number entered (ex. 5 would be 1+2+3+4+5= 15) I cant get this to work It has to be something simple Im over looking Thanks in advance <code> <?php $num = $_GET["loop"]; $first = 0; $answer = 0; while ($num <= $first) { $first++; $answer == $first + $answer; PRINT $first; } PRINT "The sum of the numbers from 1 through $num is "; PRINT $answer; ?> </code> Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/ Share on other sites More sharing options...
ratcateme Posted September 22, 2008 Share Posted September 22, 2008 try this while ($num >= $first) { $first++; $answer = $first + $answer; PRINT $first; } Scott. Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647955 Share on other sites More sharing options...
Maq Posted September 22, 2008 Share Posted September 22, 2008 What is your output? Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647956 Share on other sites More sharing options...
ratcateme Posted September 22, 2008 Share Posted September 22, 2008 >= should just be > other wise it goes one to far Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647957 Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 That's inefficient, use Faulhaber's formula. $num = 101; echo ($num * ($num + 1) / 2); Edit: fitting it into your script... <?php $num = $_GET['loop']; $sum = $num * ($num + 1) / 2; echo "The sum of the numbers from 1 through $num is $sum"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647962 Share on other sites More sharing options...
JacksonSoccerBoy Posted September 22, 2008 Author Share Posted September 22, 2008 What is your output? <output> The sum of the numbers from 1 through 5 is 0 </output> should say: The sum of the numbers from 1 through 5 is 15 Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647964 Share on other sites More sharing options...
JacksonSoccerBoy Posted September 22, 2008 Author Share Posted September 22, 2008 That's inefficient, use Faulhaber's formula. $num = 101; echo ($num * ($num + 1) / 2); Edit: fitting it into your script... <?php $num = $_GET[lloopl]; $sum = $num * ($num + 1) / 2; echo "The sum of the numbers from 1 through $num is $sum"; ?> THANKS and to think I spent an hour troubleshooting what was wrong with my code when simple math would have done the trick Quote Link to comment https://forums.phpfreaks.com/topic/125355-basic-php-needs-new-eyes-to-find-problem/#findComment-647968 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.