简体   繁体   English

欧拉项目233

[英]Project Euler 233

Let f(N) be the number of points with integer coordinates that are on a circle passing through (0,0), (N,0), (0,N), and (N,N). 令f(N)为通过(0,0),(N,0),(0,N)和(N,N)的圆上具有整数坐标的点数。

It can be shown that f(10000) = 36. 可以证明f(10000)= 36。

What is the sum of all positive integers N 1011 such that f(N) = 420 ? 使f(N)= 420的所有正整数N 1011的总和是多少?

Alright, so I think that I have the basic idea for Project Euler number 233. Here is my code: 好吧,所以我认为我对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;
}
}

Basically what I am doing is finding solutions for right triangles inscribed inside the circle, having a hypotenuse the length of the circles diameter. 基本上,我正在做的事情是找到圆形内刻直角三角形的解决方案,这些直角三角形的斜边为圆形直径的长度。 I am not positive that my code is correct. 我不能肯定我的代码是正确的。

If it is correct, then my problem is optimizing the f(N) function and optimizing the loop to find numbers for f(N) = 420. 如果正确,那么我的问题是优化f(N)函数并优化循环以找到f(N)= 420的数字。

New Code: 新代码:

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;
    }
}

A few points: 几点:

  1. Don't use floating point numbers for this. 请勿为此使用浮点数。
  2. Apart from that, your algorithm is in principle correct. 除此之外,您的算法原则上是正确的。
  3. But it won't finish before the heat death of the universe. 但是它不会在宇宙热死之前完成。

You have to find a much better approach, a few hints: 您必须找到一种更好的方法,有一些提示:

  1. Use only integer maths. 仅使用整数数学。
  2. Have a look at an introduction to number theory. 看一下数论入门。 Squares and right triangles might be interesting. 正方形和直角三角形可能很有趣。 Oh, and primes. 哦,素数。
  3. Have fun. 玩得开心。
  4. Let me repeat, number theory (but very basic, you can understand the relevant bits with high school math background; you will have to invest a bit of time though). 让我再说一遍,数论(但非常基础,您可以了解具有高中数学背景的相关知识;不过您将需要花费一些时间)。

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

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