求幂的前n位 [英] first n digits of an exponentiation

查看:131
本文介绍了求幂的前n位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定幂的前n个数字(A )。

 例如:一个= 12,B = 13安培; n = 4时,前4位是1069。


解决方案

计算一个通过以下迭代:

1 = A 1 的,结果
2 = A 2 的,结果
... 的结果
I = A 的,结果
... 的结果
在<子>乙 = A

您的在<子> I + 1 = A I &倍; A 的。 Calcluate各的 I 的不完全是。问题是,相对误差的 的小于的 N 的时间的的相对误差

你想获得比的 10 -n 的减去最终的相对误差。因此,对每一个步骤相对误差可以是。在每一步删除最后一个数字。

例如, A = 2 B = 16 N = 1 的。最后的相对误差的 10 -n = 0.1 的。每一步相对误差的 0.1 / 16> 0.001 的。因此,3位是每一步非常重要。如果n = 2,则必须保存4位数。通用规则:保存[ N +登录 10 的]数字在每一步

2(1),4(2),8(3),16(4),32(5),64(6),128(7),256(8),512(9),1024( 10)RARR; 102,结果
204(11),408(12),816(13),1632(14)&RARR; 163,326(15),652(16)。

答:6

该算法的 0的compexity(B)的。但它很容易改变它得到的 O(日志B)

How do i determine the first n digits of an exponentiation (ab).

eg: for a = 12, b = 13 & n = 4, the first 4 digits are 1069.

解决方案

Calculate ab by the following iterations:

a1 = a1,
a2 = a2,
...
ai = ai,
...
ab = ab

You have ai+1 = ai×a. Calcluate each ai not exactly. The thing is that the relative error of ab is less than n times relative error of a.
You want to get final relative error less than 10-n. Thus relative error on each step may be . Remove last digits at each step.

For example, a=2, b=16, n=1. Final relative error is 10-n = 0.1. Relative error on each step is 0.1/16 > 0.001. Thus 3 digits is important on each step. If n = 2, you must save 4 digits. Common rule: save [n+log10 b] digits at each step.

2 (1), 4 (2), 8 (3), 16 (4), 32 (5), 64 (6), 128 (7), 256 (8), 512 (9), 1024 (10) → 102,
204 (11), 408 (12), 816 (13), 1632 (14) → 163, 326 (15), 652 (16).

Answer: 6.

This algorithm has a compexity of O(b). But it is easy to change it to get O(log b)

这篇关于求幂的前n位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆