13
0
Definition
In Mathematics, Harmonic Sequence refers to a sequence where
i.e. the nth term of the sequence equals the reciprocal of n.
Introduction
In this challenge, given a positive integer n as input, output the Partial Sum of first n terms of the Harmonic Sequence.
Input
You'll be given a positive integer (within the range of numbers supported by your language). It can be either of Signed and Unsigned (depends on you), since the challenge requires only positive integers.
You can take the input in any way except assuming it to be present in a predefined variable. Reading from file, terminal, modal window (prompt()
in JavaScript) etc. is allowed. Taking the input as function argument is allowed as well.
Output
Your program should output the sum of the first n terms of the Harmonic Sequence as a float (or integer if the output is evenly divisible by 1) with precision of 5 significant figures, where n refers to the input. To convey the same in Mathematical jargon, you need to compute
where n refers to the input.
You can output in any way except writing the output to a variable. Writing to screen, terminal, file, modal window (alert()
in JavaScript) etc. is allowed. Outputting as function return
value is allowed as well.
Additional Rules
The input number can be either of 0-indexed or 1-indexed. You must specify that in your post.
You must not use a built-in to calculate the partial sum of the first n elements. (Yeah, it's for you Mathematica!)
You must not abuse native number types to trivialize the problem.
Standard Loopholes apply.
Test Cases
The Test Cases assume the input to be 1-indexed
Input Output
1 1
2 1.5
3 1.8333
4 2.0833
5 2.2833
Winning Criterion
This is code-golf, so the shortest code in bytes wins!
Could you give us some testcases? – user41805 – 2017-05-28T09:50:01.370
2What precision is required? Exact output is generally only possible as a fraction, but in many languages that will have to be separate numbers for numerator and denominator. Can we output a)a float, b)a fraction or integer pair c)either? – Level River St – 2017-05-28T09:52:51.840
2@Arjun The harmonic series grows to infinity so it will get hard to meet 10 decimal places as the number gets into the thousands and millions. I would go for significant figures rather than decimal places, and I see no need to be so precise. 5 significant figures should be enough. so
9.9999E10
rather than99999999999.9999999999
– Level River St – 2017-05-28T10:17:04.193Can we go over 5 significant figures? – Erik the Outgolfer – 2017-05-28T10:25:11.487
By the way, it's known that the harmonic sequence does not contain any integers other than the initial a_1 = 1. (Idea of proof that a_n is not an integer for n>1: let 2^k be the largest power of 2 not exceeding n; then 2^k divides the denominator of a_n.) – Greg Martin – 2017-05-28T19:53:42.453
Is it fine if the output is a fraction instead of a float? Clojure's default type in division is a ratio, which is basically a fraction. – clismique – 2017-06-01T10:47:15.723
@Qwerp-Derp No, sorry, that's not allowed. – Arjun – 2017-06-02T06:11:52.197