Project Euler 101 :Optimum polynomial 最优多项式

Optimum polynomial

If we are presented with the first k terms of a sequence it is impossible to say with certainty the value of the next term, as there are infinitely many polynomial functions that can model the sequence.

As an example, let us consider the sequence of cube numbers. This is defined by the generating function,
un = n3: 1, 8, 27, 64, 125, 216, …

Suppose we were only given the first two terms of this sequence. Working on the principle that “simple is best” we should assume a linear relationship and predict the next term to be 15 (common difference 7). Even if we were presented with the first three terms, by the same principle of simplicity, a quadratic relationship should be assumed.

We shall define OP(k, n) to be the nth term of the optimum polynomial generating function for the first k terms of a sequence. It should be clear that OP(k, n) will accurately generate the terms of the sequence for n ≤ k, and potentially the first incorrect term (FIT) will be OP(k, k+1); in which case we shall call it a bad OP(BOP).

As a basis, if we were only given the first term of sequence, it would be most sensible to assume constancy; that is, for n ≥ 2, OP(1, n) = u1.

Hence we obtain the following OPs for the cubic sequence:

   
OP(1, n) = 1 1, 1, 1, 1, …
OP(2, n) = 7n−6 1, 8, 15, …
OP(3, n) = 6n2−11n+6 1, 8, 27, 58, …
OP(4, n) = n3 1, 8, 27, 64, 125, …

Clearly no BOPs exist for k ≥ 4.

By considering the sum of FITs generated by the BOPs (indicated in red above), we obtain 1 + 15 + 58 = 74.

Consider the following tenth degree polynomial generating function:

un = 1 − n + n2 − n3 + n4 − n5 + n6 − n7 + n8 − n9 + n10

Find the sum of FITs for the BOPs.



最优多项式

如果我们知道了一个数列的前k项,我们仍无法确定地给出下一项的值,因为有无穷个多项式生成函数都有可能是这个数列的模型。

例如,让我们考虑立方数的序列,它可以用如下函数生成,
un = n3: 1, 8, 27, 64, 125, 216, …

如果我们只知道数列的前两项,秉承“简单至上”的原则,我们应当假定这个数列遵循线性关系,并且预测下一项为15(公差为7)。即使我们知道了数列的前三项,根据同样的原则,我们也应当首先假定数列遵循二次函数关系。

给定数列的前k项,定义OP(k, n)是由最优多项式生成函数给出的第n项的值。显然OP(k, n)可以精确地给出n ≤ k的那些项,而可能的第一个不正确项(First Incorrect Term,简记为FIT)将会是OP(k, k+1);如果事实的确如此,我们称这个多项式为坏最优多项式(Bad OP,简记为BOP)。

在最基本的情况下,如果我们只得到了数列的第一项,我们应当假定数列为常数,也就是说,对于n ≥ 2,OP(1, n) = u1。

由此,我们得到了立方数列的最优多项式如下:

   
OP(1, n) = 1 1, 1, 1, 1, …
OP(2, n) = 7n−6 1, 8, 15, …
OP(3, n) = 6n2−11n+6 1, 8, 27, 58, …
OP(4, n) = n3 1, 8, 27, 64, 125, …

显然,当k ≥ 4时不存在坏最优多项式。

所有坏最优多项式的第一个不正确项(用红色标示的数)之和为1 + 15 + 58 = 74。

考虑下面这个十阶多项式生成函数:

un = 1 − n + n2 − n3 + n4 − n5 + n6 − n7 + n8 − n9 + n10

求其所有坏最优多项式的第一个不正确项之和。

解题

mathblog 提到拉格朗日多项式,突然明白了。

wiki中拉格朗日多项式的定义,在数值计算方法中叫拉格朗日差值函数

上面第k+1项就是所求的答案,至于为什么?已知的点很显然能够准确的预测出来,对于未知的点,为什么第k+1个点不能够预测对?

根据上面博客中写的程序,我记忆中本科时候好像写过这个程序的。

Java

package Level4;

public class PE0101{
    public static void run(){
        Lagrange();

    }
    public static void Lagrange(){
        long[] coef = {1,-1,1,-1,1,-1,1,-1,1,-1,1};
        Polynomial poly = new Polynomial(coef);

        long[] y = new long[coef.length];
        for(int i=0;i<y.length;i++)
            y[i] = poly.evaluate(i+1);

        long fits = 0;
        for(int n=1;n<=coef.length -1;n++){
            long result = 0;
            for(int i =1;i<=n;i++){
                long tmp1 = 1;
                long tmp2 = 1;
                for(int j=1;j<=n;j++){
                    if(i==j)
                        continue;
                    else{
                        tmp1 *= n + 1-j;
                        tmp2 *= i-j;
                    }
                }
                result +=tmp1*y[i-1]/tmp2;
            }
            fits +=result;
        }
        System.out.println(fits);
    }

    public static void main(String[] args){
        long t0 = System.currentTimeMillis();
        run();
        long t1 = System.currentTimeMillis();
        long t = t1 - t0;
        System.out.println("running time="+t/1000+"s"+t%1000+"ms");
//        37076114526
//        running time=0s1ms
    }
}
class Polynomial{
    private long[] coef;
    public int Degree;
    public Polynomial(int deg){
        Degree = deg;
        coef = new long[deg+1];
    }
    public Polynomial(long[] coef){
        Degree = coef.length - 1;
        this.coef = coef;
    }
    public long get(int i){
        return coef[i];
    }
    public void set(int i,long value){
        coef[i] = value;
    }
    public long evaluate(long x){
        long result =0;
        for(int i= this.Degree;i>=0;i--){
            result = result *x +get(i);
        }
        return result;
    }

}

Python

# coding=gbk

import time as time
import re
import math

def run():
    y = ploy()
    fits = 0
    for n in range(1,11):
        res = 0
        for i in range(1,n+1):
            tmp1 = 1
            tmp2 = 1
            for j in range(1,n+1):
                if i==j:
                    continue
                else:
                    tmp1 *= (n+1-j)
                    tmp2 *= (i-j)
            res += tmp1*y[i-1]/tmp2
        fits += res
    print res
def ploy():
    coef = [1,-1,1,-1,1,-1,1,-1,1,-1,1]
    y = list()

    for n in range(1,11):
        res = 1
        m = n
        for i in range(1,11):
            res = res + coef[i] * m
            m *=n
        y.append(res)
    print y
    return y 

t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"
时间: 2024-12-19 07:18:32

Project Euler 101 :Optimum polynomial 最优多项式的相关文章

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Python练习题 047:Project Euler 020:阶乘结果各数字之和

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

Python练习题 046:Project Euler 019:每月1日是星期天

本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Answer: 171 ''' from datetime import * firstDay = date(1901,1,1) lastDay = date(

Python练习题 035:Project Euler 007:第10001个素数

本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime # By listing the first six prime numbers: # 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? # Answer

Project Euler 126 - Cuboid layers

这题先是推公式- 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 f(t,x,y,z)=4(t?1)(x+y+z+t?2)+2(xy+yz+xz) 表示长宽高为x .y .z 的立方体第t 层放的立方体的个数. 接下来就是算答案了- 方法很简单:暴力 但是暴力还是有技巧的,开始我是直接从1到1000枚举t .x .y .z ,但这样出不来结果. 换成下面代码里的方法就行了. 1 #include <iostream> 2 #include <cstdio> 3 #includ

Project Euler 第一题效率分析

Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平台打造一个有趣和休闲 的环境. 项目主页:https://projecteuler.net 第一题 Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we ge

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule

Python练习题 041:Project Euler 013:求和、取前10位数值

本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. # Answer: 5537376230 numbers = '''371072875339021027987979982