http://blog.csdn.net/pipisorry/article/details/46008603
求刚好比最大整数小一点的M的幂
如M = 3时,因为3^19 < 2^32 < 3^20,所以结果应该3^19。
log 3^k < log 2^32 => k < log 2^32 / log 3
python代码:maxPowerOfM = int(pow(m, (int)(log(0x7fffffff) / log(m))))
欧几里德算法
欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。
计算原理
定理:gcd(a,b) = gcd(b,a mod b)
证明:a可以表示成a = kb + r ,则r = a mod b
假设d 是(b,a mod b)的公约数,则 d | b , d |r ,但是a = kb +r ,因此d也是(a,b)的公约数
因此(a,b)和(b,a mod b)的公约数是一样的,其最大公约数也必然相等,得证
[互动百科-欧几里得算法及c实现]
python实现
def gcd(a, b):
'''Return the greatest common divisor of a and b.'''
a = abs(a)
b = abs(b)
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
皮皮blog
阶乘算法
#有一年 Python 经验的程序员
def Factorial(x):
res = 1
for i in xrange(2, x + 1):
res *= i
return res
print Factorial(6)
#懒惰的Python程序员
def fact(x):
return x > 1 and x * fact(x - 1) or 1
print fact(6)
#更懒惰的Python程序员
f = lambda x: x and x * f(x - 1) or 1
print f(6)
#专家级Python程序员
import operator as op
import functional as f
fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact(6)
#Python 黑客
import sys
@tailcall
def fact(x, acc=1):
if x: return fact(x.__sub__(1), acc.__mul__(x))
return acc
sys.stdout.write(str(fact(6)) + '\n')
#专家级程序员
import c_math
fact = c_math.fact
print fact(6)
皮皮blog
编程语言中的log函数是如何实现的
基本都是通过泰勒展开做的
[C语言编写自己的对数函数 ]
[ ci.nii.ac.jp/naid/110002673332]
from: http://blog.csdn.net/pipisorry/article/details/46008603