This is for problem set 1 of the course where one needs to generate primes. I created a list of Odds and now want to go through that list to generate the primes. I'm a bit stumped on that. I know to divide the number with previous numbers in the list but am stuck on syntax. x=100oddList = ()for i in range(1,x): if (i%2) == 1: oddList = oddList + (i, )print oddListprimeList = ()for in in oddList: if (i%(i-1))==0: primeList = primeList + (i,)
Just a correction: oddList = () ###this is a tuple not a list! a list would be: oddList = [] (i know that at this point u probably must use tuples, but i just wanted to specify this difference) plus: for in in oddList: ####should be: for i in oddList: ###typo those for now...working on how to help u best in the meantime:P
Thanks. I'm just trying to get an understanind of this instead of blindly typing in code. My though process is that I want to run through each number in my tuple and divide that by the previous numbers. If any of those return a remainder of 0, then that means it's not a prime and I can move on. The question is how to iterate through the numbers beneath the selected number. Thanks Miguel
btw your code excludes 2 as a prime...2 IS a prime...so think on that too.
what you are trying to do can be done by using a for loop inside a for loop ...but i think it is very complicated....i mean there are other ways to see if a number is prime.
I'm basically going by the problem set task presented here. http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset1a.pdf It says to first make a list of odds, then start sifting through that.
Generate all (odd) integers > 1 u generate 1 aswell
ye ive done it aswell brb 5 mins
x=100 oddTuple = (1,2) for i in range(2,x): if (i%2) == 1: oddTuple = oddTuple + (i, ) print oddTuple
nono your oddtuple has to have odd numbers >1 (bigger than 1) means: (3,5,7,.....) but your primetuple must have 1 and 2
Oh right. Yeah, I see that. Will correct. Overthinking it.
Okay, gotta go. Will check on this later. :) thanks for the help.
"You might think about which integers you need to check as divisors – certainly you don’t need to go beyond the candidate you are checking, but how much sooner can you stop checking?"
you are welcome ask me anytime:)
http://primes.utm.edu/lists/small/1000.txt if u see here your 1000th prime must be 7919, but only if u dont count 1 as a prime ( http://en.wikipedia.org/wiki/Prime_number#Primality_of_one) .(primetuple starts from 2.)
Another thing to keep in mind is that the problem set was due to be turned in before the concepts of tuples and lists had been introduced to the class. When I wrote my code I purposely kept this in mind and avoided using lists which I feel would have allowed for more efficient code. Perhaps approaching it from this angle would give you a fresh perspective of the problem and possible solution.
im pretty sure tuples were introduced..but i can be wrong...
I was just going by the recommended procedure in the assignment. This was my original attempt http://codepad.org/zOOA8ys4
Join our real-time social learning platform and learn together with your friends!