找到满足 2 个方程的整数对的数量

finding the number of pairs of integers that satisfies 2 equations

我有一个等式供我复习:

The number of pairs of integers (x and y) that satisfy both x**2+y**2 <= 36 and y = x - 4 is either 4, 5, 6, 7 or 8?

老师说,如果能写出剧本,就能加分。

我尽力使用 python:

a = [[y+4, y] for y in xrange(100) if ((y+4)^2)+(y^2) <= 36] #I tried 100 just to test.

但是当我尝试这样做时,它给出了 17 个答案。我正在寻找一种方法来给出指定的答案(4、5、6、7 或 8)。提前致谢。

^ 替换为 **,这是求幂的正确运算符。

此外,您可能希望将 y 放在 [-2, 6] 范围内,因为只有 2 个正整数对满足您的条件(但没有禁止负整数,对吧?)。尝试:

>>>    [(y-4, y) for y in range(-2, 7) if (y-4)**2 + y**2 <= 36]
>>>    # answers here

in python ^ 是位异运算符 a ^ b

对于指数,您需要使用 **

应该是:

a = [[y+4, y] for y in xrange(100) if ((y+4)**2)+(y**2) <= 36]