Can some one help me with pset 1 problem 1

- anonymous

Can some one help me with pset 1 problem 1

- Stacey Warren - Expert brainly.com

Hey! We 've verified this expert answer for you, click below to unlock the details :)

- schrodinger

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

- anonymous

biggest problem I am having is how to keep using modulus (%) with prime numbers. So for example 21%2 != 0, 21%3 !=0 but 21%7 == 0...As the numbers get bigger I can't rely on doing that code over and over... I need a more effcient method

- anonymous

could we see your code?

- anonymous

could we see your code?

Looking for something else?

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

## More answers

- anonymous

use dpaste.com - paste your code in the code window, choose Python for the syntax and click the paste it button - post the link to that here

- anonymous

Hi guys, thanks for your response, here is the code http://dpaste.com/534549/
it isn't totally complete but you can see my thought process... x % 2... x % 3... soon x % 5 etc etc I know I need to do x % (each prime number i find) in order to do this correctly, but I am not sure how to create a while loop that will allow me to do that.
All the examples I have seen so far are using for loops and ranges...which we haven't learnt yet, is that the only way?

- anonymous

the while statement starts a loop. you correctly realized that you need another loop - one that is 'inside' of the while statement. A for 'loop' would work but you could probably do it with a while statement also.
what do you think you need to do with the 'inner' loop?

- anonymous

i need the inner loop to increase what I am dividing by so x % z, I need Z to represent all of the prime numbers I am storing as I do the outer loop but I think it gets tricky because z should be < than x. So I need to think of a while condition that will allow me to do the inner loop, while ( z < x ) maybe?

- anonymous

yep - so in the outer loop you are starting at 3 and adding one on each 'loop', And the inner loop basically needs to do the same thing with the divisor.

- anonymous

and then if at ANY point x % z == 0 the inner loop should break and go back to the outer loop

- anonymous

that would work but somehow you need to keep track of whether or not the 'candidate' ended up being prime. break is a keyword that will break out of a loop.
why don't you revise your code with what you've got so far then look at it.
by the way - what we are doing when we are writing down in english what we want the program to do is kinda like writing pseudo-code which is always a good place to start tackling a problem

- anonymous

Think about what you're actually trying to do. You want a counter to keep track of the primes you've found which goes to 1000. You are working with a number to be tested (which you've labeled x) and a divisor (which you've labeled y). A prime number can only be divided by 1 and itself, so if you get a remainder with your first divisor you can move on and try and divide your test number (x) by the next natural number which follows your first divisor. If you keep increasing your divisor by 1 and your test number is prime you will eventually get a remainder of 0. If this occurs when the divisor is equal to the number being tested, then you've found a prime number. Don't forget to increase your counter. If you get a remainder of 0 when the divisor is NOT equal to the test number, the number being tested was not prime. Either way, you need to test another number, so increase x by 1 and repeat the same process. Once you counter has reached 1000 you're done.
Hope that helps. I find it helpful to try and explain what I want my code to do in English.

- anonymous

:)

- anonymous

haha its funny you both mentioned that, I will give it a shot thank you :)

- anonymous

have you revised your code and added the inner loop? if so paste and post it

- anonymous

yea, getting owned though... for some reason its not working http://dpaste.com/534556/

- anonymous

i added y = 2 under the 1st while loop, but its still not really looping

- anonymous

hmmm.. ok earlier i thought you said your inner loop would make divisors up to the number you are testing and then break out of the loop if you found it wasn't prime. - it doesn't look like you did that.
you actually changed a lot -
i thought you were going to do something like
while count < 1001
while divisor < x
test if x can be divided by divisor
increment divisor

- anonymous

ya i started trying to do what matt was talking about, but even if i do a super basic nested while loop it doesnt work...so my knowledge of nested while loops is very wrong
while count < 1001 :
print "This is the first count"
z = z - 1
while z > 0 :
"This is the 2nd loop"
count += 1
doesn't even print 'This is the 2nd loop'

- anonymous

one way to help figure out what is really happening when your code runs is to put print statements in.
I think you might have your tests wrong - if the number you are testing is actually prime then lines 7-9 will run forever - z will always be greater than 0 for every value of y

- anonymous

Try and write the code based on the following:
While the count is below 1000, if there is a remainder when x/y then increase y by 1.
While the count is below 1000, if there is no remainder and x=y, increase your count and x by one, while making sure to reset y to its original value.
While the count is below 1000, if there is no remainder and x != y, increase x by 1, reset the divisor to its original value and do NOT increase the count.
Finally, print the value of x which raises the counter to 1000.

- anonymous

add this after line 7
print "x:", x, "y:", "z:", z
you can stop it from running by typing ctrl-z - then you can scroll back and see what happened.

- anonymous

whoops - ctrl-c that is NOT ctrl-z

- anonymous

a while statement will continue to loop as long as its condition is True

- anonymous

x: 3 y: z: 1

- anonymous

it keeps spamming that over and over lol

- anonymous

big lesson learned my friends when doing if statements if remainder == 0 & div = x
is not the same as remainder == 0 and div = x :( & != and !!!!

- anonymous

if anyone runs into problems with this pset please!! watch video 3 lol, he goes over for loops, ranges, and collecting values to add to tuples

Looking for something else?

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