F
F
FaTaLcheG2021-09-07 13:27:29
Mathematics
FaTaLcheG, 2021-09-07 13:27:29

How to determine the number based on the result?

You need to write a function that takes n number of games as input and gives the number of teams at which exactly n games will be played

input: n - games played
output: n - teams that played exactly n games - games

are played in 2 stages:
1 - teams are divided into pairs and play for elimination until an odd number of teams remain (maybe more than one game)
// then just divide by 2 until an odd number of teams remain

2 - if there are more than 1 team left, you need to play games between all possible pairs of teams
// formula n(n - 1) / 2

I sort of figured out how to calculate the number of games based on the number of teams, but I can't figure out how to do the opposite, calculate the number of teams based on the number of games

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2021-09-07
@FaTaLcheG

Let your number of teams be p * 2 k , where p is odd.
Then the total number of games will be p*(2 k - 1) + p*(p - 1)/2 and this is equal to n - the input parameter.
You need to find such p and k for this equality to hold.
The formula shows that n must be divisible by p , so just iterate over all the divisors of n , calculate k and check that k is an integer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question