formula - Sum of First Squared n Numbers Javascript -


alright need make javascript program finds sum of of first squared n numbers (hence title). i've finished except formula

<hmtl> <body> <center> <p1>sum of first n whole numbers</p> <p1>please enter value both boxes<br> press button , computer determine sum of numbers leading         yours</p> <script> function sum() { = parsefloat(document.getelementbyid('boxone').value);   alert("the sum "+);  } </script>  value 1: <input type="text" id="boxone" value="0"> <input type="submit" onclick="sum()" value="summarize"> </center> </body> </html> 

i know formula k^2=(n(n+1)(2n+1))/6 have no idea how put in code

alert("the sum "+ a*(a+1)*(2*a+1)/6);  

there no such thing "implicit multiplication" in programming language (other symbolic math programs). implicit multiplication formula had. note need replace n a since variable in have value of boxone (which presume input).

it interesting exercise compute value explicitly - rather applying clever formula, make computer work little bit harder. might done so:

var mysum = 0; (var i=1; i<=a; i++) {   mysum = mysum + * a; }  alert("the long way, " + mysum); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -