1 ! Copyright (c) 2008 Eric Mertens
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences sequences.lib math math.functions math.ranges locals ;
9 ! 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.
23 ! Pm = x1 * x2^2 * x3^3 * ... * xm^m
24 ! fm = x1 + x2 + x3 + ... + xm - m = 0
26 ! dG/dx_i = 0 = i * Pm / xi - L
29 ! Sum(i=1 to m) xi = m
30 ! Sum(i=1 to m) i * Pm / L = m
31 ! Pm / L * Sum(i=1 to m) i = m
32 ! Pm / L * m*(m+1)/2 = m
35 ! xi = i * (2 / (m+1)) = 2*i/(m+1)
39 : PI ( seq quot -- n )
40 [ * ] compose 1 swap reduce ; inline
45 m [1,b] [| i | 2 i * m 1+ / i ^ ] PI ;
48 2 15 [a,b] [ P_m truncate ] sigma ;