Created
March 7, 2017 10:55
-
-
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
This file contains hidden or 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
// 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