Please help with ps1! I feel stupid spending so much time on an easy problem, but I'm totally new to programming logic...I get the idea but I think my wording and variable initializations are off. This is what I have so far: n = 0 count = 0 for count in range (1,999): while n in range(2,n^1/2): if x%n == 0: print ("Not prime") else: print ("Prime") count = count + 1 if count == 1000: print ("The 1000th prime number is", n) Thanks!
Can you point us to the particular problems with your code? Syntax errors? Wrong answers? in python, there's no need to initialize count (line 2) when you use a for loop that uses count, since the for loop creates the count variable by itself and then assigns a value to count as it iterates through range(1, 999) in line 4: (while n in range(2, n^1/2)) you have a number of problems. first, the statement you are using as the condition in the while loop, which tests to see if n is inside the list [2, 3, 4, ..., n^0.5] will evaluate to False when you run the program since you've initialized n as 0 (line 1) next, you use the caret operator for whatever reason (I think you intended to find the square root of n). In python (like in other languages, like C) the ^ operator performs a binary XOR operation between two integers (it evaluates to 1 if one and only one of the operands evaluates to true). Example 1^1 = 0 1^0 = 1 0^1 = 1 0^0 = 0 for stuff like 8^3 (which evaluates to 11 in python) : 1000 (8 in binary) 0011 (3 in binary) ---------- vertical XOR 1011 (11 in binary) in python you use the ** operator, or you may import the math library and use the pow() function. so you can either: n**0.5 or: import math (at the top of the program) math.pow(n, 0.5) or: from math import pow pow(n, 0.5) third, you perform division by zero since you've initialized n as 0 (line 1) and then used the modulo operator % (line 5) with n as the divisor. finally, you increment count by one at the end of the for loop (line 8), even though the for loop would do it (by going to the next element in range (1,999)) as soon as it reaches the end of the block.
Also, don't worry about taking too long on simple problems like this one if you are totally new to things like programming or python. I spent over 3 hours on this one. The first few problems are meant to familiarize you with the basics of computing and programming logic. PS0 was just to see if you got everything working and maybe you've learned about the python shell, IO, and the raw_input function. This problem will show you basic problem solving, as well as the real basics in programming like variable declaration, control flow, etc. The following problems will show you even more basics and some python tricks. Over the course you will not only develop programming ability and problem solving intuition, but also your confidence at performing such things. You also learn basic computer science stuff.
Looks like a pretty good first effort. I would say that you are missing any initialization of x above, and as someone else stated, in python exponentiation is done with the ** operator, not ^. So it should be n**(.5), or math.sqrt(n) if you've imported it. Try to break up the problem into different tasks. Start with just the test for primeness. Assume n is the number you want to check: n = 1235741 is_prime = False #write code here that tests if n is prime. If it is prime set is_prime = True. Once you have this working properly (you can test with many different numbers) post the code here and we'll work out the next part which is just looping through numbers until your test is true 1000 times.
Sorry guys, I should have been more specific - the code I posted did not show any errors, but when I run it, nothing shows up. @agdgdgdgwngo - you are so right, I've made a whole bunch of mistakes and will see what happens after I fix those. I appreciate words of encouragement and def. feel better knowing that I'm not alone spending hours on short problems, I guess the beginning of anything is usually hard. @polpak - Thank you, i will try that and post it later! Another thing that got me stumped - how does the program know which number to test (in complete program, I mean), don't I need a user input for that?
Nope, you just have to iterate through each integer checking each one to see if it's prime. We'll do that step as soon as you've got the code above working (per my specification)
Join our real-time social learning platform and learn together with your friends!