Pregunta

I am trying to formulate an equation to determine the number of users at a given time based on an input of new users every XX amount of time and their referred users.

Basically, We expect to be manually adding 100 users to the system every week. We are assuming that every new user refers one more user every following week. Each of those new users will refer one user the following week and so on. We would like a simple equation that we can input the week # and then return the number of total users at the end of that week. I have accomplished this using a for-loop but would like to have it in one simple equation if possible.

Here is some example data and expected results (totals) for the first few weeks.

Week 1        Week 2        Week 3        Week 4        Week 5
100 added     100 referred  100 referred  100 referred  100 referred
              100 added     100 referred  100 referred  100 referred
                            100 added     100 referred  100 referred
                                          100 added     100 referred
                                                        100 added

100 total     300 total     600 total     1000 total    1500 total

Also, what if the share rate is only 1 new user for every 2 users (50% share rate)?

Week 1        Week 2        Week 3        Week 4        Week 5
100 added     50 referred   25 referred   12.5 referred 6.25 referred
              100 added     50 referred   25 referred   12.5 referred
                            100 added     50 referred   25 referred
                                          100 added     50 referred
                                                        100 added

100 total     250 total     425 total     612.5 total   806.25 total

Any help is greatly appreciated.

[EDIT]

I have found this equation to work for a 1:1 share ratio based on simply noticing the pattern in the above table but I am not sure how to implement the share rate into the equation, how does the 50 actually come into play?

$rate = 1;
$increase = 100;
$week = 5;

$balance = ($increase*$week) + (50 * ($week-1) * $week);
¿Fue útil?

Solución

You originally didn't specify a language so I wrote a solution in ruby:

sum_of_exponential_decays = -> n { (0..n).map {|n| 100 * 0.5**n}.reduce(:+)}
sum_of_n = -> n {(1..n).inject(0) { |acc,x| acc += sum_of_exponential_decays[x-1]}}
sum_of_n[5]
# => 806.25

The code seems pretty clear and it's summing exponential decays as it says.

For additional explanation look at the sum_of_exponential decays function, the reduce(:+) simply sums all the elements in it and:

exponential_decays = -> n { (0..n).map {|n| 100 * 0.5**n}}
(1..5).map {|x| exponential_decays[x] }
# => [[100.0, 50.0],
 [100.0, 50.0, 25.0],
 [100.0, 50.0, 25.0, 12.5],
 [100.0, 50.0, 25.0, 12.5, 6.25],
 [100.0, 50.0, 25.0, 12.5, 6.25, 3.125]]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top