Cody

Problem 46000. Compute the harmonic numbers

Solution 2920051

Submitted on 7 Sep 2020 by Benjamin
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
n = 1; H_correct = 1; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

2   Pass
n = 2; H_correct = 3/2; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

3   Pass
n = 3; H_correct = 11/6; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

4   Pass
n = 4; H_correct = 25/12; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

5   Pass
n = 5; H_correct = 137/60; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

6   Pass
n = 10; H_correct = 7381/2520; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

7   Pass
n = 15; H_correct = 1195757/360360; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

8   Pass
n = 20; H_correct = 55835135/15519504; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

9   Pass
n = 29; H_correct = 9227046511387/2329089562800; assert(abs(HarmonicNumber(n)-H_correct)/H_correct < 1e-12)

10   Pass
filetext = fileread('HarmonicNumber.m'); x = ~isempty(strfind(filetext, 'A002805')) || ~isempty(strfind(filetext, 'A001008')) ||... ~isempty(strfind(filetext, '360360')); assert(~x, 'Illegal approach')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!