primenumbers

Can you help with a program I'm trying to do in C.  I basically need a function that returns a the first 20 postive prime numbers.
jamesd78Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deltreeCommented:
Can't you just store them as strings and output them?

Or look at this site, it might have something. http://www.maths.uq.edu.au/~krm/N1.html

2     31
3     37
5     41
7     43
11    47
13    53
17    59
19    61
23    67
29    71

0
_lychee_Commented:
i like the first method ;-)... it smacks of ingenuity...
0
ggilmanCommented:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
void getPrimes(long numPrimes, long *primes);

void main(void)
{
      long primes[20];
      long i;

      getPrimes(20,primes);
      for(i = 0;i<20;i++)
      {
            printf("%d\n",primes[i]);
      }
}
void getPrimes(long numPrimes,long *primes)
{
      long primeCount = 1;
      long curPrime;
      long curNum;
      int isPrime;

      primes[0] = 1;

      curNum = 2;
      while(primeCount < numPrimes)
      {
            isPrime = TRUE;
            for(curPrime = 2;(curPrime < primeCount) && isPrime;curPrime++)
            {
                  if((curNum % curPrime) == 0)
                        isPrime = FALSE;
                  
            }
            if(isPrime)
            {
                  primes[primeCount] = curNum;
                  primeCount++;
            }
            curNum++;
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Redefining Cyber Security w/ AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Join our webinar on Sept. 21st to learn more about leveraging AI and machine learning to protect your business.

ggilmanCommented:
Sorry. Somehow with the copy the tabs got lost.
0
ggilmanCommented:
You could probably optimize this  a little too by having curPrime only going upto curNum/2. Little less checking for the bigger numbers. i.e. if you are checking to see if 71 is a prime, you know you don't have to go above 35.
0
ozoCommented:
1 is not prime.
0
ggilmanCommented:
Isn't it? A prime is a number that isn't divisible by anything but 1 and itself. 1 would fit in that category. I don't have any other books besides my calculus book and they don't talk about primes there.
Either way, the code should still stay pretty much the same. Here is code to get rid of the 1 with a few minor mods to make it more efficient.


#include <stdio.h>
#define TRUE 1
#define FALSE 0
void getPrimes(long numPrimes, long *primes);

void main(void)
{
      long primes[20];
      long i;

      getPrimes(20,primes);
      for(i = 0;i<20;i++)
      {
            printf("%d\n",primes[i]);
      }
}
void getPrimes(long numPrimes,long *primes)
{
      long primeCount = 0;
      long curPrime;
      long curNum;
      int isPrime;

      curNum = 2;
      while(primeCount < numPrimes)
      {
            isPrime = TRUE;
            for(curPrime = 0;(curPrime < primeCount) && isPrime && (curNum/primes[curPrime] >= 2);curPrime++)
            {
                  if((curNum % primes[curPrime]) == 0)
                        isPrime = FALSE;
            }
            if(isPrime)
            {
                  primes[primeCount] = curNum;
                  primeCount++;
            }
                  curNum++;
      }
}

0
ggilmanCommented:
jamesd78, you still there?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Hardware

From novice to tech pro — start learning today.