Created
November 15, 2016 09:31
-
-
Save greymd/c38835968a41806b268f7d5f17d7bcdb to your computer and use it in GitHub Desktop.
Rubyでの繰り返し二乗法
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
# coding: utf-8 | |
# 参考: http://judge.u-aizu.ac.jp/onlinejudge/commentary.jsp?id=NTL_1_B | |
def ppow(x, n) | |
return 1 if n == 0 | |
return ppow(x**2, n / 2) if (n % 2) == 0 | |
return ppow(x**2, n / 2) * x if (n % 2) == 1 | |
end | |
p ppow(3,21) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment