Skip to content

Commit

Permalink
problem6 added
Browse files Browse the repository at this point in the history
  • Loading branch information
goformarty committed May 11, 2017
1 parent 7a67a33 commit b88c128
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
14 changes: 13 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ <h4>Smallest multiple</h4>
<h5 class="answer">Answer: <span id="answer5"></span></h5>
</div>


<!-- Problem 6 -->
<div class="problemContainer">
<h2>Problem 6</h2>
<h4>Sum square difference</h4>
<br />
<p>The sum of the squares of the first ten natural numbers is 385. The square of the sum of the first ten natural numbers is 3025. <br /><br /> Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. <br /> <br /> Find the difference between the sum of the squares of the specified first natural numbers and the square of the sum.</p>
<br />
<input id="input6" type="number" min="1" max="200" placeholder="Enter your number" />
<button id="btn6">Calculate</button>
<br /><br />
<h5 class="answer">Answer: <span id="answer6"></span></h5>
</div>
</div>



<script src="main.js"></script>
Expand Down
39 changes: 39 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,43 @@ function problem5() {
return n * largestPower(2, max);
}

// PROBLEM 6: DOM
var btn6 = document.getElementById("btn6");
btn6.onclick = function() {
document.getElementById("answer6").innerHTML = problem6();
};

// PROBLEM 6: FUNCTION
function problem6() {
var max = document.getElementById('input6').value;
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 200) {
return "Please specify a number between 0-200";
}

function squareSum(max) {
var i, s = 1,
e = Math.log(max) / Math.LN10;
// special case: max is a power of 10
if (e - (e << 0) === 0) {
s = max * (max >> 1) + (max >> 1);
}
else {
for (i = 2; i <= max; i += 1) {
s += i;
}
}
return s * s;
}
function sumSquare(max) {
var i, s = 1;
for (i = 2; i <= max; i += 1) {
s += i * i;
}
return s;
}

return squareSum(max) - sumSquare(max);
}

0 comments on commit b88c128

Please sign in to comment.