A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

hey can anyone help me out with problem set 1, i dont where to begin writting code to print the first 1000th prime numbers....i dont want the answer just a understanding of how u get it

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok, try defining the problem in terms of discrete tasks. The first thing you will need to be able to do is identify if a given number is a prime. Write a small piece of code that takes a variable N and checks to see if N is prime. If you run into trouble, ask here and be sure to use a pastebin site (dpaste.com is a nice one) to supply the code you're working with. Once you can identify if a given number is prime, you simply need to iterate all the natural numbers, checking each of them in turn, and if they are prime print them out until you have printed 1000 numbers.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thnkz alot and thanks for the site...i really appreciate it...i think a bell just rang off as i was thinking, could the code be something like: if n is divisible by only 1 and itself print n, if not continue...i hope it something to that sort. Now i just gotta find a way for the program to keep count of every prime...as u can see im kinda of a newbie so, any more tips would be of help, but thnkz already

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hi I think you're on the right track... just realized that thinking in discrete tasks isn't easy but it's definitely a good way to approach a problem. hmmm... well... one of the ps1 suggestions was to ensure that the numbers that you use to divide and test the prime-candidate should be as limited as possible. so, instead of dividing from 2 to n, one of the earlier threads talked about looking at http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. I haven't tried using it yet but it seems pretty good.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    write a module which will can test a given number being prime or not... checking prime number is fairly easy....i'm writing in c, so that you have to rewrite it in python bool is_prime(unsigned int num){ for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0) return true; else return false; } next you have to start from 1 and check every number by passing that number into your prime number checker module, like this... unsigned int count = 0,i = 1; while(count < 1000){ if(is_prime(i)) count++; printf("%d",i); i++; }

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thnkz alot everyone, wen i finally get the code ill post it

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.