A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
A slight problem with PS1:
(Sorry if this is a duplicate, it didn't pop up when I asked it before, but:)
I'm having a bit of trouble with the first piece of code in PS1. The current piece of code I have yields me a list of all odd numbers.. which, I guess, is a first step, but doesn't really help me since I'm not precisely sure why.
def ref():
x=5
primelist=[]
count=3
while count<1000:
for y in xrange(2, x1):
if x%y==0: x=1+x; break
else: primelist.append(x); x=x+1; count=count+1; break
print primelist; print x; print count
anonymous
 5 years ago
A slight problem with PS1: (Sorry if this is a duplicate, it didn't pop up when I asked it before, but:) I'm having a bit of trouble with the first piece of code in PS1. The current piece of code I have yields me a list of all odd numbers.. which, I guess, is a first step, but doesn't really help me since I'm not precisely sure why. def ref(): x=5 primelist=[] count=3 while count<1000: for y in xrange(2, x1): if x%y==0: x=1+x; break else: primelist.append(x); x=x+1; count=count+1; break print primelist; print x; print count

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0This might be a bit strange looking, I don't know, but here's my intention on this (that's obviously going awry): Start with x, a prime. (Yes, I know 5 isn't the first prime, it's 2 and 3, but I'll get to that in a second.) Primelist is where I want to store prime numbers, and count counts the sequence of primes (5 being the third prime number). So I want to create the range from 2 to x1, so I can test all numbers besides itself and one. The plan from there is to test with the modulo operation if any number in that range divides evenly into the prime candidate (x). If one does, try the next number, because x isn't prime. If it doesn't, append that number to primelist, increase x by one (try the next number), and increase the count sequence by one since we've found another prime. The problem, as I said, is that primelist is filled with odd numbers when they're obviously not prime! (e.g., 9, 15, 21, etc.) There's obviously some sort of error here I'm not catching.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hello maxwell. I see that you are using arrays and just want to remind you that consider the problem set and compare it to lectures. Since by this problem set arrays havent been taught I assumes students are not expected to resolve this problem set by using arrays. There should be an easier way without using complex functions. I would suggest to try it one more time yourself just figuring out the algorithm. I dont think you need anything more than if, else if and while and some variables. ...If you still have a problem you can consider the following code  PrimeCounter = 2 odd = 3 interval = 1000 if interval > 0: while PrimeCounter < (interval + 1): n = odd  1 while odd%n!=0: n=n1 if n==1: PrimeCounter = PrimeCounter + 1 odd = odd + 2 print (str(interval) +'th prime number is ' + str(odd2)) elif interval == 0: print "You entered 0" else: print "You entered negative number"

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0rmaxwell: the else is really a for else construct so align the else with for and take out the break at the end.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0castlepeak: wow that worked, thanks a lot! just goes to show you, kids: mind your indents. i was worried i had the code fundamentally wrong, but that's a weight off my shoulders! Balash: thanks for the tip, the final code i ended up using to finish up ps1 had bits from your example sort of melded into mine (mainly because i thought it was quicker to spit out the list all at once rather than cycle through 1000 "The xth prime number is y." I know we hadn't been taught arrays/lists yet, but i just happened across them in the reading and figured they'd be useful, so i just went for it.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Sure no problem maxwell. I hope it helped. Hey you guys, could you do me a small favour? Can you help me promote my fundraising? its for my college... I really need it to get my studies going. Hope its not too much to ask. www.giveforward.com/saveaneducation
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.