-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.tiny
32 lines (27 loc) · 971 Bytes
/
birthday.tiny
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
###############################################################
#
# The following program calculates the smallest number of people
# in a room for whom the probability of completely unique birthdays
# is less than 50% (the birthday problem, where for 1 person the
# probability is 365/365 (or 100%), for 2 it is 364/365, for 3
# it is 364/365 it is 363/365, etc.) (answer = 23).
#
# This example is adapted from the Ocaml example included in the
# Ocaml Wikipedia entry.
#
###############################################################
year_size = 365
fn birthday_paradox prob people {
# use immediately-invoked lambda to avoid having
# to reassign prob
{ prob ->
if (prob < 0.5) {
println("answer = {0}\n", people+1)
}
else {
warn("Recursing with people == {0}", people+1)
birthday_paradox(prob, people+1)
}
} ((year_size - people) / year_size * prob)
}
birthday_paradox(1.0, 1)