elz: (ada-tubes)
elz ([personal profile] elz) wrote in [community profile] intro_to_cs2009-11-11 05:21 pm
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)
rodo: chuck on a roof in winter (Default)

[personal profile] rodo 2009-11-12 05:51 am (UTC)(link)
My problem isn't that I can't think how the code is supposed to work. I can do that, so your example isn't really helping me any. My problem is that I have no clue how to put you example into actual working code. This might be the most obvious thing ever for you, but for me, it's the thing that's standing in my way. I've run into this problem before with HTML, so it isn't new. With HTML, I managed to work my way through it by tinkering with existing and working code to figure out how to adapt it to my purposes (I'm still starting with a collage of copypasta rather than actual writing when I'm not comfortable with an element). I haven't found a similar way to deal with PHP, and I'm not sure if there is such a thing for Python since it seems more specific to me.

[personal profile] ex_wicker969 2009-11-12 06:03 am (UTC)(link)
I don't know if I'm helping or being annoying but I went back and worked through each example in the Python Programming Wikibook and tinkered with each one, seeing what variations did, so I still have no clue how to implement ungemmed's pseudocode yet (that's about as far as I got with my own) but at least I'm getting the idea of how to define variables and manipulate them.

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.
newrambler: sunrise outside my old trailer (Default)

[personal profile] newrambler 2009-11-13 09:33 pm (UTC)(link)
I'm glad to read these comments--I haven't had a chance to start working on this problem set for real yet, but I had to talk myself down from panicking. I'm going to try your suggestion and work through the rest of the examples, and perhaps some of it will start to sink in.

I'm grateful to have a community working on this stuff--thanks to all of you who have commented.

[personal profile] ex_wicker969 2009-11-14 02:40 am (UTC)(link)
We can do this!

yvixSJkxBtcbHfBLqiT

(Anonymous) 2012-01-09 04:22 am (UTC)(link)
This is way better than a brick & mortar etsablishemnt.
ungemmed: (Default)

[personal profile] ungemmed 2009-11-12 06:21 am (UTC)(link)
Hmm, okay. I sometimes run into the same problem, I think every coder does--I mean, obviously you're running into it more than most, but I've definitely had that "argh, why do computers not speak English" feeling before.

If it helps, here's how I'd translate the pseudocode I just gave you into real python:

n = the number we're checking
n = int(raw_input('n? '))

(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
x = 2

(nothing really fancy there)
while x < n
while x < n:

(another place where the python bears a striking resemblance to the pseudocode)
    
if n is divisible by x
    if x % n == 0:

(use the modulus function to see if x divides evenly into n)
        
n is not prime, we can stop checking now
        break

(breaks you out of the while loop, and sets your "instruction counter" at the instruction immediately after it)
    
otherwise,
    else
*
        
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
        x += 1

(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. ^^

[personal profile] ex_wicker969 2009-11-16 05:15 am (UTC)(link)
I need a lightbulb icon! I just tried to write a comment telling you how I got it but I rambled but I think I've at least got how I was thinking in the wrong direction, basically. I couldn't even start. Thank you!
ungemmed: (Default)

[personal profile] ungemmed 2009-11-16 05:18 am (UTC)(link)
I'm really glad I could help!

LjdywZEBondMs

(Anonymous) 2011-08-14 05:08 am (UTC)(link)
A good many vablulaes you've given me.