wer0ckz Posted March 22, 2011 Share Posted March 22, 2011 Hi guys. help please. How to code few lines for small program that calculates the sum of the first 540 natural numbers. Start numbering from 0. Is there a url tutorial about my issue? Thanks Link to comment https://forums.phpfreaks.com/topic/231348-help-calculate-natural-numbers/ Share on other sites More sharing options...
kenrbnsn Posted March 22, 2011 Share Posted March 22, 2011 What do you mean by "natural numbers"? Whole numbers like 1,2,3, ... etc? Ken Link to comment https://forums.phpfreaks.com/topic/231348-help-calculate-natural-numbers/#findComment-1190691 Share on other sites More sharing options...
Psycho Posted March 22, 2011 Share Posted March 22, 2011 http://www.9math.com/book/sum-first-n-natural-numbers function sumOfNaturalNumbers($maxNum) { return ($maxNum * ($maxNum + 1)) / 2; } $answer = sumOfNaturalNumbers(540); Link to comment https://forums.phpfreaks.com/topic/231348-help-calculate-natural-numbers/#findComment-1190727 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.