[英]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;
}
}
幾點:
您必須找到一種更好的方法,有一些提示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.