Skip to content

Instantly share code, notes, and snippets.

@gngchrs
Created March 7, 2017 10:55
Show Gist options
  • Save gngchrs/461db5097f060c2b8a8545da2b1abb03 to your computer and use it in GitHub Desktop.
Save gngchrs/461db5097f060c2b8a8545da2b1abb03 to your computer and use it in GitHub Desktop.
Given x handshakes, get the number of guests in a meeting, and assuming each guest made a handshake to each other guest once. Get the number of guests. Common aptitude problem
// After studying the problem, you'll see an arithemetic progression like pattern
// Using the sum of arithmetic progression could easily work too.
// But on further inspection, it's a simple consectuive numbers problem.
function getConsecutiveDevisors(num) {
let lowerDevisor = Math.floor(Math.sqrt(num))
let higherDevisor = lowerDevisor + 1;
if (lowerDevisor * higherDevisor === num) {
return [lowerDevisor, lowerDevisor + 1]
}
return false
}
// In short, multiply the number of handshakes by two,
// Then find two concecutive numbers whose product gives the result,
// Then pick the higher number
function getGuests(num) {
let devisors = getConsecutiveDevisors(num * 2);
if (devisors) {
return devisors[1];
}
return 'Mathematically Impossible'
}
console.log(getGuests(45))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment