简体   繁体   中英

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).

It can be shown that f(10000) = 36.

What is the sum of all positive integers N 1011 such that f(N) = 420 ?

Alright, so I think that I have the basic idea for Project Euler number 233. Here is my code:

/*
 * 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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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