Reputation: 33
Suppose a
and b
are odd integers. Then a^2 + b^2
is either 2
or 10
modulo 16
. My question is the following: Is there a way of writing a script whose assumptions are a%2 == 1
and b%2 == 1
so that the output (a^2 + b^2) % 16
gives the tuple (2,10)
?
Edit: Here's my unsuccessful approach:
def test():
aList=[]
bList=[]
for a in range(0,16):
for b in range(0,16):
if a%2==1 and b%2==1:
aList.append(a)
bList.append(b)
print a^2+b^2%16
This of course will not return a tuple. My hope is to output the tuple (2,10).
Upvotes: 0
Views: 225
Reputation: 51142
As I understand, you want to collect the distinct residues modulo 16 of a**2 + b**2
(note the ^
operator is not what you want here) into a tuple. So instead of printing each residue out, you should add them to a set in order to collect the distinct values. The set can then be converted to a tuple, using sorted
to get them in order:
residues = set()
for a in range(0, 16):
for b in range(0, 16):
if a % 2 == 1 and b % 2 == 1:
residues.add((a**2 + b**2) % 16)
residues = tuple(sorted(residues))
print(residues)
The result is (2, 10)
as expected.
This code can be simplified somewhat: instead of generating all integers in the range and just keeping the odd ones, you can generate odd integers using a range starting from 1 with a step size of 2; and you can use a set comprehension for brevity.
residues = {
(a**2 + b**2) % 16
for a in range(1, 16, 2)
for b in range(1, 16, 2)
}
residues = tuple(sorted(residues))
print(residues)
Upvotes: 3