O(1) 的小乐

Job Hunting

公告

记录我的生活和工作。。。
<2011年10月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

统计

  • 随笔 - 182
  • 文章 - 1
  • 评论 - 41
  • 引用 - 0

留言簿(10)

随笔分类(70)

随笔档案(182)

文章档案(1)

如影随形

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜

Project Euler 140

Need Patience!!calm down..

We get A(x) = (x + 3x2)/(1 - x - x2) = n is positive integer.
Then:
(n + 3)x2 + (n + 1)x - n = 0
D = 5n2 + 14n + 1 = m2 for some integer m.
Again we get:
5n2 + 14n - (m2 - 1) = 0
D = 5m2 + 44 = t2 for some integer t.
We get more general Pell's equation in the form t2 - 5m2 = 44.
I used http://mathworld.wolfram.com/PellEquation.html and http://en.wikipedia.org/wiki/Pell's_equation.
First solve "unit" form of this equation r2 - 5s2 = 1. First solution is (9, 4), then use recurence formula r(i + 1) = r(1)r(i) + 5s(1)s(i) and s(i + 1) = r(1)s(i) + s(1)r(i) to get all other solutions.
Then build set of soutions of t2 - 5m2 = 44 using it's set of fundamental solutions:{(t, m)} = {(7, 1), (8, 2), (13, 5), (17, 7), (32, 14), (43, 19)} and using the identity t2 - 5m2 = (t2 - 5m2)(r2 - 5s2) = (tr + 5ms)2 - 5(ts + mr)2 = 44. This gives tr + 5ms for the new value of t.
Finally, if t % 5 == 2 then n = (t - 7)/5 :)
My very simple program is:

Java

[hide code]

public class Problem140 {
    public static void main(String[] args) {
        long g, t, n;
        long[] p = {7, 8, 13, 17, 32, 43};
        long[] q = {1, 2, 5, 7, 14, 19};
        long[] r = {9, 9, 9, 9, 9, 9};
        long[] s = {4, 4, 4, 4, 4, 4};   int count = 5;
        // First 5 values of n
        long sum = 2 + 5 + 21 + 42 + 152;   while (true) {
            for (int k = 0; k < 6; k++) {
                g = r[k];
                r[k] = 9 * r[k] + 20 * s[k];
                s[k] = 9 * s[k] + 4 * g;
                t = p[k] * r[k] + 5 * q[k] * s[k];   if (t % 5 == 2) {
                    n = (t - 7) / 5;
                    sum += n;
                    count++;
                    System.out.println("n(" + count + ")=" + n);
                    if (count == 30) {
                        System.out.println("Sum=" + sum);
                        return;
                    }
                }
            }
        }
    }
}

 

 

Answer:

5673835352990

posted on 2010-09-17 21:15 Sosi 阅读(214) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


统计系统