Cody

# Problem 46000. Compute the harmonic numbers

Solution 2685995

Submitted on 13 Jul 2020 by EmilyR
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)

H = 0 H = 1

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

H = 0 H = 1 H = 1.5000

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

H = 0 H = 1 H = 1.5000 H = 1.8333

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833 H = 2.2833

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833 H = 2.2833 H = 2.4500 H = 2.5929 H = 2.7179 H = 2.8290 H = 2.9290

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833 H = 2.2833 H = 2.4500 H = 2.5929 H = 2.7179 H = 2.8290 H = 2.9290 H = 3.0199 H = 3.1032 H = 3.1801 H = 3.2516 H = 3.3182

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833 H = 2.2833 H = 2.4500 H = 2.5929 H = 2.7179 H = 2.8290 H = 2.9290 H = 3.0199 H = 3.1032 H = 3.1801 H = 3.2516 H = 3.3182 H = 3.3807 H = 3.4396 H = 3.4951 H = 3.5477 H = 3.5977

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

H = 0 H = 1 H = 1.5000 H = 1.8333 H = 2.0833 H = 2.2833 H = 2.4500 H = 2.5929 H = 2.7179 H = 2.8290 H = 2.9290 H = 3.0199 H = 3.1032 H = 3.1801 H = 3.2516 H = 3.3182 H = 3.3807 H = 3.4396 H = 3.4951 H = 3.5477 H = 3.5977 H = 3.6454 H = 3.6908 H = 3.7343 H = 3.7760 H = 3.8160 H = 3.8544 H = 3.8915 H = 3.9272 H = 3.9617

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!