Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
8161004837
commit
a0bc12a775
BIN
archives/10/current
Executable file
BIN
archives/10/current
Executable file
Binary file not shown.
31
archives/10/src/main.c
Normal file
31
archives/10/src/main.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||||
|
|
||||||
|
Find the sum of all the primes below two million.
|
||||||
|
|
||||||
|
https://projecteuler.net/problem=10
|
||||||
|
*/
|
||||||
|
int main(int argc,char**argv) {
|
||||||
|
FILE*f=fopen("archives/primegenerator/primes","r");
|
||||||
|
long sum=0;
|
||||||
|
int ch=' ';
|
||||||
|
while ((ch=fgetc(f))!='{');
|
||||||
|
while (true) {
|
||||||
|
int digit=0;
|
||||||
|
while ((ch=fgetc(f))!=',') {
|
||||||
|
digit*=10;
|
||||||
|
digit+=ch-'0';
|
||||||
|
}
|
||||||
|
if (digit>=2000000) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
sum+=digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
printf("\n\nThe sum of all primes below 2000000 is %ld",sum);
|
||||||
|
return 0;
|
||||||
|
}
|
7
archives/10/src/utils.h
Normal file
7
archives/10/src/utils.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
#define boolean char
|
||||||
|
struct String{
|
||||||
|
int length;
|
||||||
|
char*str;
|
||||||
|
};
|
42
src/main.c
42
src/main.c
@ -2,30 +2,30 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
|
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||||
|
|
||||||
a2 + b2 = c2
|
Find the sum of all the primes below two million.
|
||||||
For example, 32 + 42 = 9 + 16 = 25 = 52.
|
|
||||||
|
|
||||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
https://projecteuler.net/problem=10
|
||||||
Find the product abc.
|
|
||||||
|
|
||||||
https://projecteuler.net/problem=9
|
|
||||||
*/
|
*/
|
||||||
int main(int argc,char**argv) {
|
int main(int argc,char**argv) {
|
||||||
int a=1;
|
FILE*f=fopen("archives/primegenerator/primes","r");
|
||||||
int b=2;
|
long sum=0;
|
||||||
int c=997;
|
int ch=' ';
|
||||||
int sum=0;
|
while ((ch=fgetc(f))!='{');
|
||||||
for (b=2;b<c;b++) {
|
while (true) {
|
||||||
for (a=1;a<b;a++) {
|
int digit=0;
|
||||||
int tempC=1000-a-b;
|
while ((ch=fgetc(f))!=',') {
|
||||||
if (a*a+b*b==tempC*tempC) {
|
digit*=10;
|
||||||
printf("Pythagorean triplet of %d^2+%d^2=%d^2 works for %d+%d+%d=1000\n",a,b,tempC,a,b,tempC);
|
digit+=ch-'0';
|
||||||
printf("Product is %d",a*b*tempC);
|
}
|
||||||
return 0;
|
if (digit>=2000000) {
|
||||||
}
|
break;
|
||||||
}
|
} else {
|
||||||
}
|
sum+=digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
printf("\n\nThe sum of all primes below 2000000 is %ld",sum);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user