LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 September 21 2014

hussam
Member

this randomly crossed my mind

This is what I have:
let us say the population in 2014 is 72 billion and it increases by 70 million a year. Right now, 60 million people have blue hats. If I gave every year half the increase in population blue hats (that is an increase of 35 million blue hats a year), which year will almost half the planet have blue hats? :)  (49.99%)
I tried making a libreoffice spreadsheet and after 27000 rows, apparently, 48.9% of the planet have blue hats. the increase is almost negligible at this stage so making more rows is useless.
I tried goal seeker but it failed.
Any easier way to compute this?

Offline

#2 September 21 2014

rolf
Member

Re: this randomly crossed my mind

There are so many things wrong with this.

Offline

#3 September 21 2014

rolf
Member

Re: this randomly crossed my mind

I'm impressed by how stable LibreOffice is, to be able to reach 48%.
If I understand your formula, with x the number of years needed, the result of the equation is x = infinity. Or if you want to reach 49.99%, it's just going to be a very big number.
You have to relate 0.01% (50-49.99) to the initial population of the world (72 billion) minus those who already had a hat (60 million).
Another thing which is wrong is considering population growth as a linear increase. It's not, it's exponential.

Last edited by rolf (September 21 2014)

Offline

#4 September 21 2014

rolf
Member

Re: this randomly crossed my mind

Have you been smoking something?

Offline

#5 September 21 2014

Johnaudi
Member

Re: this randomly crossed my mind

Never?

Since you're giving half the increase no hats, and the other half just one half. It will never catch up.

Correct me if I'm wrong, but I do not think that there is a possible way unless he gives more blue hats. In that case, what you can do is use this code: (I added another million for the blue hats, to give a solution to the problem)

long population_now = 72000000000, population_bluehats = 60000000;
            int year_now = 2014;

            while ((population_now / 2) > population_bluehats)
            {
                population_now += 70000000;
                population_bluehats += 36000000;
                year_now++;
            } Console.WriteLine("Year: " + year_now);
            Console.WriteLine("Total population: " + population_now);
            Console.WriteLine("Population with Blue Hats: " + population_bluehats);

// Year 37954 in this case, but in yours, it's #inf

Last edited by Johnaudi (September 21 2014)

Offline

#6 September 21 2014

hussam
Member

Re: this randomly crossed my mind

that's why I said almost :)
no, not smoking anything just bored ;)

Offline

#7 September 21 2014

rolf
Member

Re: this randomly crossed my mind

Let y1 be the initial year, and y2 be the year where 49.99% of the population have blue hats.
When you will reach y2, then the 0.01 remaining % will be comprised of those people who originally had no blue hat + all the new people who have no blue hat, that is (y2-y1)x35mil.
If you can calculate the number of new people who have no blue hat, or those who have a blue hat on y2, then you can find out y2 by dividing this number by the 35million to get the number of years between y1 and y2.
I think an algebraic equation can be built from all this. I'm a bit lazy to do it myself, though.

Last edited by rolf (September 21 2014)

Offline

#8 September 21 2014

rolf
Member

Re: this randomly crossed my mind

Here's a quick JavaScript code to find the answer (can be pasted in your browser console; [F12]):

var now = {
  year : 2014,
  totalPop : 72000000000,
  totalBlueHats : 60000000
};

    
function addYear(now) {
  now.year ++;
  now.totalPop += 70000000;
  now.totalBlueHats += 35000000;
}

while((now.totalBlueHats / now.totalPop)<.4999) {
  addYear(now);
}

console.log(now);

Froze my browser for a handful of seconds, but here was the result:

Object { year=5135272, totalPop=359400060000000, totalBlueHats=179664090000000}

Offline

#9 September 21 2014

rolf
Member

Re: this randomly crossed my mind

As for the equation:

Let y1 be the initial year, y2 the target year, y1p the total pop. at y1, y1b the bluehat population at y1, y1n the non-bluehat pop at y1, etc.

y1 = 2014
m = 1000000
y2-y1 = (y2b - y1b) / 35m
y2-y1 = (y2p - y1p) / 35m
y1p = y1b + y1n
y2p = y2b + y2n
y2b / y2p = 0.4999
y1p = 72000m
y1b = 60m

Can anyone come up with an equation?

Last edited by rolf (September 21 2014)

Offline

#10 September 21 2014

InVader
Member

Re: this randomly crossed my mind

Bn = Blue hats population after n years
Tn = Total population after n years

Bn=60E6 + 35E6 x n
Tn=72E9 + 70E6 x n

Ratio=Bn/Tn
=(60 + 35n)/(72000+70n)

n=(72000xRatio-60)/35/(1-2xRatio) <- correction

Ratio=0.4999 -> n = ~5,133,257 years

Of course the 50% mark will never be reached.

Now you can try a variation of this exercise by using red hats instead of blue ones :)

Edit: Added the dropped Ratio at the indicated point

Last edited by InVader (September 21 2014)

Offline

#11 September 21 2014

rolf
Member

Re: this randomly crossed my mind

Hi InVader. Thanks for the solution. I haven't understood everything that happens in it, but most of it.
But I'm wondering why our results differ. Mine is more like 5 million years, yours is 10 million.

Offline

#12 September 21 2014

InVader
Member

Re: this randomly crossed my mind

To start with, I have corrected my previous post, and the result is ~5,133,257 years.

The derivation is actually straight forward. It relies on the fact that every n years, blue hats will increase by 35 millions and the total by 70 millions.

Now:
Blue hats = 60,000,000 = 6E6
Total = 72,000,000,000 = 70E9

In n years, the increase will be:
Delta Blue hats: 35,000,000 x n
Delta Total: 70,000,000 x n
and the ratio will be:
Ratio = (60,000,000 + 35,000,000n)/(72,000,000,000 + 70,000,000n)
=(60 + 35n)/(72,000+70n)

Now get n in terms of Ratio:
n=(72000xRatio-60)/35/(1-2xRatio)
and do the math.

I haven't read your code to ascertain, but your slightly different answer may stem from not considering the initial 60 million blue hats.

Offline

#13 September 21 2014

rolf
Member

Re: this randomly crossed my mind

InVader wrote:

I haven't read your code to ascertain, but your slightly different answer may stem from not considering the initial 60 million blue hats.

I did consider them. Check out the first lines of the program, this is the state at the initial year.
My result - 5,135,272 - is the year when .4999 ratio is reached, not the number of years needed, n.
n is 5135272 - 2014 = 5133258 which is, er, much closer to your result :)

Offline

#14 September 21 2014

InVader
Member

Re: this randomly crossed my mind

We have then almost the same number.

My result is actually 5,133,257.14285 and hence ~5,133,257. But to be strictly correct (using integer years only), it should be rounded up to 5,133,258 which is your answer :)

Offline

#15 September 21 2014

rolf
Member

Re: this randomly crossed my mind

Yep, it's due to a different procedure. My program stops as soon as .4555 is reached (so it's probably a little above that ratio). It would not stop at year ...257 because by then .4555 is not reached yet.
By the way I read your equation, and it's clearer now, thanks!
There is something I'm scratching my head on, though, it's how you get from this

Ratio = (60,000,000 + 35,000,000n)/(72,000,000,000 + 70,000,000n)
=(60 + 35n)/(72,000+70n)

To this

Now get n in terms of Ratio:
n=(72000xRatio-60)/35/(1-2xRatio)
and do the math.

Last edited by rolf (September 21 2014)

Offline

#16 September 21 2014

Adnan
Member

Re: this randomly crossed my mind

Here's my code but I've been waiting around 7 minutes for the solution and it's still working. I'm trying to find a way to get rid of this While Loop, just wanted to join in with an early piece of code (that will probably never solve it quickly).

#!/usr/bin/python

#Population part
population_in_2014=72000000000
population_each_year=population_in_2014+70000000


#Hats part
hats_in_2014=60000000
hats_in_yearX=hats_in_2014+35000000

#percentage part
def percentage_of_hats(x):
    popluation = population_in_2014+((x-2014)*70000000)
    hats = hats_in_2014+((x-2014)*35000000)
    print 100.0*hats/population #percentage
x=2015 #year to start with
while True:
    x=x+1
    percentage_of_hats(x)
    if percentage_of_hats(x)>49.99:
        print x
        break
	

Last edited by Adnan (September 21 2014)

Offline

#17 September 21 2014

rolf
Member

Re: this randomly crossed my mind

I was just reading about the speed of various languages which can be used for web apps, namely Ruby, Lua, Python, JavaScript, PHP, and I remember Python being somewhat slow (Ruby as well). PHP should be average, JavaScript and Lua the fastest.

Also your percentage_of_hats seems to be unnecessarily complicated. I'm not sure why. Did you take a look at my JavaScript code? All what it does for every while loop, is 3 additions and one division. That's all that's needed.

Yours does 2 additions, 2 substractions, 3 multiplications and one division for every loop, then a call to print, which is probably the slowest of all these operations.

And do I see a redundant call to percentage_of_hats() in the while loop? If that's true, then multiply all of the above by two.

Last edited by rolf (September 21 2014)

Offline

#18 September 21 2014

Adnan
Member

Re: this randomly crossed my mind

rolf wrote:

I was just reading about the speed of various languages which can be used for web apps, namely Ruby, Lua, Python, JavaScript, PHP, and I remember Python being somewhat slow (Ruby as well). PHP should be average, JavaScript and Lua the fastest.

Also your percentage_of_hats seems to be unnecessarily complicated. I'm not sure why. Did you take a look at my JavaScript code? All what it does for every while loop, is 3 additions and one division. That's all that's needed.

Yours does 2 additions, 2 substractions, 3 multiplications and one division for every loop, then a call to print, which is probably the slowest of all these operations.

And do I see a redundant call to percentage_of_hats() in the while loop? If that's true, then multiply all of the above by two.

I tried not to look at your code to try it myself. Anyway, I guess mine is too complicated so let's ditch it.

Now isn't this a copycat of your Javascript code ?

#!/usr/bin/python


population_now=72000000000
hats_now=60000000
current_year=2014

while hats_now / population_now<.4999:
    current_year+=1

print current_year

It still does take a long time :/

EDIT : I guess I didn't understand the "now.year ++" part, what does it mean ?

Last edited by Adnan (September 21 2014)

Offline

#19 September 21 2014

InVader
Member

Re: this randomly crossed my mind

rolf wrote:

There is something I'm scratching my head on, though, it's how you get from this

Ratio = (60,000,000 + 35,000,000n)/(72,000,000,000 + 70,000,000n)
=(60 + 35n)/(72,000+70n)

To this

Now get n in terms of Ratio:
n=(72000xRatio-60)/35/(1-2xRatio)

R = (60 + 35n)/(72,000+70n)
(72,000+70n)R = 60 + 35n
72,000R+70nR = 60 + 35n
60 + 35n = 72,000R+70nR
35n - 70nR = 72,000R - 60
(35 - 70R)n = 72,000R - 60
n = (72,000R - 60)/(35 - 70R)
n = (72,000R - 60)/(35 x (1-2R))
n = (72,000R - 60)/35/(1-2R))

Offline

#20 September 21 2014

InVader
Member

Re: this randomly crossed my mind

A Pascal program took less than a second to output 5133258!

program Project1;

var
  B, T, N: Int64;
  P: double;
begin
  B := 60000000;
  T := 72000000000;
  N := 0;
  repeat
    N := N + 1;
    B := B + 35000000;
    T := T + 70000000;
    P := B/T;
  until p >= 0.4999;
  writeln(n);
end.

Offline

#21 September 22 2014

rolf
Member

Re: this randomly crossed my mind

Adnan wrote:

I tried not to look at your code to try it myself. Anyway, I guess mine is too complicated so let's ditch it.

Now isn't this a copycat of your Javascript code ?

#!/usr/bin/python


population_now=72000000000
hats_now=60000000
current_year=2014

while hats_now / population_now<.4999:
    current_year+=1

print current_year

It still does take a long time :/

It is going to take forever, because although it looks like a copycat, there is something missing: you should increase population_now and hats_now in every while loop. Right now you're just increasing the year, which means that the test (hats_now / population_now<.4999) will always return true, since hats_now and population_now don't change, so the loop will run forever!

Don't worry about writing a "copycat", not to brag, but this is a relatively simple problem, so if you want to solve it programmatically and in the most simple way, you'll most probably end up with something close to what I wrote.

Adnan wrote:

EDIT : I guess I didn't understand the "now.year ++" part, what does it mean ?

++ is just a short-hand for += 1. I don't know if it's available in Python.

InVader wrote:

R = (60 + 35n)/(72,000+70n)
(72,000+70n)R = 60 + 35n
72,000R+70nR = 60 + 35n
60 + 35n = 72,000R+70nR
35n - 70nR = 72,000R - 60
(35 - 70R)n = 72,000R - 60
n = (72,000R - 60)/(35 - 70R)
n = (72,000R - 60)/(35 x (1-2R))
n = (72,000R - 60)/35/(1-2R))

Thanks :)
Got it now. My algebra skills are a little rusty, so thanks for that.

Offline

#22 September 22 2014

m.sabra
Member

Re: this randomly crossed my mind

public class blueHats {
        public static void main(String[] args){
                int year = 2014;
                double pop = 72000000000.0;
                double blue= 60000000.0;
                double coef = blue/pop;
                while(coef < 0.4999){
                        year++;
                        pop =pop + 70000000.0;
                        blue= blue + 35000000.0;
                        coef = blue/pop;
                }
                System.out.println(year);
        }
}

the year is 5135272

i also tried to solve it mathematically and came up with this
(pop is the current population which is 72 billion, blue is the number of people who currently have blue hats 60 million, inc is the yearly increas which is 70 million)
gif.latex?year%3D%20%5Cfrac%7B0.4999pop-blue%7D%7B%5Cfrac%7Binc%7D%7B2%7D-0.4999inc%7D&plus;2014

Offline

#23 September 22 2014

Adnan
Member

Re: this randomly crossed my mind

rolf wrote:
Adnan wrote:

I tried not to look at your code to try it myself. Anyway, I guess mine is too complicated so let's ditch it.

Now isn't this a copycat of your Javascript code ?

#!/usr/bin/python


population_now=72000000000
hats_now=60000000
current_year=2014

while hats_now / population_now<.4999:
    current_year+=1

print current_year

It still does take a long time :/

It is going to take forever, because although it looks like a copycat, there is something missing: you should increase population_now and hats_now in every while loop. Right now you're just increasing the year, which means that the test (hats_now / population_now<.4999) will always return true, since hats_now and population_now don't change, so the loop will run forever!

Don't worry about writing a "copycat", not to brag, but this is a relatively simple problem, so if you want to solve it programmatically and in the most simple way, you'll most probably end up with something close to what I wrote.

Adnan wrote:

EDIT : I guess I didn't understand the "now.year ++" part, what does it mean ?

++ is just a short-hand for += 1. I don't know if it's available in Python.

Ah, that was stupid. It takes around 9s to solve it now.

Offline

#24 September 22 2014

InVader
Member

Re: this randomly crossed my mind

I wonder if Hussam was wearing a blue hat or a red one while this randomly crossed his mind :)

Offline

#25 September 22 2014

hussam
Member

Re: this randomly crossed my mind

InVader wrote:

I wonder if Hussam was wearing a blue hat or a red one while this randomly crossed his mind :)

Blue. It's my favorite color ;)

Offline

Board footer