辗转相除进化版--stein算法
2013-12-08 15:21
169 查看
#include <stdio.h> #include <stdlib.h> #define DECDATATYPE long DECDATATYPE gcdcore(DECDATATYPE a,DECDATATYPE b) { if (!a) return b; if (!b) return a; /* make a to odd number */ while (!(a & 0x1)) a=a>>1; if (a<b) { b=(b-a)>>1; return gcdcore(b,a); }else{ a=(a-b)>>1; return gcdcore(a,b); } } DECDATATYPE gcd(DECDATATYPE a,DECDATATYPE b) { DECDATATYPE c=0; /* make at least one of them from even to odd */ while (!(a&0x1) && !(b & 0x1)){ a=a>>1; b=b>>1; c++; } if (!(a&0x1)){ a=a>>1; return gcdcore(a,b)<<c; }else{ return gcdcore(b,a)<<c; } } int main(int argc, char * args[]){ DECDATATYPE Num_A,Num_B; if(argc < 3){ printf("Args error. Usage:$(proc_name) Num_A Num_B"); exit(0); } Num_A=atoi(args[1]); Num_B=atoi(args[2]); printf("Greatest Common Denominator between %ld and %ld is: %ld",Num_A,Num_B,gcd(Num_A,Num_B)); return 0; }
相关文章推荐
- 希望OL蝙蝠猪进化幸运猪的材料
- 达尔文进化奖
- 印尼穴居小矮人是进化异类还是病态现代人?
- 迭代,进化,敏捷,UP
- 奥地利研究发现老夫择少妻是进化需要
- 暴雪和黑客的战争六:Warden和外挂的进化(一)
- 《C++0x漫谈》系列之:瘦身前后——兼谈语言进化
- 著名品牌车标进化史
- 互联网进化示意图第一次修正版
- 进化效应
- 上证综指和我的“进化”
- 计算机内的进化
- 从“绘制一个立方体”来看OpenGL的进化过程
- PHP码农进化史5 【转】
- 首次在学术会议上演讲互联网进化论
- GUI进化2--数据的定义(1)基本数据的定义
- YouTube五周年,正迫使Web进化
- 互联网进化将证明生命进化有方向性
- 八皇后问题的进化(2)-用递归函数实现
- 【程序语言】C++的扩充进化之路