anonymous
  • anonymous
Hey guys! I just started the course. I have no background in programming, but very ready to. I am on problem set one problem one. I have generated odd numbers > 1, but I'm not sure of the code or syntax to "then" find if the number is prime and how to terminate the code at the 1000th prime number. Please help!
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
Hey Jswift - welcome welcome - thanks for the question. It'd be great to see the code you've written so far to help us help you out. http://codepad.org/ is a good site to use - make sure to click 'python' after you paste in your code. Hope this helps!
anonymous
  • anonymous
x = 0 for i in range(10): x = x + 1 if x % 2:
anonymous
  • anonymous
sorry the range should be 10,000. I was just testing the code trying to see what commands I needed to get to the next step. x = 0 for i in range(10000): x = x + 1 if x % 2:

Looking for something else?

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

More answers

anonymous
  • anonymous
I know that I need to divide each odd numbers by all numbers <= the square root of that number, but don't know how to represent that in the code. :/
maitre_kaio
  • maitre_kaio
Try that: do as if you were explaining, in plain english, to someone a bit silly how to know if a number is prime. He should be able to follow your recipe. Post your explaination here.
anonymous
  • anonymous
That's the key, yeah. I just finished figuring it out. Here's how I define the problem: A prime number is divisible by no other integer except 1 or itself. Check all numbers in sequence to see if they are only divisible by one, but not any of the prime numbers you have found so far. The way I kept track of the prime numbers I found so far is a data type called a list. Look it up. You can store prime numbers kind of like this : [2,3,5,7,11,13,17]. Also, you can use it with for loops to do iterations. I won't solve the whole problem, but the end result can include a loop that looks kind of like this : current = 1 primes = [] while len(primes) <10000: for divisor in primes: if current%divisor == 0: .....etc etc etc google the documentation for lists and try it out. it will make your code a lot easier to work with and much more readable if you use list comprehension to do it.
anonymous
  • anonymous
mpeddle, Thanks for your response. I am not understanding where you got "for divisor in primes." Is "divisor" a primitive or one of the key words? Also with lists, I read the section you suggested, but I'm not clear on how I get information to propagate into the list. I assume that it goes with your "while" statement of "while len(primes)<1000:", but your program is very different from where I started so I'm having trouble incorporating the loop in my program. Please help.
anonymous
  • anonymous
Maitre_Kaio, Thank you for your response. I think where I am struggling is with the rules of the language and the explanation of the algorithm. I feel like I can say that I need to find all odd numbers > 1. Then, I need to divide each odd number, x, by all numbers > 1 up to x to see if there is a remainder. If there is a remainder then move to the next number. If there is no remainder then print the number. Mpeddle suggests using "lists" and I think I am understanding them more, but I'm not clear on how to get my answers into a list. In her sample of code she uses "while len(primes) < 10000:" I think this will terminate the code at the 10000th number, but I'm not sure. I'm just looking for a little more direction. Thank you both for your time and help!
maitre_kaio
  • maitre_kaio
Ok I see you understand how to test if a number is a prime by using the trial division method. In you explanation, you even did a bit more work than I asked you. You did not just explain how to test if a number is a prime, but also how you would loop through the numbers to test all odd numbers. Now you should read that: http://www.greenteapress.com/thinkpython/thinkCSpy/html/index.html Chapters 1-8 are especially useful for you now, and you will understand how to translate your ideas into code. If you need more help, keep asking.
anonymous
  • anonymous
Maitre Kaio has the right idea. You should read up on how to use a loop like a for...a little reading would go a long way. To answer your question, divisor is a 'throw away' variable that is instantiated by the for loop. The 'in' key word tell me that there are divisors inside of prime. Prime is a list object that contains prime numbers I have already found. For example, after searching for a bit it might look like [2,3,5,7,11,13]. When I use the for loop, it cycles through each of the numbers in my list of prime numbers and temporarily assigns that number to a variable called 'divisor' which I can use in the block of code run by the for loop.
anonymous
  • anonymous
i would look up the sieve of erastothenes (i think thats spelled right) to find prime numbers. basically, you eliminate all numbers divisible by 2, then 3, 4, 5, 6, and so on into the double digits.

Looking for something else?

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