2

试图构建一段代码,返回 range(1, limit) 中的数字是否是两个平方数的总和(平方数,例如1**2 = 1, 2**2 = 4- 所以我试图将它们分配给一个数字列表,无论它们是总和任何这些平方数的组合 - 例如 1+1、1+4、4+16 等)。下面是我写的内容,但它对所有值都返回“未平方”,这是错误的。我认为代码可能存在一个小问题,但我对此很陌生,并且正在努力了解它是什么。我将非常感谢任何指导。

代码:

for n in range(1,21):
    lst = range(1,21)
    squares = [x**2 for x in lst]
    for i in range(1, 21):
        for x in range(1, 21):
            if i in squares:
                if x in squares:
                    n2 = i+x
    if n2 == n:
        print n, " - Sum of Squares"

    else:
        print n, " - Not a Sum of Squares"
4

4 回答 4

2

这不是原来的::: 但它可能会给你更多的洞察力。

In [20]: from itertools import combinations_with_replacement

In [21]: nk=map(sum,(combinations_with_replacement([x**2 for x in range(1,21)],2)))

In [22]: for n in range(1,21):
    ...:     if n in nk:
    ...:         print n, " -Sum of Squares"
    ...:     else:
    ...:         print n, " -Not a sum of Squares"
    ...:         
1  -Not a sum of Squares
2  -Sum of Squares
3  -Not a sum of Squares
4  -Not a sum of Squares
5  -Sum of Squares
6  -Not a sum of Squares
7  -Not a sum of Squares
8  -Sum of Squares
9  -Not a sum of Squares
10  -Sum of Squares
11  -Not a sum of Squares
12  -Not a sum of Squares
13  -Sum of Squares
14  -Not a sum of Squares
15  -Not a sum of Squares
16  -Not a sum of Squares
17  -Sum of Squares
18  -Sum of Squares
19  -Not a sum of Squares
20  -Sum of Squares

In [23]: 
于 2013-01-15T18:02:19.663 回答
1

你是这个意思吗?

for n in range(1,21):
    lst = range(1,21)
    squares = [x**2 for x in lst]
    for i in range(1, 21):
        for x in range(1, 21):
            if i in squares:
                if x in squares:
                    n2 = i+x
                    if n2 == n:
                        print n, " - Sum of Squares"

                    else:
                        print n, " - Not a Sum of Squares"

输出:

>>> 
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
1  - Not a Sum of Squares
2  - Sum of Squares
2  - Not a Sum of Squares
...
于 2013-01-15T17:55:22.650 回答
0

当您将 n 与 n2 计算的最后一个值进行比较时,您应该这样做

for n in range(1,21):
    lst = range(1,21)
    squares = [x**2 for x in lst]
    sum_of_squares = False
    for i in range(1, 21):
        for x in range(1, 21):
            if i in squares:
                if x in squares:
                    n2 = i+x
                    if n2 == n:
                        sum_of_square = True
    if sum_of_square:
        print n, " - Sum of Squares"
    else:
        print n, " - Not a Sum of Squares"
于 2013-01-15T17:58:59.427 回答
0

我认为生成所有平方和的数字会更容易。

# oversized output array so we can use the numbers in 1..21 as indices
is_ssq = [False for n in range(22)]
squares = [x * x for x in range(1, 21)]
for i in squares:
    for x in squares:
        if i + x < 21:
            is_ssq[i + x] = True

所以is_ssq[n]表示是否n是两个平方的和。


如果你想获得所有 Pythonic,你可以使用itertools并保存一个缩进级别:

import itertools

is_ssq = [False for n in range(22)]
squares = [x * x for x in range(1, 21)]
for i, x in itertools.product(squares, squares):
    if i + x < 21:
        is_ssq[i + x] = True
于 2013-01-15T18:10:56.160 回答