Skip to content

Instantly share code, notes, and snippets.

@masak
Last active December 18, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/5768668 to your computer and use it in GitHub Desktop.
Save masak/5768668 to your computer and use it in GitHub Desktop.
Pool administrator math problem
use v6;
# There's a swimming pool with a water inlet and a discharging tube. The water
# inlet can make the pool full in 6 hours. The discharging tube can empty the
# pool in 8 hours.
#
# One time the pool administrator forgot to switch off the discharging tube
# when he injected the water. 40 minutes had passed when he found it.
#
# Question: how long until the pool will be full of water?
# unit of time: hour
my $hours_to_fill_pool_from_water_inlet = 6;
my $hours_to_empty_pool_by_discharging_tube = 8;
# unit of volume: pool
# (assumed to be 1, because we don't know the volume)
# (but that's fine, since we end up dividing twice, so it can be any non-zero value)
my $pool = 1;
# unit of pool-filling speed: pool/hour
my $inlet_filling_speed = $pool / $hours_to_fill_pool_from_water_inlet;
my $discharging_tube_emptying_speed = $pool / $hours_to_empty_pool_by_discharging_tube;
my $net_filling_speed_when_both_are_on = $inlet_filling_speed - $discharging_tube_emptying_speed;
# until of time: pool/(pool/hour) = hour
my $time_from_empty_to_filled = $pool / $net_filling_speed_when_both_are_on;
my $minute = 1 / 60;
my $time_so_far = 40 * $minute;
my $remaining_time = $time_from_empty_to_filled - $time_so_far;
say sprintf "%f h, or %d h %d m",
$remaining_time,
$remaining_time,
($remaining_time - $remaining_time.Int) * 60;
# Output: 23.333333 h, or 23 h 20 m
@wanradt
Copy link

wanradt commented Jun 28, 2013

Wondered "use v6;" here. "use 5.010;" works fine ;)

@masak
Copy link
Author

masak commented Jul 5, 2013

@wanradt: Let me turn that around for you: why would I switch to using Perl 5, when "use v6;" works fine? :)

@masak
Copy link
Author

masak commented Jul 5, 2013

But there is one small difference in semantics, in fact. In Perl 6 there is no loss in precision, as everything is done with rational numbers. In Perl 5 there is loss of precision due to floating-point numbers. Not big enough to matter in this small calculation — but it might in a bigger one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment