Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that calculates the Bernoulli numbers. The main method shou

ID: 3543103 • Letter: W

Question


Write a Java program that calculates the Bernoulli numbers. The main method should call a worker method with header similar to public static double[ ] calcBernoulli(int number), and then display the calculated Bernoulli numbers. The worker method should take an input parameter number, which is the number of Bernoulli numbers to be calculated, and it should return a reference to an array that holds the calculated Bernoulli numbers. The m-th Bernoulli number (m = 0,1,2,3,...) can be calculated by the following formula: Here, represents the Binomial coefficient indexed by two nonnegative integers n and k (also called "n choose k"), and n! = n(n - l ) ( n - 2)...2 middot 1 is the factorial of a nonnegative number n. For example. 6! = 6 middot 5 middot 4 middot 3 middot 2 middot 1 = 720. Notice that Bm can be calculated based on knowing B0, B1,..., Bm-1. The worker method should calculate the array of Bernoulli numbers iteratively (i.e. no recursion) by looping over the calculations and storing the results of the computation in local variables. Instead of calculating the Binomial coefficients directly, try to devise a computationally more efficient method to obtain them. Hint: the n-th row of Pascal's triangle actually contains the Binomial coefficients in this order. For example: The first 11 Bernoulli numbers are: 1.000000, 0.500000, 0.166667, -0.000000, 0.033333, -0.000000, 0.023810, -0.000000, -0.033333, 0.000000, 0.075758

Explanation / Answer


package testscores;


import java.util.ArrayList;

import java.util.List;

public class TestScores {

public static int fac(int n){

if(n==0){

return 1;

}

else{

int val=1;

for(int i=1;i<=n;i++){

val=val*i;

}

return val;

}

}

public static double[] calcBernoulli(int number){

double[] ber=new double[number];

for(int i=0;i<number;i++){

if(i==0){

ber[i]=1;

}

else{

double val=1;

for(int k=0;k<i;k++){

val=val-((fac(i)/(fac(i-k)*fac(k)))*(ber[k]/(i-k+1)));

}

ber[i]=val;

}

}

return ber;

}

public static void main(String[] args){

double[] ber1=new double[11];

ber1=calcBernoulli(11);

for(int i=0;i<ber1.length;i++){

System.out.println(ber1[i]);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote