In the 2nd assignment, one of the tips to generate the prime numbers was to generate all odd integers >1. Where can I store the generated list? Aren't we only allowed to use conditional statements, loops and variables?
Instead of generating all odd integers, I think most people tend to only test negative integers by starting with 3 and then increasing by 2 each time. That way you only test odd numbers, but you don't have to generate a list of odd numbers at the beginning.
You have several questions rolled into one. Let's take each question in turn. 1) "Aren't we only allowed to use conditional statements, loops and variables?" You are not restricted to any particular technique as long as you solve the problem. 2) The use of the word "generate" is unfortunate. The purpose of the exercise is to check integers for primality. Since we know that the even numbers are all composite (divisible by 2) we can with one fell swoop eliminate from the test. By "generating" prime numbers the mean only check the odd numbers for primality. You can do that using whatever technique. 3) "Where can I store the generated list?" For this assignment strictly speaking you don't need to store items in a list to solve the problem. However, for troubleshooting purposes and to verify that you are coming up the right results it's helpful to build that into your program initially. Let's say that you want to store the primes as you cycle through the set of odd numbers. You could a) declare an empty list called primes; b) append to that list as you come across a prime number; primes=[ ] the loop in your program which checks to see if num is indeed a prime number; if it is append it the list that stores the set of prime numbers primes.append(num)
I believe the short answer is that generating a list is not necessary. a brute force method may simply involve taking a number "x", and then attempting to divide by an increasing number "y". You can increase the value of "y" with a loop. You can increase the value of "x" with another loop. You needn't store all of these values you are generating. For some faster methods that may take advantage of concepts that are introduced later, you may see the discussion here: http://openstudy.com/study?F1244392739431MUOV2U=_#/updates/4f1241dbe4b0a4c24f583004
Join our real-time social learning platform and learn together with your friends!