Skip to content

Instantly share code, notes, and snippets.

@kiyotune
Created August 18, 2012 17:22
Show Gist options
  • Save kiyotune/3388549 to your computer and use it in GitHub Desktop.
Save kiyotune/3388549 to your computer and use it in GitHub Desktop.
Project Euler 問題27
#!/usr/local/bin/octave -q
clear;
% n=0: f=b
% (∴) b は素数である必要がある
% n=1: f=1+a+b
% b は素数であることが確定している
% b=2の時: a は偶数
% b=2以外: a は奇数
% (∴) a は偶数である必要がある(0も偶数と見なして除外)
_b = primes(999);
m = 100;
n = [0:m];
u = ones(1,m+1);
len_max = 0;
for(j=[1:1:length(_b)])
b = _b(j);
if(b == 2)
_a = [-998:2:998];
else
_a = [-999:2:999];
end
for(i=[1:1:length(_a)])
a = _a(i);
%calc
f = n.^2+a*n+b;
f = f.*(f > 0);
f2 = isprime(f);
len = find(xor(f2,u))(1)-1;
if(len > len_max)
printf("len = %d, a = %d, b = %d, Ans: a*b = %d\n", len, a, b, a*b);
len_max = len;
end
end
end
printf("(for check: last a = %d, b = %d\n", a, b);
% <出力>
% len = 71, a = -61, b = 971, Ans: a*b = -59231
@kiyotune
Copy link
Author

【TODO】aとbのループが遅い。行列計算でなんとか処理できないか考える。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment