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.
I am also working through that problem.. may I ask how you put numbers into an empty tuple?
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
@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)
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
or somehow do a similar operation with two lists of odd integers
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
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
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.
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
and then retyping that same statement over and over and changing the 3 with the next prime number
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
@drizzyd - ``` >>> >>> T = (3, 5, 7, 9, 11, 13, 17, 19) >>> T[-1] 19 >>> T[0] 3 >>> T[-2] 17 >>> T[1] 5 >>> ```
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
there is a nice 'table' down at the bottom of this page http://wiki.python.org/moin/MovingToPythonFromOtherLanguages
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
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] >>> ```
python is really annoying
@Alienium i like it
@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
@Alienium bah! to each his own
yeah... i guess so...
@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.
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]
Join our real-time social learning platform and learn together with your friends!