20
\$\begingroup\$

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


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 , so the shortest code in bytes wins!

\$\endgroup\$
7
  • \$\begingroup\$ Could you give us some testcases? \$\endgroup\$ Commented May 28, 2017 at 9:50
  • 2
    \$\begingroup\$ What 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? \$\endgroup\$ Commented May 28, 2017 at 9:52
  • 2
    \$\begingroup\$ @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 than 99999999999.9999999999 \$\endgroup\$ Commented May 28, 2017 at 10:17
  • \$\begingroup\$ Can we go over 5 significant figures? \$\endgroup\$ Commented May 28, 2017 at 10:25
  • \$\begingroup\$ 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.) \$\endgroup\$ Commented May 28, 2017 at 19:53

44 Answers 44

1
2
0
\$\begingroup\$

Braingolf, 20 bytes [non-competing]

VR1-1[1,!/M,$_1+]v&+ 

This won't actually work due to braingolf's inability to work with floats, however the logic is correct.

Explanation:

VR1-1[1,!/M,$_1+]v&+ Implicit input VR Create new stack and return to main stack 1- Decrement input 1 Push 1 [..........] Loop, always runs once, then decrements first item on stack at ] Breaks out of loop if first item on stack reaches 0 1,!/ Push 1, swap last 2 values, and divide without popping Original values are kept on stack, and result of division is pushed M,$_ Move result of division to next stack, then swap last 2 items and Silently pop last item (1) 1+ Increment last item on stack v&+ Move to next stack, sum entire stack Implicit output of last item on current stack 

Here's a modified interpreter that supports floats. First argument is input.

\$\endgroup\$
0
\$\begingroup\$

GolfScript, 17 bytes

~),{2.-1??./\/+}* 

Try it online!

1-indexed

~ # Parse n to an integer ), # Make an array from 0 to n { }* # For each number in the array, skipping the 0 2.-1?? # 2^(2^-1) = sqrt( 2 ) We just need any float ./ # Divide previous number by itself, this will result in the float 1.0 \/ # Divide 1.0 by the current number of the array + # Add the result 

Without the convertion to float it could be reduced to 10 bytes, but the output would be a fraction.

~),{-1?+}* 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pip, 6 bytes

$+/\,a 

Try it online!

Explanation

 a First command-line argument \, Inclusive range from 1 through that number / Invert each number in the range $+ Fold on + (summing the list) Autoprint (implicit) 
\$\endgroup\$
0
\$\begingroup\$

Ly, 14 12 bytes

n[:1f/f1-]&+ 

Try it online!

Found a simpler way to do this... The original was: 0ns[1f/+l1-s]p

And here's what it's doing.

n 

Get 'n' from the challenge (the number of iterations) from the input and push onto the stack.

[...] 

As long as the top of the stack is non-zero.

:1f/ 

Duplicate the top of the stack (the iteration counter), and compute 1/n.

f1- 

Flip the two entries on the stack so that the 1/n calculation is second and the iteration counter is on the top. Then decrement the iteration counter.

&+ 

Once all the values have been computed, sum the stack and print.

\$\endgroup\$
0
\$\begingroup\$

Excel (2016), 28 bytes

=SUM(1/ROW(OFFSET(A1,,,A1))) 

Excel (current), 20 bytes

=SUM(1/SEQUENCE(A1)) 
\$\endgroup\$
0
\$\begingroup\$

Desmos, 16 bytes

total(1/[1...n]) 

Try it on desmos!

Uses the total function instead of sigma notation to be able to shave off the latex. Also uses desmos's built-in vectorization.

\$\endgroup\$
0
\$\begingroup\$

HP 28S, 18 bytes

0 1 ROT FOR K K INV + NEXT 
\$\endgroup\$
0
\$\begingroup\$

Raku, 15 bytes

{sum 1 X/1..$_} 

Try it online!

1 X/ 1..$_ is 1 cross-divided by the numbers from 1 up to the input number $_, producing the list 1, 1/2, 1/3, ..., 1/$_. Then (of course) sum sums them.

\$\endgroup\$
0
\$\begingroup\$

J-uby, 16 bytes

:+|:sum+(:/&1.0) 

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

><>, 17 bytes

0$:@?!n$:1-}1$,+! 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Itr, 4 bytes

#¹¯S

outputs result as fraction

online interpreter

append 0e* for floating point output

Explanation

# ; read number from stdin ¹ ; 1-based range ¯ ; replace elements with their reciprocals S ; sum ; implicit output 
\$\endgroup\$
0
\$\begingroup\$

Janet, 30 bytes

|(sum(seq[i :down[$ 0]](/ i))) 
\$\endgroup\$
0
\$\begingroup\$

Tcl, 38 bytes

proc h x {expr $x?1./$x+\[h ($x-1)]:0} 

Try it online!


# [Tcl], 50 bytes
proc h x {time {append s +1./[incr i]} $x;expr $s} 

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Swift 6, 30 bytes

let h={$0<1.0 ?0:1/$0+h($0-1)} 

Try it on SwiftFiddle!

\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.