Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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?

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at in under 10 minutes. Go to now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly


Get your free account and access expert answers to this and thousands of other questions

as 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....
yes 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...
Thank 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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

you 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
i am not sure but i feel the model you have created for the problem won`t work
well 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
@harsimran_hs4 if he initializes a tuple like this: mytuple=(,) you can add more elements to it
@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 so u dont have to paste it in here
@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.
@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....
basicaly 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?!:)
@harsimran_hs4 i mean something like this: mytuple=(2,) mytuple+=(1,) print mytuple
or this: mytuple=() mytuple+=(1,) print mytuple sry for spamming xD
basicaly u cant add objects cause as u said they are immutable. BUT u can concatenate them.
@MicroBot thanks i had just figured that out`s cool
@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...
yep gl!
@MicroBot Good work and hint`s especially on tuples
Thank you guys alot. I will try and figure it out later, and let you know if I'm still confused.
oddgen = () 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.
your 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
agree on @harsimran_hs4 notes...and an addition....for(a,b) means that your numbers will have this range : [a,b) so its from 1-99 in your case
***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
in 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.
sorry for the mistake correction : your 3rd code does work but the output is tuple of odd numbers 9 times
ye i thought that 3rd code had the n+=1 outside of while loop also xD...that's why i didnt mention it
@MicroBot haha same here.....realized it afterward that it was inside....
btw 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"
why 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........
just increment the num x also after every iteration i forgot it in the above code
will 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 u just give us the link and we get perfectly indented code:) see you later
ok 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
I think there is something wrong with this model. When I evaluate the program I get 1051?
I might not be incrementing x correctly
can you please paste the code you have written and provide a link to it along with the test cases.....
@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.
@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
Nope I got it. It works perfectly. Thanks for all your help and patience.
good job @wmhs14...keep going!:)

Not the answer you are looking for?

Search for more explanations.

Ask your own question