Skip to content

Instantly share code, notes, and snippets.

@logicchains
Created March 19, 2014 10:56
Show Gist options
  • Save logicchains/9639404 to your computer and use it in GitHub Desktop.
Save logicchains/9639404 to your computer and use it in GitHub Desktop.
Prints the sum of primes less than or equal to input numbers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_ARRAY_CAPACITY 100
int isPrime(int value){
int i;
if (value > 2 && value != 4){
for (i = 2; i < value/2; i++) {
if (value % i == 0){
return 0;
}
}
}else{
return 0;
}
return 1;
}
/*Datastructure holding a prime number and the sum of itself and the primes lesser than it.*/
struct PrimeSumPair{
int Prime;
int Sum;
};
/*Datastructure for a dynamic array containing PrimeSumPairs.*/
struct DynPrimeArray{
struct PrimeSumPair* values;
int capacity;
int length;
int biggestTestedSoFar;
};
/*Create and zero-initialise a new DynPrimeArray of length size.*/
struct DynPrimeArray* NewPrimeArray(int size){
int i;
struct DynPrimeArray *primeArray = malloc(sizeof(struct DynPrimeArray));
primeArray->values = malloc(sizeof(struct PrimeSumPair)*size);
for (i = 0; i < size; i++){
primeArray->values[i].Prime = 0;
primeArray->values[i].Sum = 0;
}
primeArray->length = 0;
primeArray->capacity = size;
primeArray->biggestTestedSoFar = 0;
return primeArray;
}
/*Add a new value to a DynPrimeArray, extending the array if it's not large enough.*/
void AddValue(struct DynPrimeArray *primes, int prime, int sum){
if (primes->length == primes->capacity){
struct PrimeSumPair *newArray = malloc(sizeof(struct PrimeSumPair) * (primes->capacity * 2));
memcpy(newArray, primes->values, primes->length);
free(primes->values);
primes->values = newArray;
}
primes->values[primes->length].Prime = prime;
primes->values[primes->length].Sum = sum;
primes->length++;
}
/*Find the sum of primes up to the greatest prime that is less than n.*/
int FindSumUpToBiggestPrimeLessThan(int n, struct DynPrimeArray *primes){
int i;
for (i = 0; i < primes->length; i++){
if (primes->values[i].Prime > n){
break;
}
}
return primes->values[i-1].Sum;
}
/*Print all primes discovered so far and their sums.*/
void printPrimes(struct DynPrimeArray *primes){
int i;
for (i = 0; i < primes->length; i++){
printf("Prime: %d, sum: %d\n", primes->values[i].Prime, primes->values[i].Sum);
}
}
/*Calculate, if not calculated already, the primes lesser than n and the cumulative primes up to each of these primes.*/
int CalcSumOfPrimesLessThan(int n, struct DynPrimeArray *primes){
if (n >= primes->biggestTestedSoFar){
int i;
for (i = primes->biggestTestedSoFar+1; i <= n; i++){
if (isPrime(i)){
primes->values[primes->length].Prime = i;
if (primes->length == 0){
primes->values[primes->length].Sum = i + 0;
}else{
primes->values[primes->length].Sum = i + primes->values[primes->length-1].Sum;
}
primes->length++;
}
}
primes->biggestTestedSoFar = n;
}
return FindSumUpToBiggestPrimeLessThan(n, primes);
}
int main(int argc, char** argv){
struct DynPrimeArray* primesNSums = NewPrimeArray(INITIAL_ARRAY_CAPACITY);
int input = 0;
CalcSumOfPrimesLessThan(10, primesNSums);
printf("Enter a number: ");
while(scanf("%d", &input) == 1){
int result = CalcSumOfPrimesLessThan(input, primesNSums);
if (input <3){
result = 0;
}
if (isPrime(result)){
printf("The sum of the primes less or equal to %d is %d. That sum is prime.\n", input, result);
} else{
printf("The sum of the primes less or equal to %d is %d. That sum is not prime.\n", input, result);
}
printf("Enter a number: ");
}
free(primesNSums);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment