Reputation: 16311
I'm trying to find a way to calculate the sum of all numbers between 1 to N using JavaScript. The following is the code I have tried so far but it doesn't seem to work.
function numberSum(N) {
var total = 0;
for(var i = 1; i <= N; i++){
total += i;
}
return total;
}
I have tried using jslint and other validators online to check if I might have missed something but that doesn't seem to help me find the reason for the code not working either. Is there something that I'm missing above that's preventing the script from executing the addition??
Upvotes: 15
Views: 102400
Reputation: 11
The following code will be helpful in a simple way
let n = parseInt(readLine());
let sum = n*(n+1)/2;
console.log(sum);
Upvotes: 1
Reputation: 11
I use while loop I think this is more easy
function numberSum(S){
var a=1;
var sum=0;
while(a<=S){
sum+=a++;
}
return(sum);
}
function go(){
val = document.getElementById("value").value;
document.getElementById("results").innerHTML=val+":"+numberSum(val)
}
<input id="value">
<input type="Submit" onclick="go();">
<p id="results"></p>
Upvotes: 0
Reputation: 80
I know this is already solved but I wanted to post a quick ES6 oneliner I wrote after reading this thread and explain it more thoroughly for others who like me don't have a solid math background.
const numSum = (n) => n * (n+1) / 2;
It works because it uses a mathematic formula that Carl Friedrich Gauss came up with. (this has a great image).
Basically, whenever you are adding the sum of n numbers, you will have pairs in the sequence. So Gauss figured out that you didn't need to loop through each pair and add them, instead you just need to add the middle pair and multiply that sum by the total number of pairs. This works really well for programming because they aren't looping through each number which in programming would eat through your resources.
You can find the number of pairs by dividing n/2 and it also gives you the middle number then you just add 1 to find its pair.
Let say you are getting the sum of 1-100, by applying Gauss's approach, you'd want 50(101)=5050. 50 is the number of pairs and in the code, it is represented by n *
and 101 is the addition of the middle pair (50+51) or in the code (n+1)
, then finally we divide by 2 for the middle number.
Upvotes: 5
Reputation: 14318
It can be calculated by using the recursion
var numberSum = (n, a = n) => n ? numberSum(n = n - 1 , a = a + n) : a
Upvotes: -1
Reputation: 591
function SimpleAdding(num) {
place = 1;
i = num;
do {place = place += i; i--}
while (i > 1);
return place;
}
You set your placeholder variable equal to one. You also set the number of iterations equal to the input variable.
The do loop then adds your placeholder variable with 'i' and then the loop exits when it is no longer greater than 1, which is correct because you have your placeholder equal to one.
Upvotes: 0
Reputation: 6904
More general answer with recursion.
const sumRange = (min, max) => min !== max
? sumRange(min, max - 1) + max
: 0
While this might not be the most optimal answer for the case of min:0 max:n, it might be the easiest to read and understand.
Upvotes: -1
Reputation: 3
function numSum(n){
var sum = 0;
for(i = 0; i <= n; i++){
sum += i;
}
console.log(sum)
}
numSum(15);
Upvotes: -1
Reputation: 19264
Your code runs fine. How did you run it?
Demo:
function numberSum(N) {
var total = 0;
for(var i = 1; i <= N; i++){
total += i;
}
return total;
}
function run(){
val = document.getElementById("val").value;
document.getElementById("results").innerHTML=val+": "+numberSum(val)
}
<input id="val">
<input type="Submit" onclick="run();">
<p id="results"></p>
Upvotes: 7
Reputation: 29836
Your code is fine.
Keep it simple:
var res = (n * (n+1)) / 2;
Wiki.
Upvotes: 54