Co-authored-by: sigonasr2 <sigonasr2@gmail.com>

main
sigonasr2, Sig, Sigo 2 years ago committed by GitHub
parent 8161004837
commit a0bc12a775
  1. BIN
      archives/10/current
  2. 31
      archives/10/src/main.c
  3. 7
      archives/10/src/utils.h
  4. BIN
      current
  5. 38
      src/main.c

Binary file not shown.

@ -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;
}

@ -0,0 +1,7 @@
#define true 1
#define false 0
#define boolean char
struct String{
int length;
char*str;
};

Binary file not shown.

@ -2,30 +2,30 @@
#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
For example, 32 + 42 = 9 + 16 = 25 = 52.
Find the sum of all the primes below two million.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
https://projecteuler.net/problem=9
https://projecteuler.net/problem=10
*/
int main(int argc,char**argv) {
int a=1;
int b=2;
int c=997;
int sum=0;
for (b=2;b<c;b++) {
for (a=1;a<b;a++) {
int tempC=1000-a-b;
if (a*a+b*b==tempC*tempC) {
printf("Pythagorean triplet of %d^2+%d^2=%d^2 works for %d+%d+%d=1000\n",a,b,tempC,a,b,tempC);
printf("Product is %d",a*b*tempC);
return 0;
}
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;
}
Loading…
Cancel
Save