Challenge
Given a positive integer N
, output the sum of the first N
reciprocals as an exact fraction, which is represented as a pair of integers in a consistent order representing numerator and denominator.
Rules
Output must be exact.
Output should be as a pair of integers in a consistent order representing numerator and denominator.
Using non-integer numeric types (built-in or library) is forbidden.
- Clarification/exception: non-integer numeric types are okay if and only if all values used, computed, and returned are integers (i.e. your language uses rational numbers by default, but you only use integer arithmetic in your answer)
Output should be as reduced as possible. (
3/2
is okay,6/4
is not)Standard loopholes are forbidden.
Submissions should work for inputs at least up to 20, or this meta, whichever is higher.
Test Cases
1: 1/1
2: 3/2 (1/1 + 1/2)
3: 11/6 (1/1 + 1/2 + 1/3)
4: 25/12 etc.
5: 137/60
6: 49/20
20: 55835135/15519504
56: 252476961434436524654789/54749786241679275146400
226: 31741146384418617995319820836410246588253008380307063166243468230254437801429301078323028997161/5290225078451893176693594241665890914638817631063334447389979640757204083936351078274058192000
Test-Case Generation (Python 3)
import fractions
def f(x):
return sum(fractions.Fraction(1,i) for i in range(1,x+1))
Similar to this challenge and this challenge.
Numerators are OEIS A001008, and denominators are OEIS A002805.
gcd
a "built-in function" if your language provides it? \$\endgroup\$gcd
and other built-in functions are fine. Rational/fractional types are not allowed. \$\endgroup\$