1
\$\begingroup\$

The Challenge

Write a program that can calculate how many times and how long a ball will bounce based on a few input variables.


Input

There are three variables that will be passed. You can accept these however you like (user input, function call, etc). The variables are as follows:

  1. height
  2. velocity
  3. "bounciness"

They must be passed in this order.


Height

Height will be expressed as an integer, H, where 10^6 ≥ H ≥ 10. The height is measured in arbitrary units, u, which have no effect on your program's output.


Velocity

The velocity of the ball will be represented by an integer, V, where 10^6 ≥ V ≥ 1. It is expressed as Vu/s (V units per second).


Bounciness

The "bounciness" will be expressed as a decimal, B, where 0.00 ≤ B < 1.00. The maximum precision of B will be the hundredths place (two digits after the decimal point).


Output

You output will be two integers representing:

  • number of times the ball will bounce before coming to rest, N

  • how long it takes the ball to complete all of these bounces, T


Calculation

To calculate the number of times the ball will bounce, you would continuously multiply the current height by the "bounciness" until it dipped below 1. Once the result gets below 1, you should round the height down to 0, and stop calculating.

Each time these two are multiplied, the time is incremented by the quotient of the current height and velocity.

Intermediary calculations should have a minimum precision to the ten-thousandths (four digits after the decimal).


Example 1

If the input is:

10 10 0.5

The following table shows the necessary calculations for each bounce:

Height          Next       Height             Bounces
------        -------      ------             -------
10.0000       10.0000    *   0.5    = 5.0000        1
 5.0000        5.0000    *   0.5    = 2.5000        1
 2.5000        2.5000    *   0.5    = 1.2500        1
 1.2500        1.2500    *   0.5    = 0.6250        1

                                              -------
                                Total Bounces:      4

   Time added
----------------
10.0000    /  10   =   1.0000
 5.0000    /  10   =   0.5000
 2.5000    /  10   =   0.2500
 1.2500    /  10   =   0.1250

                       ------
           Total Time: 1.8750

At this point, the computation terminates because the height is less than 1. The time is rounded up to yield 2. These two integers should then either be returned or output as a string in this form:

4 2

Example 2

If the input is:

50 15 0.7

The following table shows the necessary calculations for each bounce:

Height      Next                  Height     Bounces
------     -------                -------    -------  
50.0000    50.0000  *   0.7    =  35.0000          1
35.0000    35.0000  *   0.7    =  24.5000          1
24.5000    24.5000  *   0.7    =  17.1500          1
17.1500    17.2500  *   0.7    =  12.0050          1
12.0050    12.0050  *   0.7    =   8.4035          1
 8.4035     8.4035  *   0.7    =   5.8825          1
 5.8825     5.8825  *   0.7    =   4.1177          1
 4.1177     4.1177  *   0.7    =   2.8824          1
 2.8824     2.8824  *   0.7    =   2.0177          1
 2.0177     2.0177  *   0.7    =   1.4124          1
 1.4124     1.4124  *   0.7    =   0.9887          1

                                            --------
                              Total Bounces:      11

Time added
-----------
50.0000    /  15   =     3.3333
35.0000    /  15   =     2.3333
24.5000    /  15   =     1.6333
17.1500    /  15   =     1.1433
12.0050    /  15   =     0.8003
 8.4035    /  15   =     0.5602
 5.8825    /  15   =     0.3922
 4.1177    /  15   =     0.2745
 2.8824    /  15   =     0.1922
 2.0177    /  15   =     0.1345
 1.4124    /  15   =     0.0942

                     ----------
          Total Time:   10.8913

At this point, the computation terminates because the height is less than 1. The time is rounded up to give 11. These two integers should then either be returned or output as a string in this form:

11 11

Scoreboard

For your score to appear on the board, it should be in this format:

# Language, Score

Or if you earned a bonus:

# Language, Score (Bytes - Bonus%)

function getURL(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){$.ajax({url:getURL(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),useData(answers)}})}function getOwnerName(e){return e.owner.display_name}function useData(e){var s=[];e.forEach(function(e){var a=e.body.replace(/<s>.*<\/s>/,"").replace(/<strike>.*<\/strike>/,"");console.log(a),VALID_HEAD.test(a)&&s.push({user:getOwnerName(e),language:a.match(VALID_HEAD)[1],score:+a.match(VALID_HEAD)[2],link:e.share_link})}),s.sort(function(e,s){var a=e.score,r=s.score;return a-r}),s.forEach(function(e,s){var a=$("#score-template").html();a=a.replace("{{RANK}}",s+1+"").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SCORE}}",e.score),a=$(a),$("#scores").append(a)})}var QUESTION_ID=58319,ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",answers=[],answer_ids,answers_hash,answer_page=1;getAnswers();var VALID_HEAD=/<h\d>([^\n,]*)[, ]*(\d+).*<\/h\d>/;
body{text-align:left!important}table thead{font-weight:700}table td{padding:10px 0 0 30px}#scores-cont{padding:10px;width:600px}#scores tr td:first-of-type{padding-left:0}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="scores-cont"><h2>Scores</h2><table class="score-table"><thead> <tr><td></td><td>User</td><td>Language</td><td>Score</td></tr></thead> <tbody id="scores"></tbody></table></div><table style="display: none"> <tbody id="score-template"><tr><td>{{RANK}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SCORE}}</td></tr></tbody></table>

\$\endgroup\$
  • 4
    \$\begingroup\$ I have two comments on the use of the sandbox: 1. If you're going to use the sandbox, use the sandbox. There is no point posting something there for 4 hours. 24 hours is the minimum which makes sense. 2. Once you post a question which has been sandboxed, you should replace the body of the sandbox post with a link to the live question. This reduces clutter and avoids confusion. \$\endgroup\$ – Peter Taylor Sep 20 '15 at 7:53
  • 1
    \$\begingroup\$ Why must the inputs be given in that order? \$\endgroup\$ – Beta Decay Sep 20 '15 at 9:21
  • \$\begingroup\$ Simplicity between answers. @BetaDecay \$\endgroup\$ – Zach Gates Sep 20 '15 at 15:19
2
\$\begingroup\$

CJam, 31 28 29 bytes

Latest version rounds up time to next integer. I had missed that in the requirements. This adds 2 bytes, and saves 1 byte based on a suggestion by Dennis.

l~_3$W#2$mLm]@1$#(@(/@/@*m]S\

Try it online

Thanks to @ThomasKwa for suggesting the use of mL instead of the quotient of two separate logarithms.

This uses basic log laws for calculating the number of bounces, and then the sum of a geometric series, which I had to look up on Wikipedia.

Explanation:

l~    Get and interpret input.
_     Copy B, will need this later.
3$    Get copy of H to top.
W#    Calculate H^-1 = 1/H as double.
2$    Get copy of B to top.
mL    Log with base.
m]    Ceiling. This is the number of bounces N.
@     Rotate one copy of B to top.
1$    Copy number of bounces to top.
#     Exponent B^N.
(     Subtract 1.
@     Get other copy of B to top.
(     Subtract 1.
/     Divide the two values.
@/    Rotate V to top and divide.
@*    Get H to top and multiply. This is T.
m]    Ceiling for time.
S\    Swap a space between the two resulting values.
\$\endgroup\$
  • \$\begingroup\$ @ThomasKwa I hadn't seen that the time needs to be rounded. It was shown as floating point in the example calculation, but it's indeed rounded to int at the very end. Weird, but I'll add it. CJam does not have a unary minus, so changing the sign is 2 bytes. I was considering that, but it didn't look like it was going to be shorter. \$\endgroup\$ – Reto Koradi Sep 20 '15 at 4:46
6
\$\begingroup\$

Python2, 84 82 bytes

from math import*
h,v,b=input()
n=ceil(-log(h,b))
print ceil(h*(b**n-1)/(b-1)/v),n

Closed forms, yay!

\$\endgroup\$
  • \$\begingroup\$ log(h,1./b) -> -log(h,b)? \$\endgroup\$ – lirtosiast Sep 20 '15 at 3:38
  • \$\begingroup\$ @ThomasKwa Of course :) \$\endgroup\$ – orlp Sep 20 '15 at 3:39
4
\$\begingroup\$

TI-BASIC, 35

Prompt H,V,B
-int(logBASE(H,B
Disp -int(H/V(B^Ans-1)/(1-B
Ans

A port of orlp's Python answer.

\$\endgroup\$
3
\$\begingroup\$

CoffeeScript, 62 bytes

f=(h,v,b,n=t=0)->(t+=h/v;h*=b;n++)while 1<=h;n+' '+Math.ceil t
\$\endgroup\$
2
\$\begingroup\$

Pyth, 27 bytes

L_/b1y_c*Qt^KvwJy.lQK*vwtKJ

Direct port of my Python answer. Takes input in the following format:

h
b
v
\$\endgroup\$
  • \$\begingroup\$ When I input 10 10 0.5 I get a syntax error, and 10\n10\n0.5 gives me -2 -1. \$\endgroup\$ – lirtosiast Sep 20 '15 at 4:00
  • \$\begingroup\$ @ThomasKwa As said in my answer, the correct format would be 10\n0.5\n10. \$\endgroup\$ – orlp Sep 20 '15 at 4:01
  • \$\begingroup\$ OP says the inputs must be passed in the order h v b (which I don't agree with, btw). \$\endgroup\$ – lirtosiast Sep 20 '15 at 4:02
  • \$\begingroup\$ @ThomasKwa Ugh... I can't be arsed to fix this. Too bad. \$\endgroup\$ – orlp Sep 20 '15 at 4:02
1
\$\begingroup\$

PHP, 87 bytes

Takes three command line arguments.

<?for($h=$argv[1],$n=$t=0;$h=>1;$n++){$t+=$h/$argv[2];$h*=$argv[3];}echo"$n ".ceil($t);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.