#Project Euler
Solutions to Project Euler
Written by dgklr, often by using python / c++
P101
Answer : 37076114526
P125
Answer: 2906969179
Running Time: 1068ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
using namespace std;
long long n;
long long li[11000];
long long pre[11000];
bool has[110000000];
const int N = 100000000;
long long ans = 0;
void check(long long ret){
if (ret > N) return;
char c[11];
if (has[ret] == 1) return;
sprintf(c,"%lld",ret);
int len = strlen(c);
for (int i=0;i<len;i++){
if (c[i] != c[len-i-1]) return;
}
has[ret] = 1;
ans += ret;
}
int main()
{
for (int i=1;i*i<=N;i++)
li[i] = i * i, pre[i] = pre[i-1] + li[i];
for (int i=2;i*i<=N;i++){
for (int j=0;j<=i-2;j++){
check(pre[i] - pre[j]);
}
}
cout << ans << endl;
}
P124
Answer: 21417
Running Time: 521ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
using namespace std;
#define N 100000
int pl = 0;
int prime[N];
int trans(int x){
int ret = 1;
for (int i=1;i<=pl;i++){
if (x % prime[i] == 0) ret *= prime[i];
while (x % prime[i] == 0) x /= prime[i];
if (x == 1) break;
}
return ret;
}
int getprime(){
for (int i=2;i<=N;i++){
int flag = 0;
for (int j=2;j*j<=i;j++)
if (i % j == 0) {flag = 1; break;}
if (!flag) prime[++pl] = i;
}
}
pair <int,int> ans[N+2];
int main(){
getprime();
for (int i=1;i<=N;i++){
ans[i].first = trans(i);
ans[i].second = i;
}
sort(ans+1,ans+N+1);
cout << ans[10000].second;
}
P126
Answer: 18522
Running Time: 81ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
const int N = 30000;
int C[N + 4];
int Cubes(int x, int y, int z, int n) {
return 2 * (x * y + y * z + x * z ) + 4 * (x + y + z + n - 2) * (n - 1);
}
int main(){
clock_t beg = clock();
for (int i=1;Cubes(i,i,i,1) <= N;i++)
for (int j=i;Cubes(i,j,i,1) <= N;j++)
for (int k=j;Cubes(i,j,k,1) <= N;k++)
for (int l=1;Cubes(i,j,k,l) <= N;l++)
C[Cubes(i,j,k,l)] ++;
for (int i=1;i<=N;i++)
if (C[i] == 1000) cout << i << ' ' << clock()-beg << endl, exit(0);
}
P123
Answer: 21035
Running Time: 740ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define MAXN 10000000000ll
long long prime[1000000];
int pl = 0;
#define ll long long
inline ll ksc(ll x,ll y,ll p){
ll res=0;
while(y){
if(y&1)res=(res+x)%p;
x=(x<<1)%p; y>>=1;
}return res;
}
long long ksm(long long x,long long base, long long MOD){
long long ret = 1;
long long xt = x;
while (base > 0){
if (base % 2 == 1) ret = ksc(ret,xt,MOD);
xt = ksc(xt,xt,MOD);
base /= 2;
}
return ret;
}
void getprime(){
for (int i=2;i<=1000000;i++){
int flag = 0;
for (int j=2;j*j<=i;j++){
if (i%j == 0) {flag = 1; break;}
}
if (!flag) prime[++pl] = i;
}
}
int main(){
clock_t beg = clock();
getprime();
for (int i=1;i;i++){
if ((ksm(prime[i]-1,i,prime[i]*prime[i]) + ksm(prime[i]+1,i,prime[i]*prime[i]))%(prime[i]*prime[i]) > MAXN)
cout << i << ' ' << clock() - beg << "ms"<< endl, exit(0);
}
}
P122
Answer: 1582
Running Time:1544ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
using namespace std;
const int MAXK = 200;
int opt[20] = {0,1};
int anslist[210] = {0,1};
void dfs(int now,int x){
if (x > 12) return;
if (now > MAXK) return;
if (anslist[now] > x) anslist[now] = x;
opt[x] = now;
for (int i=1;i<=x;i++){
dfs(now+opt[i],x+1);
}
}
int main(){
int beg = clock();
int ans = 0;
memset(anslist,0x3f,sizeof anslist);
anslist[1] = 1;
dfs(2,2);
for (int i=1;i<=MAXK;i++){
ans += anslist[i]-1;
}
cout << ans << ' ' <<clock() - beg << "ms" << endl;
}
P121
ans: 2269
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
using namespace std;
double f(int n, int i, int r, int b)
{
if (i == n) return b > r ? 1 : 0;
return 1 * f(n, i + 1, r, b + 1) / (i + 2) + (i + 1) * f(n, i + 1, r + 1, b) / (i + 2);
}
int main()
{
int beg = clock();
cout << (int)(1 / f(15, 0, 0, 0)) << endl;
cout << clock() - beg << "ms" << endl;
return 0;
}
P127
Answer: 18407904
A Better Solution
Running Time: 257ms
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define MAXN 120000
int rad[120000];
int prime[120000];
int tot[120000];
int pl;
void getprime(){
for (int i=2;i<=MAXN;i++){
int flag = 0;
for (int j=2;j*j<=i;j++){
if (i%j == 0) {flag = 1; break;}
}
if (!flag) prime[++pl] = i;
}
}
int init(){
for (int i=1;i<=MAXN;i++){
int tmp = i;
rad[i] = 1;
for (int j=1;j<=pl;j++){
if (tmp % prime[j] == 0) {
rad[i] *= prime[j];
while (tmp % prime[j] == 0) tmp /= prime[j];
}
if (tmp == 1) break;
if (prime[j] * prime[j] > tmp){
rad[i] *= tmp;
break;
}
}
}
for (int i=1;i<=MAXN;i++){
tot[rad[i]] ++;
}
}
long long ans = 0;
set <pair<int,int> > p;
int process(int x,int has){
int ret = 0;
for (auto i : p){
if (i.first * i.first > x) return ret;
if (__gcd(i.second,x) == 1 && __gcd(i.second,has-i.second) == 1 && i.first < rad[has-i.second]){
if (i.first * rad[has-i.second] < x) ret += has;
}
}
return ret;
}
int main(){
clock_t beg = clock();
getprime();
init();
p.insert(make_pair(1,1));
for (int i=2;i<MAXN;i++){
ans += process(i/rad[i],i);
p.insert(make_pair(rad[i],i));
}
cout << ans << ' ';
cout << clock() - beg << endl;
}
Running time: 1111ms
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define MAXN 120000
int rad[120000];
int prime[120000];
int tot[120000];
int pl;
void getprime(){
for (int i=2;i<=MAXN;i++){
int flag = 0;
for (int j=2;j*j<=i;j++){
if (i%j == 0) {flag = 1; break;}
}
if (!flag) prime[++pl] = i;
}
}
int init(){
for (int i=1;i<=MAXN;i++){
int tmp = i;
rad[i] = 1;
for (int j=1;j<=pl;j++){
if (tmp % prime[j] == 0) {
rad[i] *= prime[j];
while (tmp % prime[j] == 0) tmp /= prime[j];
}
if (tmp == 1) break;
if (prime[j] * prime[j] > tmp){
rad[i] *= tmp;
break;
}
}
}
for (int i=1;i<=MAXN;i++){
tot[rad[i]] ++;
}
}
long long ans = 0;
set <pair<int,int> > p;
int process(int x,int has){
map <int,int> h;
int ret = 0;
for (auto i : p){
h[i.second] = 1;
if (i.first * i.first > x) return ret;
if (__gcd(i.second,x) == 1 && __gcd(i.second,has-i.second) == 1 && h[has-i.second] == 0){
if (i.first * rad[has-i.second] < x) ret += has;
}
}
return ret;
}
int main(){
clock_t beg = clock();
getprime();
init();
p.insert(make_pair(1,1));
for (int i=2;i<MAXN;i++){
ans += process(i/rad[i],i);
p.insert(make_pair(rad[i],i));
}
cout << ans << ' ';
cout << clock() - beg << endl;
}
P128
Answer: 14516824220
Running Time: 201ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
using namespace std;
#define int long long
bool IsPrime(int x){
for (int i=2;i * i <=x;i++){
if (x % i == 0) return 0;
}
return 1;
}
signed main()
{
int beg = clock();
int count = 1;
int limit = 2000;
int n = 0;
int number = 0;
while (count < limit)
{
n++;
if (IsPrime(6 * n - 1) && IsPrime(6 * n + 1) && IsPrime(12 * n + 5))
{
count++;
number = (3 * n * n - 3*n + 2);
if (count >= limit) break;
}
if (IsPrime(6 * n + 5) && IsPrime(6 * n - 1) && IsPrime(12 * n - 7) && n != 1)
{
count++;
number = (3 * n * n + 3*n + 1);
}
}
cout << number << ' ' << clock() - beg << "ms" << endl;
return 0;
}
P129
Answer: 1000023
# copyright (c) dgklr
import math
def test(x):
if math.gcd(x,10) > 1:
return 0
i = 1
ret = 1
while i%x != 0:
i = (i * 10 + 1) % x
ret += 1
if ret > 1000000:
return 1
return 0
i = 1000001
while (1):
i += 2
if test(i):
print(i)
exit()
P114
Answer: 16475640049
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 50
int f[MAXN + 10][4]; // 0 -> Black 1 -> has 1 2 -> has 2 3 -> has 3 or More
signed main(){
f[0][0] = 1;
for (int i=1;i<=MAXN;i++)
f[i][0] = f[i-1][0] + f[i-1][3],
f[i][1] = f[i-1][0],
f[i][2] = f[i-1][1],
f[i][3] = f[i-1][3] + f[i-1][2];
cout << f[MAXN][0] + f[MAXN][3]; // Ans should only be ended with black or 3+ red
}
P115
Answer: 118
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 50
int f[10000][MAXN + 10]; // 0 -> Black 1 -> has 1 2 -> has 2 3 -> has 3 or More
signed main(){
clock_t beg = clock();
f[0][0] = 1;
for (int i=1;i;i++){
f[i][0] = f[i-1][0] + f[i-1][MAXN];
for (int j=1;j<=MAXN;j++)
f[i][j] = f[i-1][j-1];
f[i][MAXN] += f[i-1][MAXN];
if (f[i][0] + f[i][MAXN] > 1000000){
cout << i << endl;
break;
}
}
cout << clock() - beg << "ms"; // Ans should only be ended with black or 3+ red
}
P116
Answer: 20492570929
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 50
int f[MAXN][5]; // 0 -> Black 1 -> has 1 2 -> has 2 3 -> has 3 or More
signed main(){
clock_t beg = clock();
int Ans1 = 0;
f[0][0] = 1;
for (int i=1;i<=MAXN;i++){
f[i][0] = f[i-1][0] + f[i-1][1];
f[i][1] = f[i-1][0];
}
Ans1 = f[MAXN][0] - 1;
for (int i=1;i<=MAXN;i++){
f[i][0] = f[i-1][0] + f[i-1][2];
f[i][1] = f[i-1][0];
f[i][2] = f[i-1][1];
}
Ans1 += f[MAXN][0] - 1;
for (int i=1;i<=MAXN;i++){
f[i][0] = f[i-1][0] + f[i-1][3];
f[i][1] = f[i-1][0];
f[i][2] = f[i-1][1];
f[i][3] = f[i-1][2];
}
Ans1 += f[MAXN][0] - 1;
cout << Ans1 << endl;
cout << clock() - beg << "ms";
}
P117
Answer: 100808458960497
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 50
int f[MAXN][10]; // 1 -> R 2,3 -> G 4,5,6 -> B
signed main(){
clock_t beg = clock();
int Ans1 = 0;
f[0][0] = 1;
for (int i=1;i<=MAXN;i++){
f[i][0] = f[i-1][0] + f[i-1][1] + f[i-1][3] + f[i-1][6];
f[i][1] = f[i-1][0];
f[i][2] = f[i-1][0];
f[i][3] = f[i-1][2];
f[i][4] = f[i-1][0];
f[i][5] = f[i-1][4];
f[i][6] = f[i-1][5];
}
cout << f[MAXN][0] << endl;
cout << clock() - beg << "ms";
}
P118
Answer: 44680
// From www.mathblog.dk
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace euler {
class Problem118 {
public static void Main(string[] args) {
new Problem118().Bruteforce();
}
int[] perm = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
public void Bruteforce() {
Stopwatch clock = Stopwatch.StartNew();
int count = 0;
do {
count += CheckPartitions(0, 0);
} while (nextPermuation());
clock.Stop();
Console.WriteLine("There are {0} sets", count);
Console.WriteLine("Solution took {0} ms", clock.Elapsed.TotalMilliseconds);
}
private bool nextPermuation() {
int N = perm.Length;
int i = N - 1;
while (perm[i - 1] >= perm[i]) {
i = i - 1;
if (i == 0)
return false;
}
int j = N;
while (perm[j - 1] <= perm[i - 1]) {
j = j - 1;
}
// swap values at position i-1 and j-1
swap(i - 1, j - 1);
i++;
j = N;
while (i < j) {
swap(i - 1, j - 1);
i++;
j--;
}
return true;
}
private void swap(int i, int j) {
int k = perm[i];
perm[i] = perm[j];
perm[j] = k;
}
private int CheckPartitions(int startIndex, int prev) {
int count = 0;
for (int i = startIndex; i < perm.Length; i++) {
//form the number x of the digits startIndex -> i
int number = 0;
for(int j = startIndex; j <= i; j++){
number = number * 10 + perm[j];
}
//We only count ordered sets, so check that the current number is larger than the previous
if(number < prev) continue;
//Check that number is prime
if(!IsPrime(number)) continue;
// No more digits so return
if(i == (perm.Length-1)) return count + 1;
count += CheckPartitions(i + 1, number);
}
return count;
}
public bool IsPrime(int n) {
if (n < 2)
return false;
if (n < 4)
return true;
if (n % 2 == 0)
return false;
if (n < 9)
return true;
if (n % 3 == 0)
return false;
if (n < 25)
return true;
int s = (int)Math.Sqrt(n);
for (int i = 5; i <= s; i += 6) {
if (n % i == 0)
return false;
if (n % (i + 2) == 0)
return false;
}
return true;
}
}
}
P120
Answer: 333082500
Running Time: 28ms
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define MAXN 1000
#define int long long
signed main(){
int beg = clock();
int ans = 0;
for (int i=3;i<=MAXN;i++){
int MAX = 0;
int l1 = 1, l2 = 1;
for (int j=1;j<=i*2;j++){
l1 = l1 * (i-1) % (i*i);
l2 = l2 * (i+1) % (i*i);
MAX = max(MAX,(l1+l2) % (i*i));
}
ans += MAX;
}
cout << ans << ' ' << clock() - beg << "ms" << endl;
}
A Faster Soluton:
$ (a-1)^n + (a+1)^n \equiv (-1)^n + (-1)^{n-1} * na + 1 + na (\mod a^2)$
$ n = 2k + 1 (k\in \mathbb{N}) \Rightarrow (a-1)^n + (a+1)^n \equiv 2na (\mod a^2)$
$ n = 2k(k\in \mathbb{N}) \Rightarrow (a-1)^n + (a+1)^n \equiv 2 (\mod a^2)$
∴$\max r_a = \max (2na ,2) $
P130
Answer: 149253
Running Time: 43ms
Part of code:
int limit = 25;
int found = 0;
int n = 1;
int result = 0;
while (found < limit) {
n++;
if (IsPrime(n)) continue;
int a = A(n);
if (a != 0 && ((n - 1) % a == 0)) {
result += n;
found++;
}
}
P102
Answer: 228
Running Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
//using namespace std;
int check(int k1,int k2,int x,int y){
if (k2 * x > k1 * y) return -1;
if (k2 * x == k1 * y) return 0;
return 1;
}
int main()
{
int tot = 0;
int x1,x2,x3,y1,y2,y3;
while (scanf("%d,%d,%d,%d,%d,%d",&x1,&y1,&x2,&y2,&x3,&y3) != EOF){
if (check(x1,y1,x2,y2) * check(x1,y1,x3,y3) + check(x2,y2,x1,y1) *
check(x2,y2,x3,y3) + check(x3,y3,x2,y2) * check(x3,y3,x1,y1) == -3) tot ++;
}
std::cout << tot;
}
P107
Answer:259679
Running Time: 1ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 40
pair <int, pair <int,int> > edge[50*50];
int tot = 0;
int f[MAXN + 10];
int gf(int x){
if (f[x] == x) return x;
return f[x] = gf(f[x]);
}
int ut(int x,int y){
f[gf(x)] = gf(y);
}
signed main(){
freopen("data.in","r",stdin);
for (int i=1;i<=MAXN;i++) f[i] = i;
int pl = 0;
for (int i=1;i<=MAXN;i++){
for (int j=1;j<=MAXN;j++){
int tmp;
cin >> tmp;
if (tmp != -1) tot += tmp,edge[++pl].first = tmp, edge[pl].second.first = i, edge[pl].second.second = j;
}
}
tot /= 2;
sort(edge+1,edge+pl+1);
for (int i=1;i<=pl;i++){
if (gf(edge[i].second.first) != gf(edge[i].second.second))
tot -= edge[i].first, ut(edge[i].second.first,edge[i].second.second);
}
cout << tot;
}
P131
Answer: 173
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 1000000
int check(int x){
for (int i=2;i * i<=x;i++){
if (x % i == 0) return 0;
}
return 1;
}
signed main(){
int tot=0;
for (int i=2;i<=600;i++){
if (i * i * i - (i-1) * (i-1) * (i-1) > MAXN) break;
if (check(i * i * i - (i-1) * (i-1) * (i-1))) tot++;
}
cout << tot;
}
P133
Answer: 453647705
Running Time:3917ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define MAXN 40
using namespace std;
#define int long long
int ksm(int x,int base,int mod){
int res = 1;
int xt = x;
while (base > 0){
if (base & 1)
res = res * xt % mod;
xt = xt * xt % mod;
base /= 2;
}
return res;
}
int prime[500000];
int pl;
void getprime(){
for (int i=2;i<=100000;i++){
int flag = 0;
for (int j=2;j*j<=i;j++){
if (i % j == 0){
flag = 1;
break;
}
}
if (!flag) prime[++pl] = i;
}
}
int getans(int x){
int base=0;
int lft = 10;
while (lft % x != 1){
lft %= x;
lft *= 10;
base ++;
}
return base + 1;
}
signed main(){
int beg = clock();
getprime();
int ans = 2 + 3 + 5;
int tot = 0;
for (int i=4;i<=pl;i++){
int T = getans(prime[i]);
while (T % 2 == 0) T /= 2;
while (T % 5 == 0) T /= 5;
if (T != 1) ans += prime[i];//, cout << prime[i] << endl;
}
cout << ans << ' '<< clock() - beg << "ms"<< endl;
}
P134
Answer: 18613426663617118
Running Time: 655ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define MAXN 40
using namespace std;
#define int long long
int ksm(int x,int base,int mod){
int res = 1;
int xt = x;
while (base > 0){
if (base & 1)
res = res * xt % mod;
xt = xt * xt % mod;
base /= 2;
}
return res;
}
int prime[1000000];
int pl;
void getprime(){
for (int i=2;i<=1100000;i++){
int flag = 0;
for (int j=2;j*j<=i;j++){
if (i % j == 0){
flag = 1;
break;
}
}
if (!flag) prime[++pl] = i;
}
}
int D[20];
signed main(){
D[0] = 1;
for (int i=1;i<=8;i++)
D[i] = D[i-1] * 10;//,cout << D[i] << endl;
int beg = clock();
getprime();
int ans = 0;
int tot = 0;
for (int i=3;i<pl;i++){
if (prime[i] > 1000000) break;
int tmp = D[(int)log10(prime[i])+1] * (((prime[i+1] - prime[i]) * ksm(D[(int)log10(prime[i])+1],prime[i+1]-2,prime[i+1])) % prime[i+1]) + prime[i];
ans += tmp;
}
cout << ans << ' '<< clock() - beg << "ms"<< endl;
}
P135
Answer: 4989
Running Time: 131ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define MAXN 1000000
using namespace std;
#define int long long
int ksm(int x,int base,int mod)
{
int res = 1;
int xt = x;
while (base > 0)
{
if (base & 1)
res = res * xt % mod;
xt = xt * xt % mod;
base /= 2;
}
return res;
}
int prime[1000000];
int pl;
void getprime()
{
for (int i=2; i<=1100000; i++)
{
int flag = 0;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
flag = 1;
break;
}
}
if (!flag) prime[++pl] = i;
}
}
int ans[MAXN + 10];
signed main()
{
int beg = clock();
for (int u = 1; u <= MAXN ; u++)
{
for (int v = 1; u * v <= MAXN; v++)
{
if ((u + v) % 4 == 0 && 3 * v > u && ((3 * v - u) % 4) == 0) ans[u * v]++;
}
}
int tot = 0;
for (int i=1;i<=MAXN;i++)
if (ans[i] == 10) tot ++;
cout << tot << endl;
cout << clock() - beg << "ms" << endl;
}
P136
Answer: 2544559
Running Time: 1039ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define MAXN 50000000
using namespace std;
#define int long long
int ksm(int x,int base,int mod)
{
int res = 1;
int xt = x;
while (base > 0)
{
if (base & 1)
res = res * xt % mod;
xt = xt * xt % mod;
base /= 2;
}
return res;
}
int pl;
int prime[MAXN+10];
int visit[MAXN+10];
void getprime(){
for (int i = 2;i <= MAXN; i++) {
if (!visit[i]) {
prime[++prime[0]] = i;
}
for (int j = 1; j <=prime[0] && i*prime[j] <= MAXN; j++) {
visit[i*prime[j]] = 1;
if (i % prime[j] == 0) {
break;
}
}
}
}
int ans[MAXN + 10];
signed main()
{
int beg = clock();
getprime();
int result = 2;
for (int i = 2; i <= prime[0]; i++) {
if (prime[i] < MAXN / 4)
result++;
if (prime[i] < MAXN / 16)
result++;
if ((prime[i] - 3) % 4 == 0)
result++;
}
cout << result << endl;
cout << clock() - beg << "ms" << endl;
}
P137
Answer: 1120149658760
Running Time:0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 15
int Fibonacci[100];
signed main(){
Fibonacci[1] = 1;
Fibonacci[2] = 1;
for (int i=3;i<=MAXN * 2 + 2;i++){
Fibonacci[i] = Fibonacci[i-1] + Fibonacci[i-2];
}
cout << Fibonacci[MAXN * 2] * Fibonacci[MAXN * 2 + 1] << endl;
}
P138
Answer: 1118049290473932
Running Time:0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 12
int Fibonacci[100];
signed main(){
Fibonacci[1] = 1;
Fibonacci[2] = 1;
for (int i=3;i<= 99;i++){
Fibonacci[i] = Fibonacci[i-1] + Fibonacci[i-2];
}
int tot = 0;
for (int i=1;i<=MAXN;i++){
cout << (Fibonacci[i * 6 + 3]) / 2 << endl;
tot += (Fibonacci[i * 6 + 3]) / 2;
}
cout << tot;
}
P139
Answer: 10057761
Runnning Time: 0ms
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
#define int long long
#define MAXN 12
int Fibonacci[100];
signed main()
{
int result = 0;
int limit = 100000000;
int x = 1;
int y = 1;
while(x+y < limit)
{
int xnew = 3 * x + 4 * y;
int ynew = 2 * x + 3 * y;
x = xnew;
y = ynew;
result += limit / (x + y);
}
cout << result;
}
P140
Answer: 5673835352990
Running Time: 14ms
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace euler {
class Problem140 {
public static void Main(string[] args) {
new Problem140().Diophantine();
}
public void Diophantine() {
Stopwatch clock = Stopwatch.StartNew();
long result = 0;
long[,] start = new long[,] { { 0, -1 }, { 0, 1 }, { -3, -2 }, { -3, 2 }, { -4, -5 }, { -4, 5 }, { 2, -7 }, { 2, 7 } };
List<long> nuggets = new List<long>();
for (int j = 0; j < start.GetLength(0); j++) {
long k = start[j, 0];
long b = start[j, 1];
for (int i = 0; i < 30; i++) {
long knew = -9 * k + -4 * b + -14;
long bnew = -20 * k + -9 * b + -28;
k = knew;
b = bnew;
if (k > 0 && !nuggets.Contains(k))
nuggets.Add(k);
}
}
nuggets.Sort();
/*for (int i = 0; i < nuggets.Count; i++) {
Console.WriteLine("Nugget {0}: {1}", i, nuggets[i]);
} */
for (int i = 0; i < 30; i++) {
result += nuggets[i];
}
clock.Stop();
Console.WriteLine("Sum of 30 nuggets: {0}", result);
Console.WriteLine("Solution took {0} ms", clock.Elapsed.TotalMilliseconds);
}
}
}
P141
Answer: 878454337159
/* Copyright (c) dgklr */
#include<bits/stdc++.h>
#define int long long
#define DEBUG cerr << "Call out: " << __func__ << "\t" << "Line: " << __LINE__ << "\t :"
using namespace std;
map <int,int> p;
#define MAXN (int)(1e12)
signed main(){
int ans = 0;
for (int i=2;i<=10000;i++){
for (int j = 1; j < i;j ++){
if (i * i * i * j + j * j >= MAXN) break;
if (__gcd(i,j) > 1) continue;
for (int k=1;1;k++){
int tmp = i * i * i * j * k * k + j * j * k;
if (tmp >= MAXN) break;
if ((int)sqrt(tmp) * (int)sqrt(tmp) == tmp && p[tmp] == 0) ans += tmp, p[tmp] = 1;
}
}
}
cout << ans << endl;
}
P142
Answer: 1006193
x=434657
y=420968
z=150568