Modular exponentiation
来源:互联网 发布:数据库水平垂直切分 编辑:程序博客网 时间:2024/06/11 16:02
1. 递归实现:
unsigned long long modular_pow_recursive(unsigned long long base, int exponent, int modulus){ if (modulus == 1) { return 0; } if(exponent == 0) { return 1; } if(exponent & 1 == 1) { return base * modular_pow_recursive(base, exponent - 1, modulus) % modulus; } unsigned long long ret = modular_pow_recursive(base, exponent >> 1, modulus); return ((ret * ret) % modulus;}
刚开始的时候,最后两行写成了如下的形式:
int ret = modular_pow_recursive(base, exponent >> 1, modulus) %modulus; return (ret * ret) % modulus
测试的时候发现结果不对,查了半天,才发现是有 overflow了。
改成前面代码里的形式,后者:
int ret = modular_pow_recursive(base, exponent >> 1, modulus) %modulus; return ((unsigned long long )ret * ret) % modulus就可以了。
2. 迭代实现:
unsigned long long modular_pow(unsigned long long base, int exponent, int modulus){ if(modulus == 1) { return 0; } if(exponent == 0) { return 1; } unsigned long long result = 1; while(exponent > 0) { if( exponent & 1) { result = (result * base) % modulus; } base = (base * base) % modulus; exponent = exponent >> 1; } return result;}
3. 一个比较容易想到的实现:
unsigned long long modular_pow_rude(unsigned long long base, int exponent, int modulus){ int i; unsigned long long result = 1; for(i = exponent; i > 0; i--) { result = (result * base) % modulus; } return result;}
测试代码:
int main(int argc, char **argv){ int base = 19; int exponent = 78; int modulus = 199879; int ret = modular_pow_recursive(base, exponent, modulus); ret = modular_pow(base, exponent, modulus); return ret;}
References:
https://en.wikipedia.org/wiki/Modular_exponentiation
0 0
- Modular exponentiation
- modular exponentiation/快速幂取模
- Fast modular exponentiation
- Fast Modular Exponentiation
- 快速幂取模运算(Modular Exponentiation)
- Modular exponentiation模幂的计算方法
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- Exponentiation
- TCP之深入浅出send和recv
- Leetcode #43. Multiply Strings 字符串相乘 解题报告
- 如何用git,管理你的项目,同步到托管平台
- <java API源码初体验>2---collection集合之LinkedList原理分析
- 设计模式——单例模式(Singleton)
- Modular exponentiation
- 分布式数据库应用开发正解
- 链接之外部链接与内部链接
- LightOj 1298
- 最明白的JAVA——回调机制
- Android开发之java代码工具类。判断当前网络是否连接并请求下载图片
- 用AdoDataSet实现数据表的导入导出
- 拒绝别人是一种煎熬
- 链式哈希表