Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Created October 6, 2017 01:04
Show Gist options
  • Save martynchamberlin/d1920c322aa68b509760df14d6293659 to your computer and use it in GitHub Desktop.
Save martynchamberlin/d1920c322aa68b509760df14d6293659 to your computer and use it in GitHub Desktop.
If 47% of respondents answered affirmative, what is the minimum number of total respondents necessary such that rounding the affirmatives to the closest integer would result in that percentage? Express the answer in terms of a formula that could be applied to any percentage from 1-99.
const minimumUniverse = (knownPercent) => {
knownPercent = parseInt(knownPercent);
if (!(knownPercent > 0 && knownPercent < 100)) {
return 'Please enter a number between 0 and 100';
}
for (let i = 1; i < 100; i++) {
for (let j = i + 1; j <= 100; j++) {
const currentPercent = Math.round((i / j) * 100);
if (currentPercent < knownPercent) {
break;
} else if (currentPercent === knownPercent) {
return `${knownPercent}% equates to ${i} out of ${j}`;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment