2

Please look at the following and see if you could advise.

cout << "2" << endl;
cout << "3" << endl;

ofstream of("Primes.txt");

unsigned long prime = 0;
unsigned long i = 1;
for (i = 1; i < 100000; i++)
{
    prime = ((i*2)+(i+1) + (i % 2));
    of << prime << endl;
}
of.close();
return 0;

The partially completed formula for calculating the nth prime

The nth prime is spat out but so is all of its prime factors

How to sieve through the list and find only primes.

5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49
53
55
59
61
65
67
71
73
77
79
83
85
89
91
95
97
101
103

OK I changed approaches a little - I will try implementing the sieve tonight - I am off to write Informatics test now, but here is my new implementation for the some primes.

vector<int> Primes;

bool IsPrime(int q)
{
    for(unsigned int i = 0; i < Primes.size(); i++)
    {
        if(q % Primes[i] == 0)
            return false;
    }
    return true;
}

int main()
{
    Primes.push_back(2);
    cout << "2" << " is prime" << endl;
    for (unsigned int i = 2; i < 1000000000; i++)
    {
        if(IsPrime(i))
        {
            Primes.push_back(i);
            cout << i << " is prime" << endl;
        }
    }
}

OK this does give primes but really uses a lot of mem. And grows slow over time as the vector gets longer.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
Aiden Strydom
  • 1,179
  • 2
  • 14
  • 40
  • 6
    check out the simple : http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes – Akanksh Oct 21 '11 at 09:30
  • Please post a complete program along with examples of expected and actual output. See http://sscce.org. – Björn Pollex Oct 21 '11 at 09:30
  • Your algorithm just outputs all numbers that are equal to +/-1 mod 6. This has nothing to do with prime numbers. – TonyK Oct 21 '11 at 09:57
  • 1
    @TonyK: It eliminates multiples of 2 and 3, meaning you can use a smaller seive to find the other primes. – Mike Seymour Oct 21 '11 at 10:10
  • 1
    @Mike: We also know that most prime numbers are odd. This program is about as useful as a program that prints out all odd numbers. – TonyK Oct 21 '11 at 10:13
  • @TonyK: It's more useful, in that it makes the seive 1/3 smaller than one that handles all odd numbers. But as you say, this is only the first stage of the algorighm. Hence the OP's question: "how to seive through the list and find only primes". – Mike Seymour Oct 21 '11 at 10:17
  • "It eliminates multiples of 2 and 3, meaning you can use a smaller sieve to find the other primes" - That's what i am trying to do now. Thanks will update this some time soon! - – Aiden Strydom Oct 21 '11 at 10:39

3 Answers3

3

Eliminating numbers dividable by prime numbers (2,3,5,7 etc.) is a not soo bad idea when you look for a list of (small) prime numbers but you should use the newly found prime numbers too to be sure that the list contains only primes (not only 2,3,5,7 but also those passing: 11,13,17 etc.)

For bigger primes (you just can't calculate the way explained if the numbers are too big as you need to check almost all numbers (say each 4-5 anyhow) from 1 to the number to check), the usual approach is to take a random big number and check if it passes Fermats Small Theorem with say 3,5,7 and 11 (IIRC the probability for it to be a non prime if it passes with just 3,5,7 and 11 is really improbable).

Check out Fermats primality test for a more hands on explanation.

Valmond
  • 2,640
  • 8
  • 25
  • 45
0

here is the most simplest logic:

//Prime Numbers generation in C++
//Using for loops and conditional structures
#include <iostream>
using namespace std;

int main()
{
int a = 2;       //start from 2
long long int b = 1000;     //ends at 1000

for (int i = a; i <= b; i++)
{

 for (int j = 2; j <= i; j++)
 {
    if (!(i%j)&&(i!=j))    //Condition for not prime
        {
            break;
        }

    if (j==i)             //condition for Prime Numbers
        {
              cout << i << endl;

        }
 }
}
}
-3
  #include<stdio.h>
int main(void)
{int x,i,l;
printf("number of enter test cases\n");
scanf("%d",&x);
if(x>10)
{
    return;
}
printf("enter the range(please do not enter 1)\n");
int mx[20];
 for(i=0;i<x*2;i=i+2)
 {
     scanf("%d%d",&mx[i],&mx[i+1]);
     if(mx[i]==1)
     {
      return;
     }
     }
       for(l=0;l<x*2;l=l+2){

     check(mx[l],mx[l+1]);
   }
  return 0;

 }
void check(int m,int n)
  { //for checking number is prime number or not
  int j,k;
 int flag;
 for(j=m;j<=n;j++)
  {   flag=0;
for(k=2;k<j;k++)
   {
       if(j%k==0)
         {
         flag++;
         }
  }
     if(flag==0)
   {
      printf("\n%d\n",j);

    }

 }
  • 1
    Could you provide context to your code-only answer? As it is, it's not very good or very clear as to what you're accomplishing. – Makoto Aug 11 '13 at 18:31