Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

finding prime numbers..

MavrixMavrix Posts: 7Member
Hey all, I'm on an assignment and a bit lost. I'm supposed to write a program that finds the average of all prime numbers between 1 and 100 and prints this average to screen:


I can do it all easily enough, except for finding prime numbers.. I suspect it's something to do with the modulas (remainder) operator but not real sure how to lay it out, any help is appreciated guys & gals. ^^

Comments

  • PruyquePruyque Posts: 149Member
    : Hey all, I'm on an assignment and a bit lost. I'm supposed to write a program that finds the average of all prime numbers between 1 and 100 and prints this average to screen:
    :
    :
    : I can do it all easily enough, except for finding prime numbers.. I suspect it's something to do with the modulas (remainder) operator but not real sure how to lay it out, any help is appreciated guys & gals. ^^
    :

    Here's an easy (and fast) algorithm (pen and paper implementation;) for finding primes up to a certian number:

    [code]
    // I don't know if 1 is a prime number, so we'll skip it ;)
    write down the numbers from 2 to lets say 100.
    put a circle around 2 and cross out all higher multiples of 2 (4, 6, 8 etc)
    while there are numbers without a circle or a cross:
    {
    find the next noncrossed number and put a circle around it, and cross out all higher multiples of this number
    }
    all numbers with circles around them are prime
    [/code]

    good luck,
    Pruyque
  • MavrixMavrix Posts: 7Member
    I never got back to you to say thanks, was able to figure it out. :)



    : : Hey all, I'm on an assignment and a bit lost. I'm supposed to write a program that finds the average of all prime numbers between 1 and 100 and prints this average to screen:
    : :
    : :
    : : I can do it all easily enough, except for finding prime numbers.. I suspect it's something to do with the modulas (remainder) operator but not real sure how to lay it out, any help is appreciated guys & gals. ^^
    : :
    :
    : Here's an easy (and fast) algorithm (pen and paper implementation;) for finding primes up to a certian number:
    :
    : [code]
    : // I don't know if 1 is a prime number, so we'll skip it ;)
    : write down the numbers from 2 to lets say 100.
    : put a circle around 2 and cross out all higher multiples of 2 (4, 6, 8 etc)
    : while there are numbers without a circle or a cross:
    : {
    : find the next noncrossed number and put a circle around it, and cross out all higher multiples of this number
    : }
    : all numbers with circles around them are prime
    : [/code]
    :
    : good luck,
    : Pruyque
    :

Sign In or Register to comment.