プロジェクトオイラー#6に問題があります。問題は以下の通りです。pythonプロジェクトオイラー6(任意番号付き)
最初の100個の自然数の平方和と合計の平方和の差を求めます。
オイラーで質問されているもの(100を含むすべての数字)を好きな数字(xを含むすべての数字)に置き換えることができます。これを行うには3つの機能が必要だと私は決めました。コードは次のとおりです。
#the sumSquare function squares every number from 0 to number called
#in the function and adds each term to a list. the function returns the sum
#of all numbers in this list
def sumSquare(num):
list1 = []
for i in range(num+1):
x = i^2
list1.append(x)
return sum(list1)
#the squareSum function adds every whole number from 0 up to and including
#the number called in the function to list2. It returns the sum squared of #every number in the list
def squareSum(num):
list2 = []
for i in range(1,num+1):
list2.append(i)
return (sum(list2) * sum(list2))
def ans(num):
return squareSum(num) - sumSquare(num)
print ans(100)
私の出力は2549748ですが、私は、正しい解決策は、私が間違っているつもりどこ誰もが見るん25164150.であることを、オンラインで読んでいます。私はちょうどコードすることを学んでいるので、より経験豊富な人に見やすいものを見逃しているかもしれません。しかし、私が知る限りでは、リストは合計される前に適切な数字となるものでいっぱいになっています。
未解決ですが範囲はリストを返すので、値を繰り返して2番目のリストに追加する必要はありません。 'sum(range(1、num + 1))'が動作します。 – Alan