title: Java- Implementation of Bernoulli Numbers in new method
body: <p>I am having trouble editing an existing implementation of <a href="http://introcs.cs.princeton.edu/java/96optimization/Bernoulli.java.html" rel="nofollow noreferrer">Bernoulli numbers</a> in Java which has the <a href="http://introcs.cs.princeton.edu/java/92symbolic/BigRational.java.html" rel="nofollow noreferrer">BigRational helper class</a>. The original implementation puts the calculation of the Bernoulli numbers inside the Main method. I made a new class to return the calculation for individual Bernoulli numbers. What am I doing wrong?</p>
  
  <pre><code>import java.math.BigInteger;
  
  public class Bernoulli {
  
      public static void main(String[] args) {
          int N = 20;
          System.out.println(bern(N));
  
      }
  
      public static BigRational bern(int N) {
          BigInteger[][] binomial = new BigInteger[N+1][N+1];
          for (int n = 1; n &lt;= N; n++) binomial[0][n] = BigInteger.ZERO;
          for (int n = 0; n &lt;= N; n++) binomial[n][0] = BigInteger.ONE;
          for (int n = 1; n &lt;= N; n++)
              for (int k = 1; k &lt;= N; k++)
                  binomial[n][k] = binomial[n-1][k-1].add(binomial[n-1][k]);
  
          BigRational[] bernoulli = new BigRational[N+1];
          bernoulli[0] = new BigRational(1, 1);
          bernoulli[1] = new BigRational(-1, 2);
          for (int k = 2; k &lt; N; k++) {
              bernoulli[k] = new BigRational(0, 1);
              for (int i = 0; i &lt; k; i++) {
                  BigRational coef = new BigRational(binomial[k + 1][k + 1 - i], 
                          BigInteger.ONE);
                  bernoulli[k] = bernoulli[k].minus(coef.times(bernoulli[i]));
              }
              bernoulli[k] = bernoulli[k].divides(new BigRational(k+1, 1));
          }
          return bernoulli[N];
      }
  }
  </code></pre>
  
  <p>I am looking to do this in order to calculate the <a href="https://en.wikipedia.org/wiki/Particular_values_of_Riemann_zeta_function" rel="nofollow noreferrer">Zeta function</a> for even numbers.</p>
  
  <p><a href="https://i.stack.imgur.com/D62gk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D62gk.png" alt="enter image description here"></a></p>
  
  <p>A Test method that I created calculates the denominator of this equation through BigDecimal. I see an upcoming problem, would I need to convert the Bernoulli BigRational into a BigDecimal? I will likely need to adjust the <a href="http://introcs.cs.princeton.edu/java/92symbolic/BigRational.java.html" rel="nofollow noreferrer">BigRational class</a> I found.</p>
  
  <pre><code>import java.math.BigDecimal;
  import java.math.BigInteger;
  
  public class Test {
      public static void main(String[] args) {
          int N = Integer.parseInt("20");
  
          // precompute binomial coefficients
          BigInteger[][] binomial = new BigInteger[N+1][N+1];
          for (int n = 1; n &lt;= N; n++) binomial[0][n] = BigInteger.ZERO;
          for (int n = 0; n &lt;= N; n++) binomial[n][0] = BigInteger.ONE;
  
          // bottom-up dynamic programming
          for (int n = 1; n &lt;= N; n++)
              for (int k = 1; k &lt;= N; k++)
                  binomial[n][k] = binomial[n-1][k-1].add(binomial[n-1][k]);
  
  
          // now compute Bernoulli numbers
          BigRational[] bernoulli = new BigRational[N+1];
          bernoulli[0] = new BigRational(1, 1);
          bernoulli[1] = new BigRational(-1, 2);
          for (int k = 2; k &lt; N; k++) {
              bernoulli[k] = new BigRational(0, 1);
              for (int i = 0; i &lt; k; i++) {
                  BigRational coef = new BigRational(binomial[k + 1][k + 1 - i], 
                          BigInteger.ONE);
                  bernoulli[k] = bernoulli[k].minus(coef.times(bernoulli[i]));
              }
              bernoulli[k] = bernoulli[k].divides(new BigRational(k+1, 1));
          }
          BigDecimal n = new BigDecimal(6);
          BigDecimal two = new BigDecimal(2);
          System.out.println(fac(n).multiply(two));
          System.out.println("\u03A0^2");
  
  
      }
  
      public static BigDecimal fac(BigDecimal n) {
          if (n.equals(BigDecimal.ZERO)) {
              return BigDecimal.ONE;
          }
          return n.multiply(fac(n.subtract(BigDecimal.ONE)));
      }
  
  }
  </code></pre>