prime no's

This is a discussion on prime no's within the C Programming forums, part of the General Programming Boards category; Hi!.This is my basic program to display Prime no's from 2 to 100. I tried for weeks to get the ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Question prime no's

    Hi!.This is my basic program to display Prime no's from 2 to 100.
    I tried for weeks to get the algorithm correct. Still the code does have some problems. It prints all the numbers from 2 to 200.
    Glad if someone cud help to solve my problem.

    *************************code********************* ******************
    Code:
    /* All prime numbers between 2 to 100 */
    
    #include<stdio.h>
    	
    	void main()
    	{
    
    	int num,i=2,rem;
    	for(num=2;num<100;num++)
    
    	{
    
    		while(i<num)
    		{
    
    		rem=num%i;
    		i++;
    		if(rem!=0) break;
    
    		}
    
    
    	if(rem!=0)
    	printf("%d\t",num);
    
    	}
    
    }
    
    *********************************************************************
    [code tags added by ygfperson]

    Thanks
    Mur

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Search the board. This gets posted every week. No, wait, it gets posted every day. See down about ten posts?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: prime no's

    Originally posted by mur3
    void main
    /me cringes in fear

    does it bite?
    hello, internet!

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Q) What is a prime number?
    A) A prime number is a number with the factors of only one and itself.

    Q) What is the simplest way to find prime numbers on a computer?
    A) Use a for loop and go through every number until the square root of the number in question. (Half that number would work fine as well). Check each number to determine if it is a factor.
    Code:
    if (number_in_question % possible_factor == 0) ...
    If so, then you know for sure that your number is not prime.


    You're over-thinking the problem.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Furthermore, you can cut your list in half by simply ignoring everything that is even numbered. Thus:

    for( x = 1; x < 100; x += 2 )

    This skips every even number.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    A simple search would of solved this problem. Does anyone ever use that I wonder.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    thanks guys..
    really appreciate it..



    Mur..

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Then there is the most reasonable solution:
    Code:
    #include <stdio.h>
    
    #define TABLE_SIZE(table) sizeof(table) / sizeof(table[0])
    
    static const int prime_table[] = {
      /* Empty element */0,
      2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
      43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 
    };
    
    int main ( void )
    {
      int index;
      puts ( "Prime numbers from 2 to 100:" );
      for ( index = 1; index < TABLE_SIZE ( prime_table ); index++ )
        printf ( "%-3d%c", prime_table[index], ( index % 5 == 0 ) ? '\n' : ' ' );
      return 0;
    }
    -Prelude
    Last edited by Prelude; 09-05-2002 at 09:42 AM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 09:36 AM
  3. Prime Nos
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-17-2004, 01:14 PM
  4. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

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