Add project-euler.190
parent
757115bc64
commit
4fdfb97a4d
|
@ -0,0 +1,48 @@
|
|||
! Copyright (c) 2008 Eric Mertens
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel sequences sequences.lib math math.functions math.ranges locals ;
|
||||
IN: project-euler.190
|
||||
|
||||
! PROBLEM
|
||||
! -------
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=190
|
||||
|
||||
! Let Sm = (x1, x2, ... , xm) be the m-tuple of positive real numbers
|
||||
! with x1 + x2 + ... + xm = m for which Pm = x1 * x22 * ... * xmm is
|
||||
! maximised.
|
||||
|
||||
! For example, it can be verified that [P10] = 4112 ([ ] is the integer
|
||||
! part function).
|
||||
|
||||
! Find Σ[Pm] for 2 ≤ m ≤ 15.
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
! Pm = x1 * x2^2 * x3^3 * ... * xm^m
|
||||
! fm = x1 + x2 + x3 + ... + xm - m = 0
|
||||
! Gm === Pm - L * fm
|
||||
! dG/dx_i = 0 = i * Pm / xi - L
|
||||
! xi = i * Pm / L
|
||||
|
||||
! Sum(i=1 to m) xi = m
|
||||
! Sum(i=1 to m) i * Pm / L = m
|
||||
! Pm / L * Sum(i=1 to m) i = m
|
||||
! Pm / L * m*(m+1)/2 = m
|
||||
! Pm / L = 2 / (m+1)
|
||||
|
||||
! xi = i * (2 / (m+1)) = 2*i/(m+1)
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: PI ( seq quot -- n )
|
||||
[ * ] compose 1 swap reduce ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
:: P_m ( m -- P_m )
|
||||
m [1,b] [| i | 2 i * m 1+ / i ^ ] PI ;
|
||||
|
||||
: euler190 ( -- n )
|
||||
2 15 [a,b] [ P_m truncate ] sigma ;
|
Loading…
Reference in New Issue