elz (
elz) wrote in
intro_to_cs2009-11-11 05:21 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
Entry tags:
Lecture 2
Problem set 1: Computing prime numbers, product of primes
This one's a little trickier, so feel free to comment if you run into any problems or want to brainstorm with other people!
(eta: having issues with poll, alas)
no subject
no subject
NASnMmOJgL
(Anonymous) 2012-01-06 10:20 pm (UTC)(link)no subject
[x]
no subject
no subject
no subject
question = "Which prime do you wish to find?"
print (question)
n = raw_input()
print ("You wish to find the " + n + "th prime? Fat chance of THAT!"
aqgHeNVyWCUz
(Anonymous) 2012-01-07 05:23 am (UTC)(link)no subject
Anybody else make a pretty ugly program? I'm gonna go try it again at some point tonight. How'd your approach differ from mine?
no subject
no subject
(I realized I totally got off-track and tried to get an answer the problem didn't ask for.)
pXWMiprUPTOVcxLS
(Anonymous) 2012-01-07 09:28 am (UTC)(link)MoDeXpCzNyFKFCSvSS
(Anonymous) 2011-08-14 05:14 am (UTC)(link)vqiwThxRBr
(Anonymous) 2012-01-09 05:19 am (UTC)(link)no subject
no subject
And this:
You might think about which integers you need to check as divisors – certainly you don’t need to go beyond the candidate you are checking, but how much sooner can you stop checking?
is a hint that saves a lot of work. If you want to know if 5 is prime, do you need to divide it by 4? If not, why not?
(And I'd say definitely worry about one at a time!)
no subject
no subject
no subject
I think this is a problem that I'll be having with this, if I choose to continue. The lecture and the reading material don't seem to be particularly helpful either.
no subject
After a while, though, I did hit critical mass, and the learning curve flattened out a *lot.* I know that "keep on trying, you'll get it" isn't always the most useful thing to say in situations like this, but....
It might be useful for you to, for a while, explicitly write out the pseudocode for anything you want to code. That way, you're forcing yourself to think algorithmically, but you're writing English steps instead of Python ones, so it's a little more familiar.
For example, if you're checking to see that a number is prime, then the pseudocode might look like:
n = the number we're checking
start with x = 2, 'cause it's the smallest prime
while x < n
if n is divisible by x
n is not prime, we can stop checking now
otherwise,
n might still be prime!
set x to the next value to check divisibility with, and run through the loop again
no subject
no subject
I'm a lot like you, I think. This is my first non-HTML code because I self-taught in HTML by taking other people's source code and manipulating it to see how they'd done what they did. Hang in there a little longer and see if it clicks? If not, I don't mean to push you or frustrate you more.
no subject
I'm grateful to have a community working on this stuff--thanks to all of you who have commented.
no subject
yvixSJkxBtcbHfBLqiT
(Anonymous) 2012-01-09 04:22 am (UTC)(link)no subject
If it helps, here's how I'd translate the pseudocode I just gave you into real python:
n = the number we're checking
(raw_input prompts the user and returns their answer; we need to typecast the input from a string to a number using int, or otherwise the computer will think it's text and whine at us)
start with x = 2, 'cause it's the smallest prime
(nothing really fancy there)
while x < n
(another place where the python bears a striking resemblance to the pseudocode)
if n is divisible by x
(use the modulus function to see if x divides evenly into n)
n is not prime, we can stop checking now
(breaks you out of the while loop, and sets your "instruction counter" at the instruction immediately after it)
otherwise,
*
n might still be prime!
(since our program is assuming n is prime until told otherwise, already, there's no need to translate this into code)
set x to the next value to check divisibility with
(sets x to old-x + 1)**
and run through the loop again
(you don't actually need to put anything here for this, in python. When the interpreter reaches the end of a while loop, it will automatically go back to the top, assuming the loop condition (x < n) still holds true)
*You don't actually need to use an else case for this, because there's a break statement in the if case, but it's a more direct translation of the pseudocode.
**It would be faster to only check n's divisibility with just primes, rather than with all integers, but that is beyond the scope of this simple example. ^^
no subject
no subject
LjdywZEBondMs
(Anonymous) 2011-08-14 05:08 am (UTC)(link)no subject
JQGKyQUiLhGaNrGPR
(Anonymous) 2011-08-14 06:59 am (UTC)(link)jBLQAbcTXgxeuAast
(Anonymous) 2012-01-07 08:24 am (UTC)(link)no subject
If it makes you feel any better, I've been drowning in that feeling while working my way through the *very first* exercise (I'm running late - lecture 2 is for tomorrow for me) and thank you for pinning it down so exactly!
I was feeling like most of the reading material was also written in that same foreign language, but I did get there in the end.
I'm hoping it will all get easier with practise!
BbJLPbCRReuw
(Anonymous) 2012-01-06 10:05 pm (UTC)(link)no subject
no subject
no subject
no subject
Examples used in Lecture
ETA or you can just go to the lecture video at the MIT website and they have the handout right there.
Examples of overloading
3*4
3*'ab'
'a' + 'bcd'
3 + 'ab'
str(3)+'ab'
'a'<3
4<'3'
'4'<'3'
The code he used
##x=3 #create variable x and assign value 3 to it
##x=x*x # bind x to value 9
##print x
##n = raw_input('Enter a number; ')
##print n
##print n/n
##x=15
##if (x/2)*2 == x:
## print 'Even'
##else: print 'Odd'
##x=15
##if (x/2)*2 == x: print 'Even'
##else: print 'Odd'
##z='b'
##if 'x' < z :
## print 'Hello'
## print 'Mom'
##if 'x'< z :
## print 'Hello'
##print 'Mom'
##x=15
##y=5
##z=11
##print x,y,z
###Is this right?
##if x< y:
## if x< z : print 'x is least'
## else: print 'z is least'
##else: print 'y is least'
##
##if x < y and x < z : print 'x is least'
##elif y < z : print 'y is least'
##else: print 'z is least'
##y=0
##x=3
##itersLeft = x
##while (itersLeft>0):
## y= y+x
## itersLeft = itersLeft -1
###print 'y =',y,',intersLeft=',intersLeft
##print y
##x=10
##i=1
##while(i<x): ## if x%i == 0: ## print 'divisor ',i ## i = i+1 ##x = 10 ##for i in range(1,x): ## if x%i == 0: ## print 'divisor ',i
hqFbGesiTVFUz
(Anonymous) 2012-01-09 03:48 am (UTC)(link)