Skip to content

Instantly share code, notes, and snippets.

@greymd
Created November 15, 2016 09:31
Show Gist options
  • Save greymd/c38835968a41806b268f7d5f17d7bcdb to your computer and use it in GitHub Desktop.
Save greymd/c38835968a41806b268f7d5f17d7bcdb to your computer and use it in GitHub Desktop.
Rubyでの繰り返し二乗法
# 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