How is to check prime number?

This is a discussion on How is to check prime number? within the C Programming forums, part of the General Programming Boards category; I need to write to program to list out all prime number from 1 to 100. How to check each ...

  1. #1
    Unregistered
    Guest

    Question How is to check prime number?

    I need to write to program to list out all prime number from 1 to 100. How to check each number where it is prime or not? give me some advice. Thanks

    srk

  2. #2
    Unregistered
    Guest
    /*return 1 if prime */
    int isprime(int i)
    {
    int j;
    i=(i>0)?(i) : (-i);
    if(i<=1) return 0;
    if(i<=3) return 1;
    if( (i&1)==0 ) return 0;
    for(j=3;j*j<=i;j+=2)
    if( (i/j)*j == i) return 0;
    return 1;
    }

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I would use the % operator. It returns the remainder ( 10%3=1 ). A prime number will never return a 0 remainder if divided by a number smaller than itself (except 1).

    Maybe create an array and test each value, 1 to 100, recording the result in the aray for the later use (display).

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    They are searching for a large mersenne prime harnessing the combined power of many small computers , there is even a reward

    http://www.mersenne.org/prime.htm

  5. #5
    Unregistered
    Guest

    Fairly Easy

    It was the first assignement I received. I have no compiler to test it thought, it might contain mistakes.

    ANSI C

    Code:
    #include <stdio.h>
    
    int is_prime(int number, int primed)
    {
    for (number=1; number < 100; number++)
    {
       if ((number%primed)!=0)
       return 1;
    }
    return 0;
    }
    
    int main()
    {
    int i;
    for (i=1; i<100; i++)
    {
    if (is_prime(i)==0)
    printf("%d\n",i);
    }
    return 0;
    }

  6. #6
    Unregistered
    Guest
    Thank you!!!

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Little problem in the IS-Prime function. Two var in prototype but one in call.

    Also all numbers are divisible by 1 so start the loop at two.

    Try this;

    int is_prime(int number)
    {
    for (primed=2; primed < number; primed++)
    {
    if ((number%primed)==0)
    return 0;// no remainder so is not prime
    }
    return 1;//all tested and all have remainder so is prime
    }

  8. #8
    Registered User Defined's Avatar
    Join Date
    Oct 2001
    Posts
    2

    Talking TX

    All my appologizes, thanks Novacain.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Replies: 6
    Last Post: 11-04-2002, 04:11 PM
  4. prime # query
    By alokin in forum C Programming
    Replies: 8
    Last Post: 09-04-2002, 11:50 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

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