Has anyone completed PS1? I have my program written but it's not doing what I want. Would anyone be willing to take a look at it? I can't find my mistake.....
Hi CiaraB, which portion of PS1 are you having trouble with?
Hi. I can get it to test for odd numbers OK, but when I get it to test for prime numbers using n%a!=0..... it will add the number to the tuple that I made to collect the prime numbers if there are any divisors in my range for i that will not give a remainder does that make sense?
so it will add both prime and non prime multiple times to the same tuple, the only difference will be that prime numbers will be added fewer times, wheras I need a rule that says if all of the integers in the range of i do not give a remainder when divided into n, then add n to the tuple....
I also tried to do be more 'defensive' by making a tuple for non prime numbers i.e. n%a==0, and then trying to generate a second tuple for prime numbers by checking if n is in the non prime number tuple and if not adding it to the prime number tuple, but that didn't work either.....
I'm sorry CiaraB, but I'm confused. Perhaps we're looking at different course iterations? In the course I'm working on MIT 6.00 (2011) our PS1 contained three problems that dealt with credit card balances. I looked ahead to PS2 and those problems deal with polynomials, derivatives, Newtons Method and a word game.
yes we're talking about different problems
I'm taking the course online only, and PS1 (the second problem set) had a task to write a program that would generate the 1000th prime number, have you done such a problem?
I think the PS for the online course are from 2008 actually
Ahh, I see. I'm not actually enrolled so my content is different.
There are two OWAs available, one from 2008 and one for 2011.
Ah I see
Sorry!
No problem, thanks for replying anyway.....it just means I will have to stick with it to try figure it out myself! :)
I might still be able to help. If you want to paste the section of code that's not giving you the results you want I'd be happy to look at it.
OK. Well first I'm just trying to generate a list of prime numbers up to 50...... prime = (2) #tuple n=3 while n<50: if (n/2)*2!=n: #odd numbers only for i in range(2,n): if n%i!=0: #prime number test prime = str (prime) + ',' + str(n) n=n+1 print prime Thanks so much!
Ok, I think the main thing that needed to be added was a break function to ext the loop once a prime had been identified. Here's what I came up with: prime = ('2') #tuple n = 3 while n < 50: if n % 2 != 0: #odd numbers only for i in range(2, n): if n % i != 0: #prime number test prime = str (prime) + ',' + str(n) break n = n + 1 print prime I changed the initial condition as well, only because that's how I envisioned the problem solution. Hope this helps!
OK. Well I'm not sure what a break function is but I will be sure to look it up. Thanks so much for your help!
No problem. The break function tells the for loop to stop what it was doing and to return to while loop for its next iteration. That's why you were getting duplicates before, because the for loop kept going even though your if statement had already identified the prime.
Great. Only problem now is that it's not actually giving me primes, it's giving me all odd numbers, as if the prime number test is incorrect....need to check it again. Is what I have n%i!=0 the way that you would test for it?
that break function will be very useful, thanks for that!
Ahh, I see what you mean. I guess I should have paid more attention to the output. To eliminate odds that aren't primes I suggest using a Boolean. Here's what I came up with: prime = ('2') #tuple n = 3 while n < 50: if n % 2 != 0: #odd numbers only flag = True for i in range(2, n): if n % i == 0: #prime number test print '1:', n flag = False break if flag: prime = str (prime) + ',' + str(n) n = n + 1 print prime
You can also do n = n + 2 to test only odds. No need to step by 1 if you are just going to skip all the evens.
So, jmike, you are gathering the odd numbers, and giving them a flag that you then overwrite if the odd number has divisors within the range of i? That makes more sense! Thanks also e.mccormick, I was clearly make it more complicated than needed!
A lot of people start in computers doing more work than is needed. Part of computer science is to learn to do less work.
please use a code pasting site like http://dpaste.com - there are many others or enclose your code in backticks (key left of 1 on qwerty keyboard) ``` if condition: pass ```
While I haven't gotten too far in these courses, but I solved a similar problem in c++ in finding the 10001 prime. In my program I factored each number and counted how many factors each number had. If it had only 2 factors (1 and itself) then I said it was prime. This is just another solution to the problem and saves the computer a lot of time by reducing how many values it has to mod by.
Join our real-time social learning platform and learn together with your friends!