繁体   English   中英

欧拉项目233

[英]Project Euler 233

令f(N)为通过(0,0),(N,0),(0,N)和(N,N)的圆上具有整数坐标的点数。

可以证明f(10000)= 36。

使f(N)= 420的所有正整数N 1011的总和是多少?

好吧,所以我认为我对233号欧拉计划有基本的了解。这是我的代码:

/*
 * Andrew Koroluk
 */

public class euler233 {
public static void main(String[] args) {
    System.out.println(f(10000));
    System.out.println(f(1328125));
    System.out.println(f(84246500));
    System.out.println(f(248431625));
    //if(true) return;

    double ans = 0;
    for(double N=10000; N<=(Math.pow(10, 11)); N++) {
        //System.out.println(N);
        if( f(N)==420 ) {
            ans+= N;
            System.out.println(N);
        }
    }
    System.out.println(ans);
}
static double f(double N) {
    double ans = 0;     
    double r = Math.sqrt(2*N*N)/2;
    //System.out.println(r*r);
    double r2 = r*r;

    for(int x=1; x<=r; x++) {
        for(int y=1; y<=r; y++) {
            if( x*x + y*y == r2 ) {
                ans+=4;
                break;
            }
        }
    }

    return ans;
}
static boolean isInt(double a) {
    if(a==(int)a) return true;
    return false;
}
}

基本上,我正在做的事情是找到圆形内刻直角三角形的解决方案,这些直角三角形的斜边为圆形直径的长度。 我不能肯定我的代码是正确的。

如果正确,那么我的问题是优化f(N)函数并优化循环以找到f(N)= 420的数字。

新代码:

public class euler233 {
    static long[] primes;
    public static void main(String[] args) {
        System.out.println(r(1328125));
        Clock c = new Clock();
        System.out.println(f2(10000));
        c.getTimeSeconds();
        c.reset();

        System.out.println(f2(1328125));
        c.getTimeSeconds();
    }
    static long f2(long N) {
        return SquaresR2(N*N);
    }
    static boolean isInt(long a) {
        if(a==(int)a) return true;
        return false;
    }
    static int SquaresR2(long n) {
        //System.out.println("start");
        int sum = 0;
        outer:
        for(int a=0; a<Math.sqrt(n)-1; a++) {
            for(int b=0; b<Math.sqrt(n)-1; b++) {
                if( a*a + b*b == n ) {
                    if(a>b) break outer;
                    sum+=4;
                    System.out.println(n+" = "+a+"^2 + "+b+"^2");
                }
            }
        }
        sum*=2;

        if(Math.sqrt(n)==(int)Math.sqrt(n)) sum+=4;
        return sum;
    }
    static int r(int n) {
        return 4*(d1(n) - d3(n));
    }
    private static int d1(int n) {
        int k=1, sum=0;
        while(true) {
            int d = 4*k+1;
            if(d>n) break;
            if(n%d==0) sum++;
            k++;
        }
        return sum;
    }
    private static int d3(int n) {
        int k=1, sum=0;
        while(true) {
            int d = 4*k+3;
            if(d>n) break;
            if(n%d==0) sum++;
            k++;
        }
        return sum;
    }
}

几点:

  1. 请勿为此使用浮点数。
  2. 除此之外,您的算法原则上是正确的。
  3. 但是它不会在宇宙热死之前完成。

您必须找到一种更好的方法,有一些提示:

  1. 仅使用整数数学。
  2. 看一下数论入门。 正方形和直角三角形可能很有趣。 哦,素数。
  3. 玩得开心。
  4. 让我再说一遍,数论(但非常基础,您可以了解具有高中数学背景的相关知识;不过您将需要花费一些时间)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM