Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 停留在麻省理工学院的6.005软件构造元素上,无法实现computePiInHex()_Java_Hex_Pi - Fatal编程技术网

Java 停留在麻省理工学院的6.005软件构造元素上,无法实现computePiInHex()

Java 停留在麻省理工学院的6.005软件构造元素上,无法实现computePiInHex(),java,hex,pi,Java,Hex,Pi,我目前正在做麻省理工学院的开放式课程6.005软件构建元素的自学 我在上 我现在被卡住的部分是1.d。它要求我在PiGenerator中实现computePiInHex。请注意,此函数应 只返回Pi的小数位数,不返回前导的3 以下是我到目前为止的情况: public class PiGenerator { /** * Returns precision hexadecimal digits of the fractional part of pi. * Returns digits in m

我目前正在做麻省理工学院的开放式课程6.005软件构建元素的自学

我在上

我现在被卡住的部分是1.d。它要求我在PiGenerator中实现computePiInHex。请注意,此函数应 只返回Pi的小数位数,不返回前导的3

以下是我到目前为止的情况:

public class PiGenerator {
/**
 * Returns precision hexadecimal digits of the fractional part of pi.
 * Returns digits in most significant to least significant order.
 * 
 * If precision < 0, return null.
 * 
 * @param precision The number of digits after the decimal place to
 *                  retrieve.
 * @return precision digits of pi in hexadecimal.
 */
public static int[] computePiInHex(int precision) {
    if(precision < 0) {
        return null;
    }

    return new int[0];
}

/**
 * Computes a^b mod m
 * 
 * If a < 0, b < 0, or m < 0, return -1.
 * 
 * @param a
 * @param b
 * @param m
 * @return a^b mod m
 */
public static int powerMod(int a, int b, int m) {
   if(a < 0 || b < 0 || m < 0)
       return -1;

   return (int) (Math.pow(a, b))  % m;
}

/**
 * Computes the nth digit of Pi in base-16.
 * 
 * If n < 0, return -1.
 * 
 * @param n The digit of Pi to retrieve in base-16.
 * @return The nth digit of Pi in base-16.
 */
public static int piDigit(int n) {
    if (n < 0) return -1;

    n -= 1;
    double x = 4 * piTerm(1, n) - 2 * piTerm(4, n) -
               piTerm(5, n) - piTerm(6, n);
    x = x - Math.floor(x);

    return (int)(x * 16);
}

private static double piTerm(int j, int n) {
    // Calculate the left sum
    double s = 0;
    for (int k = 0; k <= n; ++k) {
        int r = 8 * k + j;
        s += powerMod(16, n-k, r) / (double) r;
        s = s - Math.floor(s);
    }

    // Calculate the right sum
    double t = 0;
    int k = n+1;
    // Keep iterating until t converges (stops changing)
    while (true) {
        int r = 8 * k + j;
        double newt = t + Math.pow(16, n-k) / r;
        if (t == newt) {
            break;
        } else {
            t = newt;
        }
        ++k;
    }

    return s+t;
  }
}

据我所知,piDigits方法得到以16为基数的π的第n位数字。那么在computePiHex中,应该实现?否则,有人能给我指出正确的方向,因为我不知道他们在computePiInHex中要求什么。

尝试不同的powermod算法

public static final int PI_PRECISION = 10000;

int [] piHexDigits  = PiGenerator.computePiInHex(PI_PRECISION);
对我来说,这篇文章帮助很大:

我使用的实际算法是基于Bruce Schneier的Applied Cryptography中的伪代码

function modular_pow(base, exponent, modulus)
    if modulus = 1 then return 0
    Assert :: (modulus - 1) * (modulus - 1) does not overflow base
    result := 1
    base := base mod modulus
    while exponent > 0
        if (exponent mod 2 == 1):
           result := (result * base) mod modulus
        exponent := exponent >> 1
        base := (base * base) mod modulus
    return result