Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2012-02-21 13:02:21 -0500

DSM gravatar image

This isn't an answer to the question -- Shashank already explained that nicely -- but might be helpful anyway. A few points:

var('N')
N=[]

The first statement, var('N'), is unnecessary. It makes a symbolic variable called N and injects it into the local scope, but you immediately make a list and bind the name N to it.

Next, you don't actually need to keep track of l. In Python, you can refer to the last element of a list L by L[-1], where the -1 means "first from the end", and -2 would mean "second from the end", etc.

sage: a = [3, 10, 5.4]
sage: a
[3, 10, 5.40000000000000]
sage: a[0]
3
sage: a[2]
5.40000000000000
sage: a[-1]
5.40000000000000
sage: a[-2]
10
sage: a[-3]
3

As well, you can use "elif:" instead of chaining else/if pairs deeper and deeper. So one way to condense your code would be into:

N=[]
for p in prime_range(10**k+1):
    if p == 2:
        N.append(0)   
    elif p % 4 == 1:
        N.append(N[-1]-1)
    else:
        N.append(N[-1]+1)

where I've used the % operator of calling Mod explicitly, because I'm lazy. (There's even a ternary operator, "a if condition else b", so this could be further condensed, but I think it actually looks clearer as is.)

There are even some slick non-loop-based approaches to getting this, but hopefully this helps. In general, if you find yourself needing to keep track of loop indices manually, there's probably a prettier way to do something.

This isn't an answer to the question -- Shashank already explained that nicely -- but might be helpful anyway. A few points:

var('N')
N=[]

The first statement, var('N'), is unnecessary. It makes a symbolic variable called N and injects it into the local scope, but you immediately make a list and bind the name N to it.it instead. You only need to use var to make symbolic variables, not to declare things in general (which is one way in which Python is different from many other languages, so it takes some getting used to.)

Next, you don't actually need to keep track of l. In Python, you can refer to the last element of a list L by L[-1], where the -1 means "first from the end", and -2 would mean "second from the end", etc.

sage: a = [3, 10, 5.4]
sage: a
[3, 10, 5.40000000000000]
sage: a[0]
3
sage: a[2]
5.40000000000000
sage: a[-1]
5.40000000000000
sage: a[-2]
10
sage: a[-3]
3

As well, you can use "elif:" instead of chaining else/if pairs deeper and deeper. So one way to condense your code would be into:

N=[]
for p in prime_range(10**k+1):
    if p == 2:
        N.append(0)   
    elif p % 4 == 1:
        N.append(N[-1]-1)
    else:
        N.append(N[-1]+1)

where I've used the % operator of calling Mod explicitly, because I'm lazy. (There's even a ternary operator, "a if condition else b", so this could be further condensed, but I think it actually looks clearer as is.)

There are even some slick non-loop-based approaches to getting this, but hopefully this helps. In general, if you find yourself needing to keep track of loop indices manually, there's probably a prettier way to do something.