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. #16
    Unregistered
    Guest

    Unhappy

    [/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 prime_answer = 3; // the answer.
    int totcount;

    printf("Welcome to the Prime Number Generator.\n");
    printf("How many prime numbers would you like to generate: ");
    scanf("%d", &prime_amount); //asking for Prime_amount

    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
    {
    prime_answer++; //increment prime_answer

    if (prime_answer %2 != 0 && prime_answer % 3 != 0 && prime_answer % 5 != 0 && prime_answer % 7 != 0)
    {
    printf("%d ", prime_answer);
    }

    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. #17
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    ops

  3. #18
    Unregistered
    Guest
    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. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    @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
    When I close my eyes nobody can see me...

  5. #20
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Oops, I didn't look at your code close enough.

  6. #21
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    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.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21