Try any numbers, but these numbers make the program crash 2468,
8902
conjecture.c
#include <stdio.h>
#include "sieve.h"
#include <string.h>
int getPrimes (int a[], int low, int high, int num, int lowIndex,
int HighIndex);
int getNextIndex (int a[], int index, int s);
extern int holder[];
/**
* assigns two variables to store primes, one the
* smallest prime (low prime) and another stores the highest
* prime that is less than num (high prime)
*/
int
main ()
{
findPrimes ();
int infinite = 1;
while (infinite)
{
int num; // the even number that can be represented by two primes
if (scanf ("%d", &num) == EOF)
break;
if (num % 2 != 0 || num < 3 || num > 100000)
{
fprintf (stderr, "%d is an invalid number\n", num);
continue;
}
int high = 0; // the low prime
int low = 0; // the high prime
int highIndex = 0;
int lowIndex = 0;
low = holder[lowIndex]; // initially low prime is first element (2)
int i = 0;
for (; i < N - 1; i++) // find the high prime
{
if (holder[i] != 0 && holder[i] < num)
high = holder[i];
highIndex = i;
}
getPrimes (holder, low, high, num, lowIndex, highIndex);
}
return 0;
}
/**
* given both the low and high prime, this function
* will increase the low prime and decrease the
* high prime until low prime + high prime = num
*/
int
getPrimes (int a[], int low, int high, int num, int lowIndex, int highIndex)
{
if (lowIndex > N - 1 || highIndex < 0)
{
printf ("No primes found\n");
return 1;
}
else if (high + low == num)
{
printf ("%d %d\n", low, high);
return 0;
}
else if (high + low < num) // get the next biggest low prime
{
lowIndex = getNextIndex (holder, lowIndex, 0);
low = holder[lowIndex];
return getPrimes (holder, low, high, num, lowIndex, highIndex);
}
else if (high + low > num) // get the next smallest high prime
{
highIndex = getNextIndex (holder, highIndex, 1);
high = holder[highIndex];
return getPrimes (holder, low, high, num, lowIndex, highIndex);
}
return 1;
}
/**
* returns the index of the next prime
* if s = 0 then search to the right of current index in the array
* if s = 1 then search to the left of current index in the array
*/
int
getNextIndex (int a[], int index, int s)
{
if (s == 0)
{
int i = index + 1;
for (; i < N - 1; i++)
{
if (a[i] != 0)
return i;
}
}
else
{
int i = index - 1;
for (; i >= 0; i--)
{
if (a[i] != 0)
return i;
}
}
return 1;
}
===========
sieve.c
#include <math.h>
#include <stdio.h>
#include "sieve.h"
int findPrimes ();
int holder[N];
/**
* findPrimes() stores all the primes
* from 2 to N in an array.
*/
int
findPrimes ()
{
int i = 0;
int j = 2;
for (; i < N - 1; i++) // delete first two elements
{ // and shift all elements two places to the left
holder[i] = j;
++j;
}
float fN = N;
float square = sqrt (fN);
int prime = 0;
int k = 0;
for (; k < N - 1; k++)
{ // loop finds next prime
if (holder[k] == 0)
continue;
prime = holder[k];
if (holder[k] > square) // no need if to continue
break; // if prime is > square of N
int p = k + 1;
for (; p < N - 1; p++)
{ // loop finds multiples of prime
if (holder[p] == 0) // then get next element
continue;
if (holder[p] % prime == 0)
{ // its a multiple
holder[p] = 0;
}
}
}
return 0;
}
===============
the header file
enum
{ N = 100000 };
extern int holder[];
int findPrimes ();
"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message
news:40**********************@aconews.univie.ac.at ...
Joe Piscapo wrote: When I compile my program in Visual Studio it does not work for every
input, some numbers make it crash. But compiling in gcc works for those
numbers that made it crash in Visual Studio. Someone told me to check if Visual
Studio is using the gcc libraries. How do I do that? I found an option
for Runtime Library that has options like Single-threaded, etc.
It could be an uninitialized variable.
At least on Linux you definitely get zeroed memory at the
start of the program. I dont know if gcc does this also in
Windows.
VC does not zero out the memory, so it has some random
numbers in it.
I had this problem frequently when trying to port code. The
error lies in the program, in assuming that the memory is NULL.
hth
Christoph