Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Programming Task help.... Any language.


I recently entered a contest, I'm finished the contest now bit there's this one question I could not get and it's been really bugging me,

I've tried ways of doing it in Pascal, JavaScript and with my little PHp knowledge but haven't succeeded.

Can anyone help me with it :-) it's really bugging me, here's the question:

A lot of scientists around the world are researching the global warming phenomena. One of the simplest models says: if this century the average temperature is T, then next century it will be T?+?D, in the next one T?+?2·D, and so on.

Scientists believe that there will be a world disaster if the average temperature is strictly greater than M degrees. Find out the numbers of centuries till world disaster.

Input The input contains three integer numbers each on a separate line: T,?D,?M (1???T,?D???100,?1???M???1000).

Output Print the numbers of centuries before the global disaster.

Sample test(s) input 20 1 23 output 4 input 100 10 90 output 0 input 1 1 1000 output 1000 Note In the first sample, the temperature increases by 1 each century. So, after 4 centuries it will be 24, which is more than 23, and a disaster will happen.

In the second sample, the disaster should happen in the current century, so the answer is 0.

2 Answers

I could write you a solution, but that would feel like cheating... So let me walk you through it.

You get the 3 inputs, in whatever way available, and assign them to variables T, D, and M.

First you check if T is greater than M — if it is, the temperature is already greater so you output 0.

Otherwise, you initialize a counter at 1 and start an endless loop. The rest of the code goes into that loop. Then you multiply D by the value of the counter and add it to the value of T.

If that value is greater than M you output the value of the counter (which will amount to the number of centuries), and you break out of the endless loop. If not, you just increment the counter by one.

That's pretty much it. It's not the most elegant of solutions, but it gets the job done. I can think of two much better solutions (more elegant, anyway) but you should be able to figure them out from this.

Thank you so much Dino Paškvan !

I never wanted the suction to the problem I just wanted to know how to figure it out in any langauge,

I am going to try this in JS now! Thanks :-)

Good luck! If you run into trouble, don't hesitate to ask for help.