LoginSignup
0
0

More than 5 years have passed since last update.

基本情報技術者試験に出てくるx mod yをrubyで書くとこうなる

Last updated at Posted at 2017-09-23

本題

引用先リンク:基本情報技術者試験ドットコム

第1問

関数 f(x,y)が次のように定義されているとき,f(775,527)の値は幾らか。ここで,x mod yはxをyで割った余りを返す。
f(x,y): if y = 0 then return x else return f(y,x mod y)

ア0  イ31  ウ248  エ527

平成29年春期 問6

rubyで書いてみる

test.rb

class Test

  def f(x , y)

    if y == 0
      x
    else
      f(y, xmody(x, y))
    end
  end

  def xmody(x, y)
    x % y
  end
end


instance = Test.new

puts instance.f(775, 527)

test.rbを実行

$ ruby ./test.rb
31

31という答えが得られた。イを選択し正解でした。
x mod yというメソッドの呼び方が見慣れないし、その形式を用いる必要性もよくわからない。
この形式について考察している時間のほうが勉強の時間よりも長い、まずい。。

0
0
0

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
0
0