Q: Suppose we have N
items. We want to divide these N
between three groups. Two groups should get the same amount of items.
How can we divide the items so that the difference between the number of items dedicated to each group is minimal?
Idea: x + 2y = N
=> diff = |y - x| = |(N - 3x)/2|
=> If N
is even, x
also should be even to have a natural diff
value. And if N
is odd, x
also should be odd to have a natural diff
value.
= > Based on N
, create the list of possible x
values, calculate the diff
values for each x
, and then find the minimum of diff
values. Then you can find the associated x
and calculate the y
value.
Q: Suppose we have P0
dollars. We also have a saving account in a bank that proposes r%
annual interest compounded every 1/n
period in a year (for example, every month becomes 1/12
). At the end of each period, we add P
dollars to the account. After t
years, how much money will we have?
Idea:
We can find the formula to find the final value by writing the value of our money step by step. We should consider that, at the end of each period, an interest of r/n
is applied to our money. So:
0 --> P0
1 --> P0 + (r/n)P0 + P = (1+r/n)P0 + P
2 --> (1+r/n)P0 + P + (r/n)[(1+r/n)P0 + P] + P = (1+r/n)^2.P0 + [(1+r/n) + 1]P
3 --> (1+r/n)^2.P0 + [(1+r/n) + 1]P + (r/n)[(1+r/n)^2.P0 + [(1+r/n) + 1]P] + P = (1+r/n)^3.P0 + [(1+r/n)^2 + (1+r/n) + 1]P
.
.
.
nt --> ... = (1+r/n)^(nt).P0 + [(1+r/n)^(nt) + (1+r/n)^(nt - 1) + ... + (1+r/n)^2 + (1+r/n) + 1]P
So, we should write a code that calculates the final coefficients of P0
and P
to calculate the final value!