A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
"Solved" the first half of PS1, code works perfectly, but I have a question or two.
Sticking to the idea that:
1. I should only use "if" and "while" because they're the only things covered by Lec2.
2. I don't want to needlessly divide by every number between the testing number and 2.
Does it make sense that I'd need to create a separate conditional to handle calculating 3 as prime?
anonymous
 5 years ago
"Solved" the first half of PS1, code works perfectly, but I have a question or two. Sticking to the idea that: 1. I should only use "if" and "while" because they're the only things covered by Lec2. 2. I don't want to needlessly divide by every number between the testing number and 2. Does it make sense that I'd need to create a separate conditional to handle calculating 3 as prime?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I've certainly seen implementations that do that. There's frequently a way to rearrange things so that the separate conditional is unnecessary. If you post your solution we can talk about it.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Also, it's a good idea to copy your whole post before clicking post, in case it gets lost.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'm a bit apprehensive about posting my solution, simply because it works and I feel like I'd be giving answers away (even if I feel like I'm doing it wrong). :P Is that sort of thing okay? lol

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0That's fine, but it makes it difficult for anyone to answer your question in detail. Is it possible for you to 'translate' your solution into pseudocode so that you can preserve its structure and control flow in a way that doesn't provide a working answer?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, my general logic was to first check if the square of my divisor was greater than the number being tested, and that's obviously problematic when it comes to 3 given that 2 is the lowest common divisor when it comes to primes. Here's the bit that handles 3: divNum = 2 ## divisor testNum = 3 ## starting number, assuming 2 as the first prime totPrime = 1 ## total number of discovered primes primeTup = (2,) ## just a tuple I created to gather the primes, serves no purpose in the code if ((divNum*divNum) >= testNum) and (testNum <= 3): if (testNum%divNum != 0): print testNum, 'is prime.' totPrime += 1 primeTup += (testNum,) testNum += 2 What bugs me the most is that the first line might as well just be saying "if testNum == 3", because it can't even handle calculating 2 as prime. And if that's the case I might as well just assume 3 as prime the same way the assignment said to assume 2 as prime. That's what makes me feel like that part is just unnecessary and there should be a better way to do it.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, you've included 2 in your list of primes already. Is there a reason not to also include 3 and then start testing at 4?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, the instructions suggest to generate all odd numbers greater than 1 as candidates to be prime, and then further along said to keep in mind when you formulate your ending condition that the program didn't generate the first prime (2). So outside of a mix of pride and obedience, no, there's no real reason, lol.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I think if those if statements are ONLY to check for 3, it would be better to just bite the bullet and say if testNum == 3: primeTup += testNum. That way you're at least condensing it and more importantly making it more readable. The way to avoid testing for 3 specifically that I know of just uses a test, like while divisor < testNum/2, or while divisor < sqrt(testNum) if you structure the program to run Assignments assign an isPrime boolean to true while divisor < testNum/2 test for primeness, if it's not prime assign the isprime bool to False if isprime append to primeTup you don't really need to worry about 3, because when it gets tested, the while divisor < testNum/2 line skips right to the end, where isPrime should still be true.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Actually, I just had an epiphany that allowed me to get rid of that whole block of code and a couple other things I realized I didn't need at all. That brings me down to 13 lines of code, less if you don't include appending the tuple, and the two print statements declaring a number prime or not prime. So 10 lines in that case. My mistake was in thinking I needed to check if the square of my divisor was less than my testing number in the first place, when the only thing that matters is if the square of my divisor is MORE than my testing number *as long as I'm still getting a remainder* from their division. Not sure what you were trying to say about an "isPrime" boolean, but thanks for taking the time for helping me and talking me through this. Otherwise I might have just given moved on to the second half nonethewiser, and I'm sure that part will be even more of a headache, lol ... I don't even know what a logarithm is. >_>

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0 I think this works: ps1.py  def isPrime(n): # this is a function created by me ri = 2 rf = n / 2 isPrim = 1 # yes for b in range(ri, rf+1): mod = n % b if mod == 0: isPrim = 0 # no return isPrim x = 2 lastPrime = 0 numbPrime = 1000 while lastPrime < numbPrime: # range from 0 to 999, i.e. 1000 if isPrime(x): # call function isPrime() print x lastPrime = lastPrime + 1 x = x + 1  Try: c:\Python27\ps1.py > ps1.txt to see the results in a .txt file. 

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0PS1 was "assigned" at the end of Lec2 and due at Lec3. So while Functions and For loops can make things a little easier, I chose not to use them as the assignment assumes we don't know them.
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.