A community for students.
Here's the question you clicked on:
 0 viewing
wmhs14
 3 years ago
In Problem Set 1, the statement "Generate all (odd) integers > 1 as candidates to be prime" is confusing. I understand how to generate an odd number, but all the examples described in the readings and lectures have boundary conditions (or a stopping point). I know 1000 th prime is my goal, so does my range stop at 7919? Or is there an actual way to generate every odd integer > 1?
wmhs14
 3 years ago
In Problem Set 1, the statement "Generate all (odd) integers > 1 as candidates to be prime" is confusing. I understand how to generate an odd number, but all the examples described in the readings and lectures have boundary conditions (or a stopping point). I know 1000 th prime is my goal, so does my range stop at 7919? Or is there an actual way to generate every odd integer > 1?

This Question is Open

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1as far as i get it you are not able to fix the upper bound in looking for the 1000th prime ... well what you can do is instead of a for loop use while loop this way.... make a empty list and append in it the prime you encounter and for moving into the while loop place a condition while len(list) <= 1000: ........... tried my best to explain if still any problem please do ask....

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1yes i believe @harsimran_hs4 is right...just count every prime u find and stop on the 1000th. Just remember that 2 is a prime so it has to be in your list maybe from start...

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0Thank you for your help, but I'm a little lost. I'm having trouble making the connection between the outer loop nth prime and inner loop calculating the prime. I can't figure out the dependency, if that makes sense. create an empty tuple (haven't talked about lists) initialize a variable to increment the nth prime loop create a loop to count from 1 to 1000 (how do I count connect the counting of previous loop to this loop?) create a loop that calculates that generates the primes: if 2 or 3: 2 and 3 prime increment both loops store in tuple else: if 2 or 3 is a divisor of the current number: increment inner loop else: if a prime in tuple is a divisor of the current number: increment inner loop else: it's a prime increment both loops store in tuple I'm just struggling with the loops. I'm trying to avoid just asking for the code or allowing you to fix mine, because I want to understand the logic. Any help would be awesome.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1you need to introduce something like say count = 0 starting num = 1 say while count <= 1000: check if the num is prime (tricky part) store num in some way if yes increment count independent of whether the num is prime or not, every cycle increment the variable num num += 1(so that you check next number on every cycle) so first write the part for checking a prime no and then integrate it into the above mentioned model IMP: TUPLES ARE IMMUTABLE SO ONCE YOU INITIALIZE THE TUPLE IT REMAINS AS IT IS YOU CANNOT ADD OR REMOVE ELEMENTS so tuples won`t work and if you really want then every time you will have to form a new tuple adding the elements you want + picking elements from previous one and that can really take the heck out of you P.S can you also mention upto which lecture or which concepts you have covered so far

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1i am not sure but i feel the model you have created for the problem won`t work

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1well it is even possible with tuple and easier than what i had anticipated : so you can even do it with tuple but lists are better for this situation

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@harsimran_hs4 if he initializes a tuple like this: mytuple=(,) you can add more elements to it

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@wmhs14 if you want mate link us your code here or in a private message so i can see what you are doing. you can use paste sites like pastebin.com so u dont have to paste it in here

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@harsimran_hs4 and yes agree on your pseudocode version and that lists are better but tuples are safer for starters. his material covers till tuples on this problem set and im not sure if len(tupleor list) is in the material at his point....maybe it is.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot i tried mytuple = (,) and it gave me invalid syntax......can you elaborate a bit more on this aspect as i have never heard of it before....

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1basicaly what u need is: set count as 2 (cause remember that filtering for odds skips the prime number 2) while count is less than 1000: check if the element of the odds tuple is prime or not. if candidate is a prime number: increment count by 1 (when count gets to 1000 your code cannot enter while loop anymore so it continues after it) print out your (last) candidate so as you see you could use the tuple only for the odds. now the question is how many odds should you generate?!:)

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@harsimran_hs4 i mean something like this: mytuple=(2,) mytuple+=(1,) print mytuple

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1or this: mytuple=() mytuple+=(1,) print mytuple sry for spamming xD

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1basicaly u cant add objects cause as u said they are immutable. BUT u can concatenate them.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot thanks i had just figured that out ........it`s cool

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@wmhs14 now i think you have enough of pseudocode and hint (from me and @MicroBot ) for it`s implementation.... so go in for writing a code and if it`s doesn`t work you can link us to the code and we`ll try to figure out the problem...

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot Good work and hint`s especially on tuples

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0Thank you guys alot. I will try and figure it out later, and let you know if I'm still confused.

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0oddgen = () x = 100 for i in range(1,x): if i%2 != 0: oddgen += (i, ) This is a bounded loop to keep from having an infinite loop. And I was just practicing stopping the loop with an outer loop, but the inner loop won't stop. oddgen = () n = 1 while n <= 10 x = 100 for i in range(1,x): if i%2 != 0: oddgen += (i, ) n += 1 Without the ability to stop the inner loop I can't generate all odd primes or I will have an infinite inner loop. I used some print statements to check my code and the outer loop never increments. If I change the position of n += 1 as follows: oddgen = () n = 1 while n <= 10 x = 100 for i in range(1,x): if i%2 != 0: oddgen += (i, ) n += 1 n will now increment, but execute the inner loop 10 times. This gives me a tuple with odd numbers 1  100, 10 times. I feel dumb asking these questions, but I have been on this problem for a couple of days. I have learned a lot, but not the underlying lesson of the problem.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1your first code works correctly generating tuple containing all odd numbers.... your 2nd code works fine and generates the same output as 1st so it`s no better than previous your 3rd code will be struck in an infinite loop so it`s practically of no use.... now please answer this question what is your final aim to find 100 odd numbers or prime numbers from 1 to 100

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1agree on @harsimran_hs4 notes...and an addition....for(a,b) means that your numbers will have this range : [a,b) so its from 199 in your case

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1***by definition for cant be infinite since you give it boundries.if you want to get out of a loop you can use the break statement that instantly kicks you out of the current loop.But to do that correctly you should use some kind of an if statement. ex. if something happens : break ***to generate odds you dont need the while loop...that you will need to count the prime numbers after you generate the final oddtuple. cause as it is now in your second code...your n = 51 after just one while loop so it will never run while again after the 1st one

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1in your second code if u move the n+=1 outside of the for loop ull see it runs it 9 times as specified by your while loop.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1sorry for the mistake correction : your 3rd code does work but the output is tuple of odd numbers 9 times

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ye i thought that 3rd code had the n+=1 outside of while loop also xD...that's why i didnt mention it

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot haha same here.....realized it afterward that it was inside....

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0btw none of my code was indented. not sure why? The way it was written above couldn't even run, because of indention errors. 1st: #oddgen = () #x = 100 #for i in range(1,x): ##if i%2 != 0: ###oddgen += (i, ) 2nd: #oddgen = () #n = 1 #while n <= 10 ##x = 100 ##for i in range(1,x): ###if i%2 != 0: ####oddgen += (i, ) ####n += 1 3rd: #oddgen = () #n = 1 #while n <= 10 ##x = 100 ##for i in range(1,x): ###if i%2 != 0: ####oddgen += (i, ) ##n += 1 "***to generate odds you dont need the while loop...that you will need to count the prime numbers after you generate the final oddtuple." Exactly what I was trying to get at. I know you don't need the while loop for the oddgen. I was trying to practice exiting the oddgen loop prior to completiton by using the while loop. I was under the impression that the odd number generator will be infinite unless I can stop it with prime counter. That is what I was aiming for above, but my final goal is still the problem. I only bound the oddgen to 100 so I woouldn't enter an infinite loop. Can someone explain how this happens: "cause as it is now in your second code...your n = 51 after just one while loop so it will never run while again after the 1st one"

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1why are you keeping the n condition i don`t think you need it do something simple ... you don`t need to separately generate odd number just do it along the way... try this : prime = (2,) x = 3 while len(prime) <= 1000: # which ever number you want i am taking 1000 is_prime = True for i in range(2, x) if x % i == 0 is_prime = False if is_prime == True prime += (x,) print prime[999] this will give you 1000th prime just put in the semicolons etc yourself.....this is a simple model for calculation of prime with no concentration on optimization........

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1just increment the num x also after every iteration i forgot it in the above code

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1will reread everything later tonight cause now i dont have time. just one fast thing for the indentation problem while pasting code here u can use pastesites as pastebin.com u just give us the link and we get perfectly indented code:) see you later

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1ok i have an addition to make for @harsimran_hs4 's code, about the break statement if u want .not gonna spoil it for u further unless you want to see the code. So just let me know and ill send it to you. btw @harsimran_hs4 's code is very simple and good

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0I think there is something wrong with this model. When I evaluate the program I get 1051?

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0I might not be incrementing x correctly

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1can you please paste the code you have written and provide a link to it along with the test cases.....

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1@wmhs14 check your mail and note that in @harsimran_hs4 's code there are syntax errors (probably deliberatly put) and in general check always the indantations when pasting from smwhere . gl let me know if u need explanations.

harsimran_hs4
 3 years ago
Best ResponseYou've already chosen the best response.1@MicroBot yes i just gave him flow of handling the problem and even specified that there is a need of adding semi colons etc....@wmhs14 you can correct those yourself and i feel this should work ......and if it doesn`t you can paste you code and show us

wmhs14
 3 years ago
Best ResponseYou've already chosen the best response.0Nope I got it. It works perfectly. Thanks for all your help and patience.

MicroBot
 3 years ago
Best ResponseYou've already chosen the best response.1good job @wmhs14...keep going!:)
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.