Home > database >  Generalized Harmonic Numbers in java
Generalized Harmonic Numbers in java

Time:02-01

Generalized harmonic numbers. Write a program GeneralizedHarmonic.java that takes two integer command-line arguments n and r and uses a for loop to compute the nth generalized harmonic number of order r, which is defined by the following formula: \frac{1}{1^{b}}, but ![\frac{1}{0^{b}}.

The code that works:

public class GeneralizedHarmonic {

    public static void main(String[] args) {

        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        double sum = 0;
        for (int i = 1; i <= a; i  ) {
            sum  = 1 / Math.pow(i, b);
        }
        System.out.println(sum);
    }
}
  • Related