15
4
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.
Sandbox post (deleted) – pizzapants184 – 2018-07-17T00:18:23.543
related – Leaky Nun – 2018-07-17T01:33:59.090
Is
gcd
a "built-in function" if your language provides it? – Chas Brown – 2018-07-17T01:49:18.383@ChasBrown
gcd
and other built-in functions are fine. Rational/fractional types are not allowed. – pizzapants184 – 2018-07-17T02:02:02.123support range?. – l4m2 – 2018-07-17T03:25:48.753
Can the returned pair of integers be fractional types? My Perl 6 answer only deals with integers, but the default type for numbers is Rat (a fractional type) – Jo King – 2018-07-17T05:49:36.583
1@JoKing That's fine if the numbers are rational type as long as only integers are used. I'll update the question. – pizzapants184 – 2018-07-17T05:54:23.233
@user202729 for the range: at least 20, and this meta
– pizzapants184 – 2018-07-17T07:17:15.930@user202729 numerical errors are not allowed (i.e. the first input value that gives incorrect output values is considered to be outside your answer 's range) – pizzapants184 – 2018-07-17T07:19:22.590
Is the output for 20 shown here wrong? I get 55835135 and assumed my output was wrong, until I saw the top python answer also returns the same thing. – sundar - Reinstate Monica – 2018-07-17T21:57:36.863
@sundar Fixed (it is 55835135, not 55835125) – pizzapants184 – 2018-07-17T23:12:02.427
Yes, that's the value I mentioned in my comment, but you've edited in the wrong value again in the question. Now the numerator says
55825135
, while it should say55835135
(two 3's, no 2's). – sundar - Reinstate Monica – 2018-07-17T23:51:28.523