6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

第10回 :: ruby_fifth

Last updated at Posted at 2020-12-10

!Mac OS X-10.15.7 !ruby-2.7.1p83$

お題:Fibonacci数列

fib(n) = fib(n-1)+fib(n-2)
0 1 1 2 3 5 8 13 21 ...

をrecursion(再帰)で求める.

解説

前回の講義で作成した作ったassert_equal.rbをcodes(作業directory)にcopyしておくこと.

fib(0) = 0

Fibonacci数列の初項は0.

def fib(n)
  if n==0
    return 0
  end
end

p fib(0)

前回作成したassert_equalを用いて出力ために

require './assert_equal'

def fib(n)
  if n==0
    return 0
  end
end

assert_equal(0, fib(0))

実行すると

expected :: 0
result   :: 0
succeeded in assert_equal

ちゃんとできてる

fib(1) =1

次はfib(1)=1

require './assert_equal'

def fib(n)
  if n==0
    return 0
  end
  if n==1
    return 1
  end
end

assert_equal(0, fib(0))
assert_equal(1, fib(1))

これで実行すると上手く出力される.

テストの部分が多くなってきたので,配列にする.

require './assert_equal'

def fib(n)
  if n==0
    return 0
  end
  if n==1
    return 1
  end
end

[[0,0],[1,1]].each do |pair|
  puts assert_equal(pair[0], fib(pair[1]))
end

fib(2) = fib(1) + fib(0) = 1

条件分岐を少し変更する

require './assert_equal'

def fib(n)
  if n==0
    return 0
  end
  if n<=2
    return 1
  end
end

[[0,0],[1,1],[2,1]].each do |pair|
  puts assert_equal(pair[1], fib(pair[0]))
end

これが正解.

refactoring

もう少し配列の受け取りを明示的にすると,

index, expected = pair

と修正できて

require './assert_equal'
[[0,0],[1,1],[2,1]].each do |index, expected|
  puts assert_equal(expected, fib(index))
end

の方がいいらしい.

最終的に

 require './assert_equal'

 def fib(n)
   if n==0
     return 0
   end
   if n<=2
     return 1
   end
 end
[[0,0],[1,1],[2,1]].each do |index, expected|
  puts assert_equal(expected, fib(index))
end

あとはフィボナッチ数列の計算を繰りかえす

フィボナッチ数列は第2項からfin(n) = fin(n-1) + fin(n-2)になるから最終形は

require './assert_equal'

def fib(n)
  return 0 if n==0
  return 1 if n==1
  return fib(n-1) + fib(n-2)
end

[[0,0],[1,1],[2,1],[3,2],[4,3],
[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
  puts assert_equal(expected, fib(index))
end

これを書いて実行すると

> ruby fobonacci.rb
expected :: 0
result   :: 0
succeeded in assert_equal
expected :: 1
result   :: 1
succeeded in assert_equal
expected :: 1
result   :: 1
succeeded in assert_equal
expected :: 2
result   :: 2
succeeded in assert_equal
expected :: 3
result   :: 3
succeeded in assert_equal
expected :: 5
result   :: 5
succeeded in assert_equal
expected :: 8
result   :: 8
succeeded in assert_equal
expected :: 13
result   :: 13
succeeded in assert_equal
expected :: 21
result   :: 21
succeeded in assert_equal
expected :: 34
result   :: 34
succeeded in assert_equal

ちゃんとフィボナッチ数列が実行されてる.


  • source ~/grad_members_20f/members/okamoto0910/article/c10.org
6
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?