Help with prime numbers

This is a discussion on Help with prime numbers within the C++ Programming forums, part of the General Programming Boards category; can someone please write a small programs showing how to find if the number you entered is prime.If the number ...

  1. #1
    Clean Killa
    Guest

    Unhappy Help with prime numbers

    can someone please write a small programs showing how to find if the number you entered is prime.If the number entered is not prime it will tell you the next highest prime number. Thanks in advance.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well no I can't. I was going to give you a program that generates prime numbers but I can't find in on my computer Sorry.

    But you just do a search. Once you have a list of primes you can make a lookup table in order to accomplish the task you desire.

  3. #3
    Clean Killa
    Guest

    Unhappy

    Thanks for the help man....anyone else please i need this to help pass my college class. Thanks again

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >anyone else please i need this to help pass my college class.
    No, do your own homework. And before you post again, be sure to read this.

    -Prelude
    My best code is written with the delete key.

  5. #5
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    this is a homework problem, and the people on this board dont take too kindly to doing other people's homework.

    but i will help guide you on the path to enlightenment. you will have to write the code yourself.

    let us forget about code for a moment and consider the definition of a prime number. a prime number is a number divisible only by 1 and itself.

    now, let us say that we have been given a number as input. how would we determine if it is a prime number.

    how about we just throw away the number 2, since it is divisible by 1 and every number in between itself and 1. 2 will always be prime, so:

    if ( "input" == 2 ) cout << "the number is prime";

    ok, that is taken care of. now, as for every number above 2. well, a number will never be divisible by over half of itself, so we will start there.

    ( pseudocode: )
    is "input" % ( "input" / 2 ) equal to 0? if so, then its not prime. (dont worry about odd numbers. as long as you are using ints, it will round down)

    now we put ("input" / 2 ) into a variable, say "x"

    we now go down the line:

    "input" % x = ?
    "input" % x - 1 = ?
    "input" % x - 2 = ?
    "input" % x - 3 = ?

    all the way until x = 2. if the answer to any of the above is ever 0 then it is not a prime number. if it gets all the way to the bottom of the list without getting a 0,then it is a prime number.

    and there you have it. a program to check for primes.

    as far as the next highest prime: its a pretty simple solution, so dont get overwhelmed. but you are going to have to figure it out yourself. use the method described above. dont think about the code until you know how you want the problem solved in plain english.
    Last edited by ...; 11-14-2002 at 09:21 AM.
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    Clean Killa
    Guest
    thx i hope this works

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, 06: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. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 06:53 AM
  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