-1

I'm using "Sage," but I do not think that it is relevant for this question.

I have made the following definitions, and attempted to implement an if loop:

Q = DiagonalQuadraticForm(ZZ,[1,1,1,1])
L = Q.representation_number_list(10)
for i in range(len(L)):
         if L[i]  == 0 % 2:
              print i

Upon looking up Python demos, it seems as though my syntax is correct and (I haven't been made aware of any syntax errors.)

However, nothing is being printed! In fact, if I declare "i" in the next textbox, all that displays is "9," which doesn't change despite my fiddling with the modulus. What is wrong?

Andres Mejia
  • 221
  • 2
  • 8
  • 2
    `0 % 2` is the same as `0`, so your test is the same as `if L[i] == 0`. Is that what you meant? Or did you mean `if L[i] % 2 == 0`? – Barmar Jun 08 '16 at 20:23
  • The latter is the correct way to test if a value is even. – Barmar Jun 08 '16 at 20:23
  • You say _Syntax Error_ in the subject... but you don't have a syntax error. Can you edit the subject to remove the confusing wording? – tdelaney Jun 08 '16 at 20:25
  • edited. @Barmar, I'm really used to $a \equiv b (mod2)$ and so I think that's what I had in mind. That's what I meant – Andres Mejia Jun 08 '16 at 20:28
  • Can you print out `i` as you iterate through it? `print(repr(i))` is best. It appears as though the values are strings and maybe digits with commas. – tdelaney Jun 08 '16 at 20:28
  • @Barmar you were indeed correct, that fixed the problem. *facepalm* sorry, still getting used to this. – Andres Mejia Jun 08 '16 at 20:30
  • Please don't edit the question to fix the problem you had in your code - that makes the question and posted answers meaningless and confusing. – interjay Jun 08 '16 at 20:34
  • Unedited, you are correct. – Andres Mejia Jun 08 '16 at 20:38
  • By the way, if you intend to use integers modulo `n`, you may want to use `mod(x,n)` to get them (or just create the ring of integers mod n in Sage). – kcrisman Jun 09 '16 at 13:53

2 Answers2

1

Modulus syntax in Python (and most other programming languages) is not the same as mathematical notation. In math, you write

a ≡ b (mod n)

to indicate that a and b are congruent modulo n. In programming languages, modulus is treated as an arithmetic operation on a and n, analogous with division (it returns the remainder of the division rather than the quotient). To determine if the number is congruent with another number, you compare that remainder with the number, so it's written as:

a % n == b

Therefore, your if statement should be:

if L[i] % 2 == 0:

When you write

if L[i] == 0 % 2:

it's parsed as:

if L[i] == (0 % 2):

And since the remainder of 0 / 2 is 0, it's equivalent to:

if L[i] == 0:

which isn't what you intended.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You intend to print even numbers? This should work:

Q = DiagonalQuadraticForm(ZZ,[1,1,1,1])
L = Q.representation_number_list(10)
for i, v in enumerate(L):
     if v % 2 == 0:
          print i

enumerate(...) can replace range(len(...))

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139