I'm trying to figure out how to use the sieve of eratosthenes to find the prime numbers from 1-300. I'm having trouble figuring it out, so any help would be nice! Btw, im new to programming so if you could keep it simple that would be best Below is my code (so far)
#include <stdio.h>
#include <simpio.h>
#include <genlib.h>
#include <math.h>
#define max 301
main()
{
bool is_prime[max];
int i, int1, j, n;
int1=sqrt(max);
for(n=0; n<=max; n++);
{
is_prime[n]=TRUE; //set everything to prime
}
is_prime[0]=FALSE; //false = NOT prime
is_prime[1]=FALSE;
for(i=2; i<int1; i++); //multiply starting from 2 end at 17
{
for(j=i; j<=(max/i); j++); //number being multiplied by
{
n=(j*i);
is_prime[n]==FALSE; //all multiples of i are false
}
}
if (is_prime[n]=TRUE); //print all prime numbers
{
printf("%d", n);
}
getchar();
}
Here is a sample implementation in c++:
In order to find the prime numbers from 1-300 you have to use a technique called sieve of Eratosthenes which is the most efficient way to find the prime number list from a given range.
Here is the Code:
You can have a look at the implementation here.
Sieve implementation:
And there is a blog written on Prime Numbers link.
Be inappropriate semicolon except that it has been already pointed out. E.g. Is not executed when the block such as the following intended
fix like this
yes, C# example - but near enough to convert
Results in: