1
0
Fork 0

Problem 50

This commit is contained in:
Jonathan Chan 2016-12-07 23:52:58 -08:00
parent f59e27514a
commit 4fa79494b6
2 changed files with 48 additions and 0 deletions

BIN
50 Executable file

Binary file not shown.

48
50.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "primes.h"
int main () {
int n = 1000000;
long* primes;
bool* primesTable;
long numPrimes = listOfPrimes (n, &primes, &primesTable);
int terms = 22;
int mostTerms = terms;
int largestSum = 953;
while (terms < n) {
int sum = 0;
for (int i = 0; i < terms; i++) {
sum += primes[i];
}
if (sum > n)
break;
if (primesTable[sum]) {
mostTerms = terms;
largestSum = sum;
terms++;
continue;
}
for (int j = terms; j < n; j++) {
sum += primes[j] - primes[j - terms];
if (sum > n)
break;
if (primesTable[sum]) {
mostTerms = terms;
largestSum = sum;
break;
}
}
terms++;
}
printf ("%d terms: %d\n", mostTerms, largestSum);
free (primes);
free (primesTable);
}