組合せ(combination)(Luarida)

組合せ(くみあわせ、combination)

 nCr

function combination( n, r)とする。エラーチェックはしていません。
------------------------------------------
-- 組合せ(combination) Funcのサンプル combination_Func_sample.lua
------------------------------------------
function combination( n, r)
  local c = 1
  local i
  for i = 1, r do
    c = c * ( n - i + 1) / i
  end
  return c
end
 

利用例
------------------------------------------
-- 組合せ(combination) Funcのサンプル combination_Func_sample.lua
------------------------------------------
function combination( n, r)
  local c = 1
  local i
  for i = 1, r do
    c = c * ( n - i + 1) / i
  end
  return c
end
 
function main()
  local n = 5
  local r = 3
 
  canvas.drawCls(color(255,255,255))
  canvas.drawText("組合せ(combination) Funcのサンプル", 0, 0, 24, color(0,0,0))
 
  canvas.drawText("n = " .. n .. "   r = " .. r, 0, 50, 24, color(0,0,0))
  canvas.drawText("組合せの数 nCr は、".. n .. "      " .. r .. "  =  " .. combination( n , r), 0, 80, 24, color(0,0,0))
  canvas.drawText("C", 235, 74, 36, color(0,0,0))
 
  canvas.drawText("画面タッチで終了します。", 0, 130, 24, color(0,0,0))
  touch(3)
end
 
main()
 


コメント(最大30行)
名前:
コメント

すべてのコメントを見る



タグ:

+ タグ編集
  • タグ:
最終更新:2011年09月14日 23:20
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。