Hi guys, I'm in the making of the primefinder (assignment 1) and I have made this http://dpaste.com/777261/ but I'm getting an error i don't understand http://dpaste.com/777263/ can someone point me in the right direction?
You can't have more than one variable in a for loop, you are using both 'n' and 'p'. Your program will need to find every prime number between 1 and 1000 so you your loop's stop condition needs to reflect this range.
Thanks, I started from scratch since I posted this question and my code now looks like this, http://dpaste.com/777335/ but I am still getting error http://dpaste.com/777334/ I looked in docs and as far as I can see (please correct me if I'm wrong) here http://docs.python.org/tutorial/datastructures.html#looping-techniques it should be possible to use two variables if you use the zip function?
check_int = 3 primes_found = 1 primes = [2] while primes_found <= 1000: This isn't a good way to approach this, and you always need to make sure to increment the first operator of a while loop, in this case 'primes_found' is always 1 so the loop is infinite. if check_int % 2 == 0: The definition of a prime number is one that can only be divided by 1 and itself, so i'm not sure what you are attempting to do by dividing by 2. check_int += 1 else: for p, n in zip (primes, check_int): I don't understand your approach with this loop. pl_pos = 0 pl_len = len(p) I'd recommend that you make a for loop inside a for loop, the first loop will count from 1 to 1000, and the second loop will divide the progress of the first loop by each number between it and 1. So if your first loop is "n" which will always be between 1 and 1000, your second loop will divide n by each number between 1 and n, if any of these have a remainder of 0 (other than 1 and n), it's not a prime number. If the only remainder of 0 are from 1 and n, then it's a prime number.
The code is not half finished yet, I know. But basicaly the point is that by if check_int % 2 == 0: check_int += 1 I can rule out all the number that are divisable by 2 witch is half of all numbers, so its basicaly to make it work faster. Then I would have for p , n in zip(primes, check_int): go to the list primes and divide check_int with the primes already found (since all numbers are made up of smaler primes, exept primes). Then if check_int is divisable by a number in primes increment check_int by 1 and repeat if check_int is not divisable by any number in primes then ad check_int to the list primes increment primes_found by 1 print check_int increment check_int by 1 rinse repeat Thats the gist of my plan, but I'm kinda stuck on that error...
Oh, and pl_pos and pl_len is for getting and giving coordinates in the list primes
check_int is a scalar value, you need to use two lists or tuples for zip.
Also, the zip function doesn't seem like the best approach to this problem.
Ok, but as far as I know (witch is obviously not verry far yet) it's not posible to change values in a list (only to delete and then put in a new value, witch seems counter intuitive) so could I solve that by doing something like this check_int = 2 check_int_list = [check_int] ? And what would you suggest instead of zip?
You won't be able to compare each number with your list because you can't reference each character of an integer. If i understand you, you want to take a number (say 251) and check to see if 2, 5, and 1 are on the prime number list already... if they are, you will consider it prime. This seems like a long way to approach this problem, but to accomplish this you will need to convert your check_int value to a string, which can be referenced digit by digit... so.... for n in range(0,len(str(check_int))): this will convert check_int to a string, so "251" and return the length of the string, 3. It will then run the loop 3 times, where you can check each of these against your other list of found prime numbers.
Ok, so I was coding a bit and it has come to this now http://dpaste.com/777365/ I'm getting a new error http://dpaste.com/777366/ but I'm honestly too tired to code think right now... Input is still much appreciated :D but I really need some rest :)
the pattern x, y = [1, 2] is called unpacking (in Python at least) - it unpacks the items in the sequence on the left into the variables on the left http://dpaste.com/777527/ you are getting an error because you have unpacked the pair (that you created with the zip function, line 8) into two separate variables - p and n - then you are trying to index into the variable p like it is a matrix when it is actually just an integer - the traceback gives you a hint that you are trying to do something with/to an integer that is not possible. print statements in your code help show you what is going on and are a good debugging tool. here is your code with a print statements showing you what p and n are http://dpaste.com/777533/ it helps to write down in words what you are trying to do then translate that into code' you should write down in words the steps you need to perform to test if a number is prime then write down in words the steps you need to perform to test a sequencew of numbers for primeness til you find 1000 of them.
Thanks, that was actually very helpful :D I've got it runing now exept a loop and cant get over, but I'll fix that as soon as I have read some more up on loops :D
Hey im only just finishing unit 1 - just wondering when in the course assignment 1 is assigned? thanks
Join our real-time social learning platform and learn together with your friends!