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
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.
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
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.
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++; }
thnkz alot everyone, wen i finally get the code ill post it
Join our real-time social learning platform and learn together with your friends!