Generating prime pairs in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tmitt
    New Member
    • Apr 2008
    • 4

    Generating prime pairs in C

    Hi, I missed a couple classes due to being sick this past week and I am having trouble getting steered in the right direction for my programming assignment. I'd ask the professor for help but he has a total of four hours during the week for office hours, all of which I have a class. But anyways...

    Write a C program that produces prime pairs. At the start, user is prompted to input a positive integer less than 100,000 after which the program prints all the prime pairs in the range 3 through <= N (user input). The program should check the user input to be a value greater than 5 and less than 100,000.

    So an example of the running program would look something like the following...

    {{
    Enter an integer > 5 and < 100,000 and the program will print all the prime pairs up to that value: -4

    The integer must be > 5 and < 100,000: 120000

    The integer must be > 5 and < 100000: 400

    List of prime pairs from 3 to 400

    3 - 5
    5- 7
    11- 13
    17- 19
    . .
    . .
    . .
    . .
    347- 349
    }}

    Now I know that I need to use a ceiling function and I want to do something like...
    i<=(ceil(sqrt(d ouble)n)); i++)

    but, I really don't know where to start, or end for that matter with this program.

    I could use some help, thanks.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Now I know that I need to use a ceiling function and I want to do something like...
    but, I really don't know where to start,
    Those must be some amazing talents if you can tell what arbitrary line of code you need to write before you have even begun...

    But, always try to break down your program into chunks you can manage. Start by breaking down your program into some big, big general steps it must take. Do that. What steps does your program take?

    Let me give you an example. If I write a program that sums integers from 1 to a user specified number, by program steps would be 1. Ask the user what the upper limit is and 2. Add up all the numbers from 1 to that upper limit.

    It sounds kinda pointless to do this, but then when you say you have no idea where to start, this is what I make you do to understand.

    Comment

    • tmitt
      New Member
      • Apr 2008
      • 4

      #3
      I did that.

      I want to prompt the user to enter their number, scan their number, and then I want my function to test it using the ceiling function.

      Now I am not sure if I need to create a prototype beneath the include section and before the int Main function.

      Void ceil(x); is the prototype I used but it seems far fetched to use this. Then after the ceiling function is used and it checks for prime numbers but I also don't think my function is entirely correct.

      I also keep messing up my loops and its frustrating! =)

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        If you've #included <math.h>, you have the ceil() function built in. It'll return an integer.

        Comment

        • tmitt
          New Member
          • Apr 2008
          • 4

          #5
          Okay so no prototype...but I'm still at such a loss for the rest of the program.

          Comment

          • tmitt
            New Member
            • Apr 2008
            • 4

            #6
            To generate from three instead of using a ceiling function I could use n-(n-i) with i set to 3 and i++

            Comment

            • scruggsy
              New Member
              • Mar 2007
              • 147

              #7
              Originally posted by tmitt
              To generate from three instead of using a ceiling function I could use n-(n-i) with i set to 3 and i++
              Stop thinking in C. You're not ready to code until you have an algorithm.
              Instead, write down the steps required to find prime pairs, in general. How would you do it if you didn't have a computer?
              Once you have the steps, you can translate them into a language the computer will understand. If you have trouble there, post your algorithm and your code.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                Instead of musing about the content of your program, why don't you actually sit down and write down various parts of it?

                I want to prompt the user to enter their number
                Why don't you try writing this part up first?

                Comment

                Working...