]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/100/100.factor
project-euler: Rewrap, update links, add copyrights, tests
[factor.git] / extra / project-euler / 100 / 100.factor
1 ! Copyright (c) 2008 Eric Mertens.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions project-euler.common ;
4 IN: project-euler.100
5
6 ! https://projecteuler.net/problem=100
7
8 ! DESCRIPTION
9 ! -----------
10
11 ! If a box contains twenty-one colored discs, composed of
12 ! fifteen blue discs and six red discs, and two discs were taken
13 ! at random, it can be seen that the probability of taking two
14 ! blue discs, P(BB) = (15/21)*(14/20) = 1/2.
15
16 ! The next such arrangement, for which there is exactly 50%
17 ! chance of taking two blue discs at random, is a box containing
18 ! eighty-five blue discs and thirty-five red discs.
19
20 ! By finding the first arrangement to contain over 10^12 =
21 ! 1,000,000,000,000 discs in total, determine the number of blue
22 ! discs that the box would contain.
23
24
25 ! SOLUTION
26 ! --------
27
28 : euler100 ( -- answer )
29     1 1
30     [ dup dup 1 - * 2 * 10 24 ^ <= ]
31     [ [ 6 * swap - 2 - ] keep swap ] while nip ;
32
33 ! TODO: solution needs generalization
34
35 ! [ euler100 ] 100 ave-time
36 ! 0 ms ave run time - 0.14 SD (100 trials)
37
38 SOLUTION: euler100