私はプログラミングとルビーが初めてです。私は特定のディオファントス方程式 (MIT オープンコースウェアの問題から) を扱うコードに取り組んでおり、それを使って何ができるかを調べています。
このコードは、3 つの変数を持つ特定の線形方程式に対して、3 つの配列と、そのうちの 2 つのハッシュを生成します。
コードは次のとおりです。
def diophantine_solutions(x)
#For all x > 1, finds values for a, b, c such that 6a + 9b + 20c = x,
#Creates array of solvable x, unsolvable x,
#Creates hash of solvable x with possible values of a, b, c
nopes=(1..x).to_a #will contain sums with no solutions at the end
yups=[] #has solvalbes
yups_values=[] #solutions for a, b, c
yups_hash={} #sums with the solutions
while x>0
for a in (0..x/6):
for b in (0..x/9):
for c in (0..x/20):
total=6*a + 9*b + 20*c
if total==x
yups<< x
yups_values<< [a, b, c]
end
end
end
end
x=x-1
end
yups_hash=[yups.zip(yups_values)]
yups=yups.uniq
nopes=nopes-yups
puts yups_hash[20]
end
diophantine_solutions(20)
私が今やろうとしているのは、個々のハッシュペアにアクセスして、それらが正しく並んでいることを確認することですが、
puts yups_hash[]
任意の数値に対して nil を返します。何か助けはありますか?また、初心者なので何か良い方法があれば教えていただけると助かります。