Last active
December 27, 2015 22:29
-
-
Save regonn/7398949 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def B(string) #現在の計算式を後ろから見ていき最初に現れるB()を取得する関数 | |
| i = 1 | |
| num_n = 0 | |
| num_m = 0 | |
| while(string[-i] == ")") | |
| i += 1 | |
| end | |
| j = 0 | |
| while(string[-i] != ",")#数字が何桁になってもいいように数字を取得(すべて文字列で扱っているため) | |
| num_n += string[-i].to_i * (10**j) | |
| i += 1 | |
| j += 1 | |
| end | |
| j = 0 | |
| while(string[-i-1] != "(") | |
| num_m += string[-i-1].to_i * (10**j) | |
| i += 1 | |
| j += 1 | |
| end | |
| string.gsub!("B(#{num_m},#{num_n})",change(num_m,num_n)) | |
| return string | |
| end | |
| def change(num_m,num_n) #与えられたB()の計算を行う | |
| if(num_m == 0 && num_n >0) | |
| return "#{num_n + 1}" | |
| end | |
| if(num_m > 0 && num_n == 0) | |
| return "B(#{num_m - 1},1)" | |
| end | |
| if(num_m > 0 && num_n > 0) | |
| return "B(#{num_m - 1},B(#{num_m},#{num_n - 1}))" | |
| end | |
| end | |
| def fish(string) #B()がなくなるまで計算を繰り返す | |
| while(string[0] == "B") | |
| print "=" | |
| string = B(string) | |
| puts string | |
| end | |
| end | |
| def g(x) #式を生成 | |
| formula = "B(#{x},#{x})" | |
| puts formula | |
| fish(formula) | |
| end | |
| puts g(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment