Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 21 Online
OpenStudy (anonymous):

I have a question about problem set 1 problem 1. I have written a program that can generate prime numbers. My initial thought was to define an empty tuple, find prime numbers and put them into the empty tuple, which I have done. However, I cannot seem to write the code properly to select the 1000th element of the tuple in my program. can someone help me with the code.

OpenStudy (anonymous):

I am also working through that problem.. may I ask how you put numbers into an empty tuple?

OpenStudy (harsimran_hs4):

you can keep track of the length of the tuple , e.g while(len(tuple) != 1000) proceed to find prime numbers PS : lists will be better in this case

OpenStudy (harsimran_hs4):

@jkpalmer94 you cannot put numbers into a tuple but you will have to form a new one e.g a = (1,2) you cannot append a element into a tuple, but you can a = a + (3,) # now a is (1,2,3)

OpenStudy (anonymous):

if i have 2 tuples or even if i have 2 lists of all odd integers up to 7919.. assuming they are named tupleofoddintegers1 and tupleofoddintegers2 could i write code like this... tupleofoddintegers1 = tupleofoddintegers1%tupleofoddintegers2 != 0

OpenStudy (anonymous):

or somehow do a similar operation with two lists of odd integers

OpenStudy (harsimran_hs4):

no % operation is not defined for tuples or list so this is invalid can you tell what do you want to do by this line of code so that i can help better

OpenStudy (anonymous):

I just realized my initial request makes absolutely no sense haha. What I am trying to do is take a list or tuple of all odd integers and weed out any none prime numbers in order to ultimately find the 1000th prime number

OpenStudy (harsimran_hs4):

this approach is not good due to the following reasons : 1.) you don`t know upto which odd number you need to fill the list/tuple 2.) i have not seen any method or built in function that can handle the procedure to delete/remove non primes in one step so the best you can do is to start from first prime no and keep track of the prime numbers encountered subsequently count = 0 prime = [ ] #just in case you need all the prime nos upto 1000th num = 2 while(count != 1000) if num is prime: prime.append(num) count += 1 num += 1 this is the basic procedure , with no concentration on optimization.

OpenStudy (anonymous):

thank you that will definitely give me an organized list however I am still having trouble finding all prime numbers other than making if statements like If x%3 != 0

OpenStudy (anonymous):

and then retyping that same statement over and over and changing the 3 with the next prime number

OpenStudy (anonymous):

this is my closest code thus far http://dpaste.com/1355099/

OpenStudy (harsimran_hs4):

Q : what is a prime number ? A : any number which is not divisible by any number other than 1 and the number itself Q : how can this be applied ? A: you will need a loop to check if any number is prime http://dpaste.com/1355151/ your code is not a good design decision (so i did not check if it`s right or not), you simply guess that from the number of nested if statements

OpenStudy (anonymous):

@drizzyd - ``` >>> >>> T = (3, 5, 7, 9, 11, 13, 17, 19) >>> T[-1] 19 >>> T[0] 3 >>> T[-2] 17 >>> T[1] 5 >>> ```

OpenStudy (anonymous):

In python, elements/items of a sequence data type can be accessed with slice notation. Tuples are sequences. Here is an explanation in the Tutorial - it uses strings which are also sequences: http://docs.python.org/2.7/tutorial/introduction.html#strings

OpenStudy (anonymous):

there is a nice 'table' down at the bottom of this page http://wiki.python.org/moin/MovingToPythonFromOtherLanguages

OpenStudy (anonymous):

hi this was my method so far prime = () for n in range (2, 10000): for x in range(2, n): if n % x == 0: break else: prime = prime + (n,) just not sure how to get prime[1000] out. May not be the optimal way to calculate all prime numbers but it seems to work. Thanks everyone for your replies

OpenStudy (anonymous):

you want to execute the inner loop until a condition has been met. the condition is that you have found 1000 primes that sounds like a while loop ``` >>> things = [] >>> n = 1 >>> while len(things) < 1000: things.append(n) n += 1 >>> things[-1] 1000 >>> len(things) 1000 >>> things[-5:] [996, 997, 998, 999, 1000] >>> ```

OpenStudy (alienium):

python is really annoying

OpenStudy (anonymous):

@Alienium i like it

OpenStudy (alienium):

@bwCA but it's sooo loosly typed and that indentation it's soo confusing, i want code inside brackets and an int to be an int not dynamicly change to a string or smth

OpenStudy (anonymous):

@Alienium bah! to each his own

OpenStudy (alienium):

yeah... i guess so...

OpenStudy (anonymous):

@drizzyd How is your code finding primes? It's only checking to see if the numbers are divisible by 2. Just because it isn't divisible by 2 doesn't mean it's prime.

OpenStudy (anonymous):

This is how I would solve this problem, using a Sieve to remove Non Primes quickly and effeciently. def primes_sieve(limit): '''Generate all primes up to a limit.''' # Creates a list of bools equal to the len(limit) a = [True] * limit # Set the first two elements of 'a' equal to False. (0,1) a[0] = a[1] = False # Enumerating a list, is like running iteritems on a dictionary. # (i,isprime) will for example return: (2,True),(3,True),etc... for (i,isprime) in enumerate(a): # Checks if the value is True if isprime: # Yield is like 'return' for a generator, which enumerate is. yield i # First, we go through the list and set all values divisible # by 2, equal to False. if i == 2: for n in xrange(i,limit,i): a[n] = False # Now we can step by 2*i # Now we remove all multiples of 'n'. Removing them by stepping # 2*i, because stepping by i would iterate over even values # that have already been removed. for n in range(i*i,limit,2*i): a[n] = False print list(primes_sieve(1000000))[10000]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!