Skip to content

Instantly share code, notes, and snippets.

@jtribble
Last active January 20, 2016 17:09
Show Gist options
  • Save jtribble/27cf32a4d6966c2c1dbc to your computer and use it in GitHub Desktop.
Save jtribble/27cf32a4d6966c2c1dbc to your computer and use it in GitHub Desktop.
//
// See: http://careercup.com/question?id=4816567298686976
//
'use strict';
// var set = [2, 3], n = 8;
// expect: results.exp = [1, 2, 3, 4, 6, 8, 9, 12]
// expect: results.non = [5, 7, 10, 11]
// expect: results.result = 12
var primeExpressibles = function (set, n) {
if (n < 1) return {exp: [], non: [], result: null};
if (n === 1) return {exp: [1], non: [], result: 1};
var results = {exp: [1], non: [], result: 1};
iteration:
for (var i = 2; i < set[0] * n; i++) {
var num = i, j = 0;
while (set[j] !== undefined) {
if (!Number.isInteger(num / set[j])) {
j++; continue; // continue while loop
}
num /= set[j];
if (num === 1) {
results.exp.push(i);
if (results.exp.length === n) {
results.result = i;
return results;
}
continue iteration; // break out of while loop, continue iteration
}
}
results.non.push(i);
}
return results; // should never reach this point, in theory...
};
@smith-kyle
Copy link

Dude this is gnarly. I've never even seen that iteration property before. What does this do?

@smith-kyle
Copy link

Oh never mind I just saw the career cup question

@jtribble
Copy link
Author

I had never seen it either, pretty cool though, you can specify which loop you want to continue/break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment