Skip to content

Commit 48133d0

Browse files
committed
add a solution to Euler #286.
1 parent 452df0d commit 48133d0

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

categories/euler/prob286-shlomif.p6

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use v6;
2+
3+
=begin pod
4+
5+
=TITLE Scoring probabilities
6+
7+
=AUTHOR Shlomi Fish
8+
9+
L<http://projecteuler.net/problem=286>
10+
11+
Barbara is a mathematician and a basketball player. She has found that the probability of scoring a point when shooting from a distance x is exactly (1 - x/q), where q is a real constant greater than 50.
12+
13+
During each practice run, she takes shots from distances x = 1, x = 2, ..., x = 50 and, according to her records, she has precisely a 2 % chance to score a total of exactly 20 points.
14+
15+
Find q and give your answer rounded to 10 decimal places.
16+
17+
=end pod
18+
19+
sub calc($q)
20+
{
21+
my @probs = (1.0);
22+
23+
for 1 .. 50 -> $x
24+
{
25+
my $p = 1 - $x / $q;
26+
my @new_probs;
27+
for 0 .. $x -> $i
28+
{
29+
@new_probs.push(
30+
(($i == $x) ?? 0 !! @probs[$i] * (1-$p))
31+
+
32+
(($i == 0) ?? 0 !! ($p * @probs[$i-1]))
33+
);
34+
}
35+
@probs = @new_probs;
36+
}
37+
return @probs[20];
38+
}
39+
40+
sub MAIN(:$verbose = False) {
41+
my $l = 50.0;
42+
my $h = 100000.0;
43+
my $Epsilon = 1e-16;
44+
my $wanted = 0.02;
45+
BIN_SEARCH:
46+
while (1)
47+
{
48+
my $m = (($l + $h) / 2);
49+
my $v_m = calc($m);
50+
printf("f(%.40f) = %.40f\n", $m, $v_m);
51+
my $delta = abs($v_m - $wanted);
52+
# say ("Delta = $delta");
53+
if ($delta < $Epsilon)
54+
{
55+
last BIN_SEARCH;
56+
}
57+
if ($v_m > $wanted)
58+
{
59+
$l = $m;
60+
}
61+
else
62+
{
63+
$h = $m;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)