This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.
But I don\'t think tha
Here is my implementation of Sieve of Eratosthenes (for primes between 2 & n)
#include <iostream>
int main (){
int n=0;
std::cout << "n = ";
std::cin >> n;
std::cout << std::endl;
if (n==0 || n==1){
std::cout << "No primes in this range" << std::endl;
return 0;
}
const int array_len = n-2+1;
int the_int_array[array_len];
for (int counter=2; counter <=n; counter++)
the_int_array[counter-2]=counter;
int runner = 0;
int new_runner = 0;
while (runner < array_len ){
if (the_int_array[runner]!=0){
new_runner = runner;
new_runner = new_runner + the_int_array[runner];
while (new_runner < array_len){
the_int_array[new_runner] = 0;
new_runner = (new_runner + the_int_array[runner]);
}
}
runner++;
}
runner = 0;
while (runner < array_len ){
if (the_int_array[runner]!=0)
std::cout << the_int_array[runner] << " ";
runner++;
}
std::cout << std::endl;
return 0;
}
Just try this. It's easy without any extra builtin functions.
#include <iostream>
int prime(int n,int r){
for(int i=2;n<=r;i++){
if(i==2 || i==3 || i==5 || i==7){
std::cout<<i<<" ";
n++;
} else if(i%2==0 || i%3==0 || i%5==0 || i%7==0)
continue;
else {
std::cout<<i<<" ";
n++;
}
}
}
main(){
prime(1,25);
}
Testing by 2,3,5,7 is good enough for up to 120, so 100 is OK.
There are 25 primes below 100, an 30 below 121 = 11*11.
Finding primes up to a 100 is especially nice and easy:
printf("2 3 "); // first two primes are 2 and 3
int m5 = 25, m7 = 49, i = 5, d = 4;
for( ; i < 25; i += (d=6-d) )
{
printf("%d ", i); // all 6-coprimes below 5*5 are prime
}
for( ; i < 49; i += (d=6-d) )
{
if( i != m5) printf("%d ", i);
if( m5 <= i ) m5 += 10; // no multiples of 5 below 7*7 allowed!
}
for( ; i < 100; i += (d=6-d) ) // from 49 to 100,
{
if( i != m5 && i != m7) printf("%d ", i);
if( m5 <= i ) m5 += 10; // sieve by multiples of 5,
if( m7 <= i ) m7 += 14; // and 7, too
}
The square root of 100 is 10, and so this rendition of the sieve of Eratosthenes with the 2-3 wheel uses the multiples of just the primes above 3 that are not greater than 10 -- viz. 5 and 7 alone! -- to sieve the 6-coprimes below 100 in an incremental fashion.
If j
is equal to sqrt(i)
it might also be a valid factor, not only if it's smaller.
To iterate up to and including sqrt(i)
in your inner loop, you could write:
for (int j=2; j*j<=i; j++)
(Compared to using sqrt(i)
this has the advantage to not need conversion to floating point numbers.)
If a number has divisors, at least one of them must be less than or equal to the square root of the number. When you check divisors, you only need to check up to the square root, not all the way up to the number being tested.
I did it in perl based on the most popular answer by ProdigySim's 2nd method. I had to add a break
equivalent in perl, last
, right after the print $i . " \n";
to avoid outputting the primes twice.
#!/bin/perl
use strict;
for(my $i=2; $i < 100; $i++){
my $prime = 1;
for (my $j=2; $j*$j<=$i; $j++){
if ($i % $j == 0){
$prime = 0;
last;
}
if($prime) {
print $i . " \n";
last;
}
}
}