Rifts Posted November 29, 2010 Share Posted November 29, 2010 I can not seem to figure out how to do this.. I just need to add 100+99+98+97+96...+1 no you can not do 100! because that multiples them =] Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/ Share on other sites More sharing options...
kenrbnsn Posted November 29, 2010 Share Posted November 29, 2010 How about a simple loop: <?php $total = 0; for ($i=1;$i<101;++$i) { $total += $i; } echo $total; ?> Ken Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/#findComment-1141020 Share on other sites More sharing options...
PFMaBiSmAd Posted November 29, 2010 Share Posted November 29, 2010 Or - <?php $n = 100; $total = $n * ($n + 1)/2; echo $total; ?> Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/#findComment-1141025 Share on other sites More sharing options...
Rifts Posted November 29, 2010 Author Share Posted November 29, 2010 <3 you both thanks Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/#findComment-1141026 Share on other sites More sharing options...
Pikachu2000 Posted November 29, 2010 Share Posted November 29, 2010 Or even: <?php $i = 99; $num = 100; while( $i > 0 ) { $num += $i--; } echo $num; ?> Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/#findComment-1141027 Share on other sites More sharing options...
ManiacDan Posted November 29, 2010 Share Posted November 29, 2010 Or: array_sum(range(1,100)); PFMaBiSmAd is the most correct since that would be the fastest calculation, but there are plenty of ways to solve this problem. -Dan Link to comment https://forums.phpfreaks.com/topic/220159-epic-math-fail/#findComment-1141030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.