# algorithm

This is a discussion on algorithm within the C Programming forums, part of the General Programming Boards category; [/code] /************************************************** Filename: main.c Author: Mike Hartwig Purpouse: Generate given amount of prime numbers. Input: Keyboard Output: Screen ************************************************** */ ...

1. [/code]
/**************************************************
Filename: main.c
Author: Mike Hartwig
Purpouse: Generate given amount of prime numbers.
Input: Keyboard
Output: Screen
************************************************** */
#include <stdio.h> /* Pre-processor*/
#include <stdlib.h> /* */
#include <math.h> /*Directives */

int main ()
{
int prime_amount; //the amount of primes to generate.
int totcount;

printf("Welcome to the Prime Number Generator.\n");
printf("How many prime numbers would you like to generate: ");

if (prime_amount < 0) //dont print any primes if given amount is less than 0
{
printf("Please Enter A Number Greater Than 0\n");
}

else
{
printf("Generating %d prime numbers...\n", prime_amount);
}

if (prime_amount > 4)
{
printf("2 3 5 7 ");
}

do
{

if (prime_answer %2 != 0 && prime_answer % 3 != 0 && prime_answer % 5 != 0 && prime_answer % 7 != 0)
{
}

if (prime_amount == 1)
{
printf("2");
}
if (prime_amount == 2)
{
printf("2 3 ");
}
if (prime_amount == 3)
{
printf("2 3 5 ");
}
if (prime_amount == 4)
{
printf("2 3 5 7 ");
}
}

while (prime_answer <= prime_amount); //stop printing primes when hit prime_amount

return (0); //all done
}
[code]

There is only one thing I need help with. It is supposed to print a given amount of primes, not until it gets to the given number. IE. Say the user puts in 100, right now it will stop at like 97 or something, but its supposed to print 100 of them. Im not to good at programming, and am just trying to get by in the class. Im about to give up. I dont know why I am having sutch a hard time.

2. ops

3. Ok I got this finally. It works.

Code:
```/**************************************************
Filename: main.c
Author: Mike Hartwig
Purpouse: Generate given amount of prime numbers.
Input: Keyboard
Output: Screen
***************************************************/
#include <stdio.h> /* Pre-processor */
#include <math.h> /*   Directives	*/

int main ()
{
int x = 0; //the loop variable
int y = 0; //the prime amount variable
int y_prim = 0; //the answer

printf("Welcome to the Prime Number Generator.\n");
printf("How Many Primes To Generate: ");
scanf("%d", &y ); //How many primes to generate

do
{
x++; //increment x
while (y_prim < 1000000)
{
y_prim++; //increment y_prim

if ((y_prim % 2 != 0 || y_prim == 2) && (y_prim % 3 != 0 || y_prim == 3) && (y_prim % 5 != 0 || y_prim == 5) && (y_prim % 7 != 0 || y_prim == 7) && y_prim != 1) //filter out multiples of 2, 3, 5, & 7
{
printf("%d ", y_prim);
break; //break, and go to outer loop
}
else
{
continue;
}
}
} while (x < y); //

return (0);// all done
}```

4. @dharh:
#define MAX 20 /* number of primes you want */
Thatīs incorrect. You could do it in the following way:
Code:
```#include <stdio.h>

#define N 100000
#define MAX 20  /* number of primes you want */

int main(void) {
char p[N];
unsigned int max=0; /* counts the number of outputed primes */
register unsigned long i;
unsigned long j;

for(i=1;i<N;p[i]=1, i+=2) ;

for(i=2, p[1]=0;j<N/2;i++)
if(p[i]) for(j=2;j<N/i;j++) p[i*j]=0;

for(i=1;max<MAX;i+=2)
{
if(p[i]) printf("%lu\n", i);
max++; /* one more primenumber */
}

return 0;
}```
klausi

5. Oops, I didn't look at your code close enough.

6. Unreg,

Your code is still not general enough. You must work on a function that determines primes.

That is, a block of code that takes an integer as an argument (input) and outputs prime or not prime at the end. Think of it as a prime number calculator. Push the button and out comes the answer.

Code:
`if ((y_prim % 2 != 0 || y_prim == 2) && (y_prim % 3 != 0 || y_prim == 3) && (y_prim % 5 != 0 || y_prim == 5) && (y_prim % 7 != 0 || y_prim == 7) && y_prim != 1) //filter out multiples of 2, 3, 5, & 7`
This is SPECIFIC. Need general. What about multiples of 11, 13, 17 ect ?

Take one of the examples from myself or Barjor's (is clearly commented). You replace the line above with

Code:
`if(prime(y_prime)==1) // is prime`
and add in Barjor's prime() funtion above your code will work with all numbers.

Page 2 of 2 First 12