Thread: Prime numbers

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    26

    Question Prime numbers

    I am required to write a program which writes all the prime numbers between 1 and 10000, my only problem is that I cant find a way to determine the prime numbers from the rest! plz help! im not good at maths!

    I wrote the following:

    #include <stdio.h>
    int prime (int);
    int main ()
    {
    int i;
    prime (i);
    return 0;
    }

    int prime (int i)
    {
    i=1;
    while ( i <=1000) {

    if ( i % 2 == 1 && i % 3 == 1)
    printf ("%d\n",i);
    i++;
    }
    return i;
    }
    Last edited by Lazy Student; 05-13-2002 at 03:18 PM.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I assume you know what a prime number is, from the definition of a prime number it should be quite easy to write an algorithm for that. Unless you're a Real Lazy Student.

    A little hint:

    Code:
    for n = 1 to 10000 do
    begin
        for i = 2 to n - 1
            if (n mod i = 0)
                n_is_prime = false;
    end;
    There are more efficient ways, hint: think about how the square root of n could make the algorithm more efficient. But this one is easy to understand, isn't it?

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Plus: All even numbers are divisible by 2.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    129
    10.000... your from europe right?
    flashdaddee.com rocks!!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    Plus: All even numbers are divisible by 2.
    LMAO. Guess what! Every fifth number is divisible by 5!

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

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    Thank you all!
    10.000... your from europe right?
    no, im not from europe! i wont tell you where im from!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    129
    well, your not from the US (I dont mean that in an ******* kind of way, just the way you did your number 10.000 not 10,000).
    flashdaddee.com rocks!!!

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    thats because of my stupid keyboard! i cant recognize the comma from the point!

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by quzah


    LMAO. Guess what! Every fifth number is divisible by 5!

    Quzah.
    What I meant was he doesn't have to check divisibility with every number between 1 and sqrt(n), just the uneven ones (except for 2). That cuts the checks by 50% which will make a great difference on huge numbers.
    So please be careful with your flames in the future
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    LMAO. Guess what! Every fifth number is divisible by 5!
    only those in the series 0,5,10...
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    129
    fenix is right, it would be faster.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Windows XP consists of 32 bit extensions and a graphical shell
    > for a 16 bit patch to an 8 bit operating system originally coded
    > for a 4 bit microprocessor, written by a 2 bit company, that
    > can't stand 1 bit of competition.

    I've heard this before, applied to Win9x. However, this is inaccurate for WinXP. Windows XP is not an extension of an 8 or 16bit OS at all. WinXP is based off of WinNT. Windows NT is NOT a 16bit OS.

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

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    28
    I had to do something similar. maybe it will help you:

    int number=1;

    while(number<=10000){
    if (number<test*2)
    {
    printf("%4d",number);
    i++; ref++; test=2;
    }
    else
    if (number%test==0)
    {
    number++;
    test=2;
    }
    else
    test++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. More Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 3
    Last Post: 02-21-2003, 10:06 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM