java 级数求和

调和级数Harmonic numbers,H(n)= 1/1 + 1/2 + 1/3 + 1/4 + ... + 1/n。求调和级数前n项的和

public static void main(String[] args) {
        System.out.println(harmonic(15));
    }
    public static double harmonic (int n) {  
        double sum = 0.0;  
        for (int i = 1; i <= n; i++) {  
            sum += 1.0 / i;  
        }  
        return sum;          
    }  

你可能感兴趣的:(java 级数求和)