ok i've taken the suggestions in and changed the code.
i got the scrap book out and wrote down a flow diagram and then worked out the the code, adding the square root idea and incrementing by 2 idea in.
i try to compile but i get lots of errors still. but it's close.
Code:
#include <stdio.h>
#include <math.h> /* included for sqrt() function */
main()
{
int p,prevprime,root,count,max;
count=0;
p=3;
prevprime=0;
FILE *fwrite;
fwrite=fopen("/home/primes.txt","a+"); /*adds to the end of file */
FILE *fread;
fread=fopen("/home/primes.txt","r"); /*reads values of file */
root=sqrt(p);
printf("Enter how many primes to find:");
scanf("%d",&max);
while(max<=count)
{count++
while(prevprime<=root) /*First logic argument checks previous prime value isn't greater then root value of p, since p divided by previous prime values (no greater than root) are the only ones needed to be checked*/
{if((prevprime%p)==0) /*2nd logic argument check if p is actually a prime value */
{printf("not a prime:%d\n",p); /* not a prime... dang */
fclose(fread); /*closes file so it will read from start next time */
prevprime=0;
p=p+2; /* incrementing up by 2, primes are only odds */
root=sqrt(p); /* new root value when new p value*/
}
else{fscanf(fread, "%d" &prevprime); /* changes preprime value so it can be checked again by 1st and 2nd logic argument */
printf("prevprime value:%d",prevprime); /* i just want to see the preprime value to check whats happening */
}
}
fclose(fread);
printf("Prime:%d\n",p); /* YAY prime number found*/
fprintf(fwrite,"%d\n",p); /* calls primes.txt and adds value at end of file */
p=p+2; /* incrementing up by 2, primes are only odds */
root=sqrt(p); /* new root value when new p value*/
prevprime=0; /* resets preprime value for logic arguments */
}
printf("finnished finding %d primes", max);
}