Created
January 17, 2015 19:13
-
-
Save Mark-Simulacrum/3167a600dee8bda14a6a to your computer and use it in GitHub Desktop.
Calculates numbers that add up to a given sum with a given number of digits. // source http://jsbin.com/himemutimo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Calculates numbers that add up to a given sum with a given number of digits." /> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
Sum: <input type="number" id="sum" /><br> | |
Digits: <input type="number" id="digits" /><br> | |
<button id="submit">Calculate</button> | |
<section id="output"></section> | |
<script id="jsbin-javascript"> | |
var output = document.querySelector('#output'); | |
function append() { | |
var args = _.toArray(arguments); | |
var para = document.createElement('P'); | |
para.innerHTML = args.join(" "); | |
output.appendChild(para); | |
} | |
function calculate(sum, digitAmount) { | |
console.log(sum, digitAmount); | |
var sumOfTwo = sum; | |
var numberOfDigits = digitAmount; | |
var numberArr = _.range(0, sumOfTwo/2); // Going half-way is enough since other number is sumOfTwo - current | |
function isValidNumber(number) { | |
var digitArr = number.toString().split(""); | |
var digitArrTwo = (sumOfTwo - number).toString().split(""); | |
if (digitArr.length === numberOfDigits && digitArrTwo.length === numberOfDigits) { | |
if (_.uniq(digitArr).length === numberOfDigits && _.uniq(digitArrTwo).length === numberOfDigits) { | |
if (_.uniq(digitArr.concat(digitArrTwo)).length === numberOfDigits) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
var filteredNumbers = numberArr.filter(isValidNumber); | |
if (filteredNumbers.length > 0) { | |
var topArr = [filteredNumbers.length, "pair(s) of #s that add up to", sumOfTwo, "and are", numberOfDigits, "digits long:<br>"]; | |
var numArr = []; | |
filteredNumbers.forEach(function (firstNum) { | |
numArr.push([firstNum, "+", sumOfTwo - firstNum].join(" ")); | |
}); | |
topArr.push(numArr.join(",<br> ")); | |
append.apply(null, topArr); | |
} | |
} | |
//_.range(1, 5000).forEach(function (num) { calculate(num, 3); }); | |
document.getElementById("submit").addEventListener("click", function () { | |
output.innerHTML = ""; | |
var start = Date.now(); | |
calculate( | |
parseInt(document.getElementById("sum").value, 10), | |
parseInt(document.getElementById("digits").value, 10) | |
); | |
output.innerHTML += (Date.now() - start)/1000 + " seconds"; | |
}); | |
document.getElementById("sum").value = 1000000; | |
document.getElementById("digits").value = 6; | |
document.getElementById("submit").click(); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Calculates numbers that add up to a given sum with a given number of digits." /> | |
<script src="http://d3js.org/d3.v3.min.js"><\/script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
Sum: <input type="number" id="sum" /><br> | |
Digits: <input type="number" id="digits" /><br> | |
<button id="submit">Calculate</button> | |
<section id="output"></section> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">var output = document.querySelector('#output'); | |
function append() { | |
var args = _.toArray(arguments); | |
var para = document.createElement('P'); | |
para.innerHTML = args.join(" "); | |
output.appendChild(para); | |
} | |
function calculate(sum, digitAmount) { | |
console.log(sum, digitAmount); | |
var sumOfTwo = sum; | |
var numberOfDigits = digitAmount; | |
var numberArr = _.range(0, sumOfTwo/2); // Going half-way is enough since other number is sumOfTwo - current | |
function isValidNumber(number) { | |
var digitArr = number.toString().split(""); | |
var digitArrTwo = (sumOfTwo - number).toString().split(""); | |
if (digitArr.length === numberOfDigits && digitArrTwo.length === numberOfDigits) { | |
if (_.uniq(digitArr).length === numberOfDigits && _.uniq(digitArrTwo).length === numberOfDigits) { | |
if (_.uniq(digitArr.concat(digitArrTwo)).length === numberOfDigits) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
var filteredNumbers = numberArr.filter(isValidNumber); | |
if (filteredNumbers.length > 0) { | |
var topArr = [filteredNumbers.length, "pair(s) of #s that add up to", sumOfTwo, "and are", numberOfDigits, "digits long:<br>"]; | |
var numArr = []; | |
filteredNumbers.forEach(function (firstNum) { | |
numArr.push([firstNum, "+", sumOfTwo - firstNum].join(" ")); | |
}); | |
topArr.push(numArr.join(",<br> ")); | |
append.apply(null, topArr); | |
} | |
} | |
//_.range(1, 5000).forEach(function (num) { calculate(num, 3); }); | |
document.getElementById("submit").addEventListener("click", function () { | |
output.innerHTML = ""; | |
var start = Date.now(); | |
calculate( | |
parseInt(document.getElementById("sum").value, 10), | |
parseInt(document.getElementById("digits").value, 10) | |
); | |
output.innerHTML += (Date.now() - start)/1000 + " seconds"; | |
}); | |
document.getElementById("sum").value = 1000000; | |
document.getElementById("digits").value = 6; | |
document.getElementById("submit").click();</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var output = document.querySelector('#output'); | |
function append() { | |
var args = _.toArray(arguments); | |
var para = document.createElement('P'); | |
para.innerHTML = args.join(" "); | |
output.appendChild(para); | |
} | |
function calculate(sum, digitAmount) { | |
console.log(sum, digitAmount); | |
var sumOfTwo = sum; | |
var numberOfDigits = digitAmount; | |
var numberArr = _.range(0, sumOfTwo/2); // Going half-way is enough since other number is sumOfTwo - current | |
function isValidNumber(number) { | |
var digitArr = number.toString().split(""); | |
var digitArrTwo = (sumOfTwo - number).toString().split(""); | |
if (digitArr.length === numberOfDigits && digitArrTwo.length === numberOfDigits) { | |
if (_.uniq(digitArr).length === numberOfDigits && _.uniq(digitArrTwo).length === numberOfDigits) { | |
if (_.uniq(digitArr.concat(digitArrTwo)).length === numberOfDigits) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
var filteredNumbers = numberArr.filter(isValidNumber); | |
if (filteredNumbers.length > 0) { | |
var topArr = [filteredNumbers.length, "pair(s) of #s that add up to", sumOfTwo, "and are", numberOfDigits, "digits long:<br>"]; | |
var numArr = []; | |
filteredNumbers.forEach(function (firstNum) { | |
numArr.push([firstNum, "+", sumOfTwo - firstNum].join(" ")); | |
}); | |
topArr.push(numArr.join(",<br> ")); | |
append.apply(null, topArr); | |
} | |
} | |
//_.range(1, 5000).forEach(function (num) { calculate(num, 3); }); | |
document.getElementById("submit").addEventListener("click", function () { | |
output.innerHTML = ""; | |
var start = Date.now(); | |
calculate( | |
parseInt(document.getElementById("sum").value, 10), | |
parseInt(document.getElementById("digits").value, 10) | |
); | |
output.innerHTML += (Date.now() - start)/1000 + " seconds"; | |
}); | |
document.getElementById("sum").value = 1000000; | |
document.getElementById("digits").value = 6; | |
document.getElementById("submit").click(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment