function add1(n) {
let sum = 0;
for(let i = 1; i <= n; i++) {
sum = sum + i;
}
console.log(sum);
}
function add2(n) {
sum = n * (n + 1) / 2;
console.log(sum);
}
add1(134217730);
add2(134217730);
When I ran this code with the value of 134217729
fun add1()
working fine. after this value it print different result.
and when I ran this concept in C, most of the time both functions give same result.
#include<stdio.h>
#include<conio.h>
typedef unsigned long long int ulli;
ulli add1(ulli num) {
ulli i,total=0;
for(i = 1; i <= num; i++) {
total += i;
}
printf("Result of add1 is %llu\n", total);
return total;
}
ulli add2(ulli num) {
ulli total;
total = (ulli)(num * (num + 1) /2.0);
printf("Result of add2 is %llu\n", total);
return total;
}
int main() {
ulli num;
printf("Enter A Number");
scanf("%llu", &num);
printf("Number is %llu\n",num);
add1(num);
add2(num);
return 0;
}
I have no Idea why this kind of strange output I have seen. is there any problem in my code. or this is internal behaviour of V8 engine. or something else.