In ps1, what do they mean with 'generate all odd integer greater than 1.' I get that if you start with odd_number = 3, and then update odd_number = odd_number + 2 will get you the next odd integer. Is that generating ALL odd integers? It really is just generating the next odd integer. But there are infinite odd integers. Am I looking at this wrong..?
No you're pretty much right. But you don't have to generate them forever: you already know where you have to stop (when you have the answers you're looking for). So the point is to write code that generates integers infinitely UNTIL your code tells it to stop (because it performed a test that passed, telling it it's done). Here's a sample piece of code that explains this clearly: derp = 0 while derp != 10: derp += 1 print derp In this example, you see that, were the test (is derp 10 yet?) never to pass, you really WOULD generate every integer ever, because this loop would never stop. But because of that test, your code knows when to stop, so it's only going to keep doing it until that point. Think of it this way: you have a coding toolbox, full of various tools. Loops are one of those tools. They are a way for you to tell the computer to do something repeatedly until you tell it to stop, whether it's "increase this number by 1 until ____" or "print this word until ____", or something way more complex than that. Maybe this diagram will help break down while loops for you: while (when do you want it to stop? put that test here): what do you want it to do until it stops? put that code here
The idea of the problem set is this. You need to find the 1000th prime number from the set of natural numbers. You don't have to check the even numbers because we already know that except for 2 they are not primes. So, we have to check each of the odd numbers until we reach the 1000th prime. The prime numbers, except for the number 2, are a subset of the odd numbers.
in essence you have to create a nested loop, your first loop is in creating odd numbers, your second loop is one that checks each odd number to see if it is prime. It also has to have a counter so that the program knows not to continue past 1000 primes. Once the correct number of primes are generated, the program must know to terminate the loop on it's own.
This got the right answer. Is this the most efficient way of getting the answer.
Join our real-time social learning platform and learn together with your friends!