【问题标题】:Finding the last two digits before the decimal point for the number (4+sqrt(11))^n查找数字 (4+sqrt(11))^n 小数点前的最后两位
【发布时间】:2014-02-23 05:20:13
【问题描述】:

我正在做一个问题,我必须找到数字小数点前的最后两位数字
[4 + sqrt(11)]n.

例如,当 n = 4, [4 + sqrt(11)]4 = 2865.78190... 时,答案是 65。其中 n 可以从 2 变化9.

我的解决方案 - 我尝试构建一个平方根函数来计算 11 的 sqrt 其精度等于用户输入的 n 值。

我在 Java 中使用了BigDecimal 来避免溢出问题。

public class MathGenius {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        long a = 0;
        try {
            a = reader.nextInt();
        } catch (Exception e) {
            System.out.println("Please enter a integer value");
            System.exit(0);
        }

        // Setting precision for square root 0f 11. str contain string like 0.00001
        StringBuffer str = new StringBuffer("0.");
        for (long i = 1; i <= a; i++)
            str.append('0');
        str.append('1');

        // Calculating square root of 11 having precision equal to number enter
        // by the user.
        BigDecimal num = new BigDecimal("11"), precision = new BigDecimal(
                str.toString()), guess = num.divide(new BigDecimal("2")), change = num
                .divide(new BigDecimal("4"));
        BigDecimal TWO = new BigDecimal("2.0");
        BigDecimal MinusOne = new BigDecimal("-1"), temp = guess
                .multiply(guess);
        while ((((temp).subtract(num)).compareTo(precision) > 0)
                || num.subtract(temp).compareTo(precision) > 0) {

            guess = guess.add(((temp).compareTo(num) > 0) ? change
                    .multiply(MinusOne) : change);

            change = change.divide(TWO);
            temp = guess.multiply(guess);
        }

        // Calculating the (4+sqrt(11))^n
        BigDecimal deci = BigDecimal.ONE;
        BigDecimal num1 = guess.add(new BigDecimal("4.0"));
        for (int i = 1; i <= a; i++)
             deci = deci.multiply(num1);

        // Calculating two digits before the decimal point
        StringBuffer str1 = new StringBuffer(deci.toPlainString());
        int index = 0;
        while (str1.charAt(index) != '.')
            index++;
        // Printing output

        System.out.print(str1.charAt(index - 2));
        System.out.println(str1.charAt(index - 1));
    }
}

此解决方案在 n = 200 时有效,但随后开始变慢。它在 n = 1000 时停止工作。

什么是处理问题的好方法?

2 -- 53
3 -- 91
4    65
5    67
6    13
7    71
8    05
9    87
10   73
11   51
12   45
13   07
14   33
15   31
16   85
17   27
18   93
19   11
20   25
21   47
22   53
23   91
24   65
25   67

【问题讨论】:

  • 我会在前 200 个中寻找一个模式......然后也许做一个归纳证明来说明确实有一个模式;然后使用它而不是计算它。
  • @d'alar'cop - 你能解释一下哪种模式???
  • 另外,那里有一个非常明显的模式......从n = 22开始它又从n = 2开始......检查这是否一致。然后只需将这些数字保存在一个数组中......然后将结果基于提供的 n 并使用适当的 %ing。
  • 有一个模式在 n=22 @d'alar'cop 重复

标签: java performance precision bigdecimal


【解决方案1】:

在 n=22 时,结果似乎从 n=2 的位置重复。 因此,将这 20 个值以与列表中相同的顺序保存在数组中,例如nums[20]

那么当用户提供一个n时:

return nums[(n-2)%20]

现在有证据证明这种模式重复 here

或者,如果您坚持详细计算;因为您通过循环乘法(而不是 BigDecimal pow(n))来计算幂,所以您可以将前面使用的数字修剪为最后 2 位数字和小数部分。

【讨论】:

  • @JigarJoshi 不。我想做更多的测试,也许是一个证明。但我确实说过“似乎”,并且我还展示了一个替代解决方案以防万一。但是,在问题 cmets 中,OP 说确实存在一种模式 - 我不确定他是如何验证这一点的。
  • Nathaniel Johnston 在 MO 发布了一个证明:mathoverflow.net/questions/158420
  • @NoahSnyder 谢谢你,先生。纳撒尼尔做得很好。很高兴看到证明!
【解决方案2】:

这里有一个更简单的解决方案...

使用4+sqrt(11)的有理表示:

BigInteger hundred     = new BigInteger("100");
BigInteger numerator   = new BigInteger("5017987099799880733320738241");
BigInteger denominator = new BigInteger("685833597263928519195691392");
BigInteger result = numerator.pow(n).divide(denominator.pow(n)).mod(hundred);

更新:

正如您在下面的 cmets 中提到的,此过程容易丢失精度,最终会产生不正确的结果。我发现这个问题在数学方面相当有趣,所以我在 MO (https://mathoverflow.net/q/158420/27456) 上发布了一个问题。

您可以在https://mathoverflow.net/a/158422/27456阅读答案。

【讨论】:

  • 4+sqrt(11) 是不合理的 .. 一段时间后舍入误差会很大
  • @T.J,但无论你在任何图灵机(即任何现代计算机)上做什么都是如此!!!!!!!无论您使用上面的分子/分母,还是任何其他计算 11 平方根的函数,您的计算最终都会使用有理数。
  • 但我需要计算确切的值..问题是要实际寻找其中的模式... ronding 错误会使答案彼此不同。 Twist 不是计算,而是寻找规律。
  • @T.J:对于double val = 4+sqrt(11),上面的分子/分母是 100% 准确的,没有精度损失。当然,使用sqrt 时会发生精度损失,但您无法避免这种情况(除非您实现BigRational 类,该类将平方根计算到比double 类型提供的更准确的水平) .我的建议只是解决您问题中描述的问题的一种更简单,更准确的方法。如果您正在寻找实际的模式,那就另当别论了。
猜你喜欢
  • 2012-01-06
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
相关资源
最近更新 更多