a的n次方的快速算法(A fast algorithm for the n power of a).doc

a的n次方的快速算法(A fast algorithm for the n power of a).doc

  1. 1、本文档共11页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 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

您可能关注的文档

文档评论(0)

jgx3536 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6111134150000003

1亿VIP精品文档

相关文档