- 1、本文档共11页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
a的n次方的快速算法(A fast algorithm for the n power of a)
a的n次方的快速算法(A fast algorithm for the n power of a)
EN binary power
The power of n a for solving this problem, many people see after all do not think what content! The first time I think this problem would not have what content, but then think carefully, there really is a learning oh!
See if we find A8, there are two kinds of methods
1.a8 = a * a * a * a * a * a * a * a need to compute 7 multiplication
2.a = 8 (a * a * A * A) (where A = a * a) need to compute 3 multiplication
An algorithm for such a simple calculation step difference is a big difference, so for a common n representation
The general method of solving?
After many days of thinking, no results, today suddenly in a book to see about this algorithm, exultation. The solution to tidy up a bit, and then gives the specific code.
The idea of the algorithm
General for a (2x + b) = a2x * a B
So there is
(b = 0): a = 2x + B (AX) 2;
(b = 1): a = 2x + B (a x) 2 * a;
For an, the first N binary representation to write, so
An = a (N1 N2 N3 N4 (2) =..)...
Then we can from left to right as shown in the following table (when n = 13, 1101)
N bit
One
One
Zero
One
Multiplicative
A
A2 * a = A3
(A3) A6 = 2
(A6) 2 * a = A13
The first code (using Java, with the function call, failure effect)
Package alg;
Public class {AN
Public static final int EXIT = 3;
Int a; / / base
Int bin; / / index
Public StackInteger s;
Public AN (int a, int bin) {
This.a = a;
This.bin = bin;
S = new (StackInteger);
S.push (EXIT); / / as a sign of the last
ToBin (bin);
}
Public void renew (int a, int bin) {
This.a = a;
This.bin = bin;
S.push (EXIT); / / as a sign of the last
ToBin (bin);
}
/ / is from left to right
Public int (leftToRight) {
Int i;
Int result = 1;
While (true) {
(I = s.pop);
Switch (I) {
Case 0:
Result = result;
Break;
Case 1:
Result = result * result * a;
Break;
Case EXIT:
Return result;
}
}
}
Public int rightToLeft (int a) {
Return 0;
}
/ * *
* the decimal into binary stack
*
* @param ten
* /
Public StackInteger toBin (int ten) {
Int te
您可能关注的文档
- 7、相邻体积单位之间的进率(7, the rate of entry between adjacent volume units).doc
- 7个性格模式(7 personality patterns).doc
- 7名中国研修生遭管理日企欠薪 法庭展首次辩论维权(7 Chinese trainees have been punished, Japanese enterprises wage Court Exhibition for the first time debate rights).doc
- 7大赚钱金点子(7 big money tips).doc
- 7的口决.(7 mouths never do).doc
- 7天酒店 放羊式管理(7 day Hotel sheep type management).doc
- 8.杰出经营者的高明之处(8., outstanding operator's brilliance).doc
- 6大型电视辩论赛方案(6 large-scale TV debate program).doc
- 8.语病分析(8. error analysis).doc
- 800mw汽轮机末级叶片断裂原因分析及防范措施(Fracture analysis and preventive measures of last stage blade of 800MW turbine).doc
最近下载
- C103116【基础】2025年东北师范大学045101教育管理《841学校组织与管理之教育管理学》.pdf VIP
- ABB 传动变频器 ACS880-04(200-600 kW)传动模块硬件手册 安装及使用手册.pdf
- (入党积极分子)思想汇报.doc VIP
- 入党积极分子(发展对象)培训班学习心得体会.docx VIP
- 物业二次装修管理培训课件.pptx
- 祭母舅的祭文.docx
- 国家开放大学电大本科《社会保障学》2024期末试题及答案(试卷号:1283).doc VIP
- 公路工程标准施工招标文件 2018年版(完整版).doc
- 国家开放大学电大本科《社会学概论》期末试题及答案(试卷号:1282).doc
- 官地水电站大坝固结和接触灌浆工程施工组织措施.doc
文档评论(0)