Created
December 15, 2014 23:51
-
-
Save dmi3y/64cf81e39828e9612504 to your computer and use it in GitHub Desktop.
Find sequence of numbers which form specified sum
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
function checkNumSeq(sum, chunks) { | |
var | |
i, | |
hchunks, | |
chunk, | |
out = []; | |
if ( sum % chunks === 0 && chunks % 2 ) { | |
chunk = sum / chunks; | |
hchunks = (chunks - 1)/2; | |
out.push(chunk); | |
for (i = 1; i <= hchunks; i += 1) { | |
out.unshift(chunk - i); | |
out.push(chunk + i); | |
} | |
console.log('Got it! ' + out.join(' + ') + ' = ' + sum) | |
} else { | |
console.log('No paticular solution for ' + sum); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment