Definition
In Mathematics, Harmonic Sequence refers to a sequence where
$$a_n = \frac 1 n$$
i.e. the \$n_{th}\$ 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
$$\sum_{i=1}^n \frac 1 i$$
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!
44 Answers 44
Python 3, 27 bytes
h=lambda n:n and 1/n+h(n-1)
-
\$\begingroup\$ 0-indexing or 1-indexing? \$\endgroup\$Arjun– Arjun2017年05月28日 11:29:58 +00:00Commented May 28, 2017 at 11:29
-
2\$\begingroup\$ Throws
RuntimeError
when handling input greater than the recursion limit, 1000 by default. \$\endgroup\$sagiksp– sagiksp2017年05月28日 15:04:09 +00:00Commented May 28, 2017 at 15:04 -
\$\begingroup\$ you can do
sys.setrecursionlimit(473755252663)
but the stack will eventually overflow quite easily \$\endgroup\$cat– cat2017年05月29日 04:31:19 +00:00Commented May 29, 2017 at 4:31 -
\$\begingroup\$ @Arjun it's 1-indexed \$\endgroup\$shooqie– shooqie2017年05月29日 20:23:33 +00:00Commented May 29, 2017 at 20:23
JavaScript, (削除) 19 (削除ここまで) 18 bytes
1 byte saved thanks to @RickHitchcock
f=a=>a&&1/a+f(--a)
This is 1-indexed.
f=a=>a&&1/a+f(--a)
for(i=0;++i<10;)console.log(f(i))
-
\$\begingroup\$ From what I've seen of other posts, you can remove
f=
from your answer to save 2 bytes. \$\endgroup\$Rick Hitchcock– Rick Hitchcock2017年05月28日 10:44:06 +00:00Commented May 28, 2017 at 10:44 -
1\$\begingroup\$ @RickHitchcock I cannot remove
f=
because the function is recursive and it references itself inf(--a)
. But if this was not a recursive solution, I would have been able to do that \$\endgroup\$user41805– user418052017年05月28日 10:45:30 +00:00Commented May 28, 2017 at 10:45 -
\$\begingroup\$ Ah, makes sense! Save one byte with
f=a=>a&&1/a+f(--a)
. \$\endgroup\$Rick Hitchcock– Rick Hitchcock2017年05月28日 10:49:52 +00:00Commented May 28, 2017 at 10:49 -
\$\begingroup\$ @RickHitchcock Nice one! \$\endgroup\$user41805– user418052017年05月28日 10:52:13 +00:00Commented May 28, 2017 at 10:52
APL (Dyalog), 5 bytes
+/÷∘⍳
You can add ⎕PP←{number}
to the header to change the precision to {number}
.
This is 1-indexed.
Explanation
+/÷∘⍳ Right argument; n
⍳ Range; 1 2 ... n
÷ Reciprocal; 1/1 1/2 ... 1/n
+/ Sum; 1/1 + 1/2 + ... + 1/n
Mathematica, (削除) 21 (削除ここまで) (削除) 20 (削除ここまで) 16 bytes
This solution is 1-indexed.
Sum[1./i,{i,#}]&
-
\$\begingroup\$ It is 1-indexing \$\endgroup\$ZaMoC– ZaMoC2017年05月28日 11:31:07 +00:00Commented May 28, 2017 at 11:31
-
1\$\begingroup\$ > You must not use a built-in to calculate the partial sum of the first n elements. (Yeah, it's for you Mathematica!) \$\endgroup\$MCCCS– MCCCS2017年05月28日 11:32:46 +00:00Commented May 28, 2017 at 11:32
-
4\$\begingroup\$ OP means I can't use HarmonicNumber[#] & \$\endgroup\$ZaMoC– ZaMoC2017年05月28日 11:35:56 +00:00Commented May 28, 2017 at 11:35
-
4\$\begingroup\$ And one can further shorten to
Tr[1./Range@#]&
. \$\endgroup\$Greg Martin– Greg Martin2017年05月28日 19:56:23 +00:00Commented May 28, 2017 at 19:56 -
2\$\begingroup\$ @Ian Mathematica may display 5 sig fig, but the function returns machine-precision numbers (52 binary bits or just under 16 decimal digits of precision) \$\endgroup\$LLlAMnYP– LLlAMnYP2017年05月30日 09:12:56 +00:00Commented May 30, 2017 at 9:12
Japt -x
, (削除) 8 (削除ここまで) (削除) 6 (削除ここまで) (削除) 5 (削除ここまで) 3 bytes
õpJ
With some thanks to ETHproductions
-
\$\begingroup\$ 0-indexing or 1-indexing? \$\endgroup\$Arjun– Arjun2017年05月28日 11:27:18 +00:00Commented May 28, 2017 at 11:27
-
\$\begingroup\$ I think you can save a byte with
õ x@1/X
\$\endgroup\$ETHproductions– ETHproductions2017年05月28日 12:25:57 +00:00Commented May 28, 2017 at 12:25 -
\$\begingroup\$ ...and another couple bytes by using
XpJ
instead of1/X
:-) \$\endgroup\$ETHproductions– ETHproductions2017年05月28日 12:27:04 +00:00Commented May 28, 2017 at 12:27 -
\$\begingroup\$ Thanks, @ETHproductions :) I twigged those as soon as I walked away. \$\endgroup\$Shaggy– Shaggy2017年05月28日 12:45:38 +00:00Commented May 28, 2017 at 12:45
-
\$\begingroup\$ Actually I don't think you even need the
_
due to auto-functions. I should really write that tip :P (I should have time today or tomorrow, due to it being Memorial Day) \$\endgroup\$ETHproductions– ETHproductions2017年05月28日 14:50:18 +00:00Commented May 28, 2017 at 14:50
Jelly, 3 bytes
İ€S
1-indexed.
Explanation:
İ€S Main link, monadic
İ€ 1 / each one of [1..n]
S Sum of
CJam, (削除) 11 (削除ここまで) 10 bytes
1 byte removed thanks to Erik the outgolfer
ri),{W#+}*
This uses 1-based indexing.
Explanation
ri e# Read integer, n
) e# Increment by 1: gives n+1
, e# Range: gives [0 1 2 ... n]
{ }* e# Fold this block over the array
W# e# Inverse of a number
+ e# Add two numbers
-
\$\begingroup\$ You can use
W
instead of-1
. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月28日 12:24:44 +00:00Commented May 28, 2017 at 12:24 -
\$\begingroup\$ @EriktheOutgolfer has outgolfed himself :-) \$\endgroup\$Luis Mendo– Luis Mendo2017年05月28日 13:04:00 +00:00Commented May 28, 2017 at 13:04
-
\$\begingroup\$ @LuisMendo I like my name, it's just a name. And yes I outgolfed myself in the process of helping a fellow golfer golf even further. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月28日 13:10:24 +00:00Commented May 28, 2017 at 13:10
-
\$\begingroup\$ @Erik It was meant as a joke. Thanks for the help \$\endgroup\$Luis Mendo– Luis Mendo2017年05月28日 13:57:40 +00:00Commented May 28, 2017 at 13:57
Haskell, 20 bytes
f 0=0
f n=1/n+f(n-1)
Original solution, 22 bytes
f n=sum[1/k|k<-[1..n]]
These solutios assumes 1-indexed input.
Tcl 38 bytes
proc h x {expr $x?1./($x)+\[h $x-1]:0}
That's a very dirty hack, and the recursive calls pass literal strings like "5-1-1-1..." until it evaluates to 0.
-
\$\begingroup\$ Thanks @Christopher for the formatting. With that, the duplication of backslash was no longer necessary. \$\endgroup\$avl42– avl422017年05月28日 20:30:12 +00:00Commented May 28, 2017 at 20:30
-
\$\begingroup\$ No problem! It looks better \$\endgroup\$user63187– user631872017年05月28日 21:30:10 +00:00Commented May 28, 2017 at 21:30
Axiom, (削除) 45 (削除ここまで) 34 bytes
f(x:PI):Any==sum(1./n,n=1..x)::Any
1-Indexed; It has argument one positive integer(PI) and return "Any" that the sys convert (or not convert) to the type useful for next function arg (at last it seems so seeing below examples)
(25) -> [[i,f(i)] for i in 1..9]
(25)
[[1,1.0], [2,1.5], [3,1.8333333333 333333333], [4,2.0833333333 333333333],
[5,2.2833333333 333333333], [6,2.45], [7,2.5928571428 571428572],
[8,2.7178571428 571428572], [9,2.8289682539 682539683]]
Type: List List Any
(26) -> f(3000)
(26) 8.5837498899 591871142
Type: Union(Expression Float,...)
(27) -> f(300000)
(27) 13.1887550852 056117
Type: Union(Expression Float,...)
(29) -> f(45)^2
(29) 19.3155689383 88117644
Type: Expression Float
MATL, 5 bytes
:l_^s
This solution uses 1-based indexing.
Try it at MATL Online
Explanation
% Implicitly grab input (N)
: % Create an array from [1...N]
l_^ % Raise all elements to the -1 power (take the inverse of each)
s % Sum all values in the array and implicitly display the result
C, 54 bytes
i;float f(n){float s;for(i=n+1;--i;s+=1./i);return s;}
Uses 1-indexed numbers.
Haskell, 21 bytes
f n=sum$map(1/)[1..n]
Brachylog, 6 bytes
⟦1/1m+
This is 1-indexed.
Explanation
⟦1 Range [1, ..., Input]
m Map:
/1 Inverse
+ Sum
QBIC, 13 bytes
[:|c=c+1/a]?c
Explanation
[ | FOR a = 1 to
: the input n
c=c+ Add to c (starts off as 0)
1/a the reciprocal of the loop iterator
] NEXT
?c PRINT c
Gol><>, 8 bytes
F1LP,+|B
Example full program & How it works
1AGIE;GN
F1LP,+|B
1AGIE;GN
1AG Register row 1 as function G
IE; Take input as int, halt if EOF
GN Call G and print the result as number
Repeat indefinitely
F1LP,+|B
F | Repeat n times...
1LP, Compute 1 / (loop counter + 1)
+ Add
B Return
Rust, 36 bytes
|n|(1..=n).map(|n|1./n as f64).sum()
Input is an integer, if the input is <=0 the output is zero, and inputting one gets one.
J, 9 bytes
1#.1%1+i.
I'm only posting this because I think it looks really pretty.
1#.1%1+i.
i. NB. range 0..n-1
1+ NB. add 1
1% NB. 1 divided by each element
1#. NB. sum the result
Pyt, 3 bytes
ř1⁄Ʃ
implicit input
ř produces a list containing 1 to n
1⁄ takes the reciprocals
Ʃ sums the list
(implicit print)
9.9999E10
rather than99999999999.9999999999
\$\endgroup\$