1 ! Copyright (c) 2008 Eric Mertens.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences math math.functions math.ranges locals ;
6 ! http://projecteuler.net/index.php?section=problems&id=190
11 ! Let Sm = (x1, x2, ... , xm) be the m-tuple of positive real numbers
12 ! with x1 + x2 + ... + xm = m for which Pm = x1 * x22 * ... * xmm is
15 ! For example, it can be verified that [P10] = 4112 ([ ] is the integer
18 ! Find Σ[Pm] for 2 ≤ m ≤ 15.
24 ! Pm = x1 * x2^2 * x3^3 * ... * xm^m
25 ! fm = x1 + x2 + x3 + ... + xm - m = 0
27 ! dG/dx_i = 0 = i * Pm / xi - L
30 ! Sum(i=1 to m) xi = m
31 ! Sum(i=1 to m) i * Pm / L = m
32 ! Pm / L * Sum(i=1 to m) i = m
33 ! Pm / L * m*(m+1)/2 = m
36 ! xi = i * (2 / (m+1)) = 2*i/(m+1)
40 : PI ( seq quot -- n )
41 [ * ] compose 1 swap reduce ; inline
46 m [1,b] [| i | 2 i * m 1+ / i ^ ] PI ;
48 : euler190 ( -- answer )
49 2 15 [a,b] [ P_m truncate ] sigma ;
51 ! [ euler150 ] 100 ave-time
52 ! 5 ms ave run time - 1.01 SD (100 trials)