Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Least amount of voters, given two halves

One of my former students sent me a message about this interview question he got while applying for a job as a Junior Developer.

There are two candidates running for president in a mock classroom election. Given the two percentages of voters, find out the least amount of possible voters in the classroom.

Examples:

Input: 50.00,50.00
Output: 2

Input: 25.00,75.00
Output: 4

Input: 53.23, 46.77
Output: 124 // The first value, 1138 was wrong. Thanks to Loïc for the correct value

Note: The sum of the input percentages are always 100.00%, two decimal places

The last example got me scratching my head. It was the first time I heard about this problem, and I'm kindof stumped on how to solve this.

EDIT: I called my student about the problem, and told me that he was not sure about the last value. He said, and I quote, "It was an absurdly large number output" :( sorry! I should've researched more before posting it online~ I'm guessing 9,797 is the output on the last example though..

like image 755
GaiusSensei Avatar asked Oct 23 '10 08:10

GaiusSensei


People also ask

Which age group has the lowest voting turnout?

Young people have the lowest turnout, though as the individual ages, turnout increases to a peak at the age of 50 and then falls again. Ever since 18-year-olds were given the right to vote in 1972, youth have been under represented at the polls as of 2003.

What do you call a split vote?

Split-ticket voting is when a voter in an election votes for candidates from different political parties when multiple offices are being decided by a single election, as opposed to straight-ticket voting, where a voter chooses candidates from the same political party for every office up for election.

How does split voting work?

Vote splitting is an electoral effect in which the distribution of votes among multiple similar candidates reduces the chance of winning for any of the similar candidates, and increases the chance of winning for a dissimilar candidate.

What affects voter turnout?

Additionally, many factors impact voter turnout, including new election laws, the type of election (e.g., presidential or midterm), and the competitiveness of the race. The number of voting-age voters (i.e., 18 years of age or older) in a jurisdiction. According to the 2020 U.S. Census, Connecticut's VAP was 2,869,227.


2 Answers

You can compute these values by using the best rational approximations of the voter percentages. Wikipedia describes how to obtain these values from the continued fraction (which can be computed these using the euclidean algorithm). The desired result is the first approximation which is within 0.005% of the expected value.

Here's an example with 53.23%:

10000 = 1 * 5323 + 4677
5323  = 1 * 4677 + 646
4677  = 7 * 646  + 155
646   = 4 * 155  + 26
155   = 5 * 26   + 25
26    = 1 * 25   + 1
25    = 25* 1    + 0

Approximations:
1:   1 / 1
 -> 1 = 100%
2:   1 / (1 + 1/1) 
 -> 1/2 = 50%
2.5: 1 / (1 + 1 / (1 + 1/6))
 -> 7/1 = 53.75%
3:   1 / (1 + 1 / (1 + 1/7))
 -> 8/15 = 53.33%
3.5: 1 / (1 + 1 / (1 + 1 / (7 + 1/3)))
 -> 25/47 = 53.19%
4:   1 / (1 + 1 / (1 + 1 / (7 + 1/4)))
 -> 33/62 = 53.23%

The reason we have extra values before the 3rd and 4th convergents is that their last terms (7 and 4 respectively) are greater than 1, so we must test the approximation with the last term decremented.

The desired result is the denominator of the first value which rounds to the desired value, which in this vase is 62.

Sample Ruby implementation available here (using the formulae from the Wikipedia page here, so it looks slightly different to the above example).

like image 144
Nabb Avatar answered Oct 31 '22 06:10

Nabb


First you can notice that a trivial solution is to have 10.000 voters. Now let's try to find something lower than that.

For each value of N starting à 1
  For Each value of i starting à 1
     If i/N = 46.77
       return N

Always choose the minimum of the two percentages to be faster.

Or faster :

For each value of N starting à 1
  i = floor(N*46.77/100)
  For j = i or i+1 
     If round(j/N) = 46.77 and round((N-j)/N) = 53.23
       return N


For the third example :
  • 605/1138 = .5316344464
  • (1138-605)/1138 = .4683655536

but

  • 606/1138 = .5325131810
  • (1138-606)/1138 = .4674868190

It can't be 1138...

But 62 is working :

  • 33/62 = .5322580645
  • (62-33)/62 = .4677419355

Rounded it's giving you the good values.

like image 23
Loïc Février Avatar answered Oct 31 '22 07:10

Loïc Février