| Bytes | Lang | Time | Link |
|---|---|---|---|
| 030 | Swift 6 | 250501T153428Z | macOSist |
| 038 | Tcl | 170528T185732Z | sergiol |
| 030 | Janet | 250430T173825Z | xigoi |
| 004 | Itr | 230812T171615Z | bsoelch |
| 002 | Thunno 2 S | 230809T182557Z | The Thon |
| 047 | Scala | 230429T074650Z | 138 Aspe |
| 017 | ><> | 221217T190705Z | Emigna |
| 003 | Pyt | 221217T014558Z | Kip the |
| 016 | Juby | 221215T213315Z | Jordan |
| 015 | Raku | 221115T225712Z | Sean |
| 009 | J | 221115T223514Z | south |
| 005 | MATL | 170528T143416Z | Suever |
| 018 | HP 28S | 210720T144528Z | Count Ib |
| 016 | Desmos | 210602T171013Z | Undersla |
| 028 | Excel 2016 | 210518T175609Z | Axuary |
| 012 | Ly | 210518T041551Z | cnamejj |
| 006 | Pip | 210518T035015Z | DLosc |
| 003 | Vyxal | 210518T011905Z | Undersla |
| 017 | GolfScript | 201012T175531Z | 2014MELO |
| 036 | Rust | 201012T162259Z | Aiden4 |
| 003 | Husk | 201012T064000Z | Razetime |
| 008 | Gol><> | 180503T073915Z | Bubbler |
| 003 | Japt x | 170528T105554Z | Shaggy |
| 020 | Braingolf | 170530T092142Z | Mayube |
| 013 | QBIC | 170530T083026Z | steenber |
| 035 | C gcc | 170530T072109Z | Giacomo |
| 006 | Brachylog | 170529T094752Z | Fatalize |
| 021 | Haskell | 170529T085235Z | Uri Gore |
| 003 | Jelly | 170528T102004Z | Erik the |
| 011 | CJam | 170528T103238Z | Erik the |
| 003 | 05AB1E | 170528T103455Z | Erik the |
| 005 | Pyth | 170528T103809Z | Erik the |
| 054 | C | 170528T201834Z | Uriel |
| 038 | Tcl | 170528T194848Z | avl42 |
| 016 | Mathematica | 170528T101650Z | ZaMoC |
| 034 | Axiom | 170528T112511Z | user5898 |
| 018 | JavaScript | 170528T104022Z | user4180 |
| 010 | CJam | 170528T114809Z | Luis Men |
| 015 | R | 170528T153051Z | Nitrodon |
| 020 | Haskell | 170528T145640Z | Ryan McC |
| 018 | Pari/GP | 170528T101717Z | alephalp |
| 005 | APL Dyalog | 170528T101725Z | user4180 |
| 033 | PHP | 170528T103823Z | Jör |
| 027 | Python 3 | 170528T103213Z | shooqie |
Tcl, 38 bytes
proc h x {expr $x?1./$x+\[h ($x-1)]:0}
proc h x {time {append s +1./[incr i]} $x;expr $s}
Janet, 30 bytes
|(sum(seq[i :down[$ 0]](/ i)))
Itr, 4 bytes
#¹¯S
outputs result as fraction
append 0e* for floating point output
Explanation
# ; read number from stdin
¹ ; 1-based range
¯ ; replace elements with their reciprocals
S ; sum
; implicit output
Thunno 2 S, 2 bytes
RỊ
Explanation
RỊ # Implicit input
R # One range
Ị # Reciprocal
# Take the sum
# Implicit output
Pyt, 3 bytes
ř⅟Ʃ
implicit input
ř produces a list containing 1 to n
⅟ takes the reciprocals
Ʃ sums the list
(implicit print)
Raku, 15 bytes
{sum 1 X/1..$_}
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.
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
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
HP 28S, 18 bytes
0 1 ROT FOR K K INV + NEXT
Desmos, 16 bytes
total(1/[1...n])
Uses the total function instead of sigma notation to be able to shave off the latex. Also uses desmos's built-in vectorization.
Excel (2016), 28 bytes
=SUM(1/ROW(OFFSET(A1,,,A1)))
Excel (current), 20 bytes
=SUM(1/SEQUENCE(A1))
Ly, 14 12 bytes
n[:1f/f1-]&+
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.
Pip, 6 bytes
$+/\,a
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)
Vyxal, 3 bytes
ɾĖ∑
Lame answer, but range -> reciprocal -> sum -> implicit out, same as jelly
Vyxal s, 2 bytes
ɾĖ
2 Bytes w/ -s from @lyxal, -s sums the stack
GolfScript, 17 bytes
~),{2.-1??./\/+}*
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?+}*
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.
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
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.
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
Brachylog, 6 bytes
⟦₁/₁ᵐ+
This is 1-indexed.
Explanation
⟦₁ Range [1, …, Input]
ᵐ Map:
/₁ Inverse
+ Sum
Haskell, 21 bytes
f n=sum$map(1/)[1..n]
Jelly, 3 bytes
İ€S
1-indexed.
Explanation:
İ€S Main link, monadic
İ€ 1 / each one of [1..n]
S Sum of
C, 54 bytes
i;float f(n){float s;for(i=n+1;--i;s+=1./i);return s;}
Uses 1-indexed numbers.
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.
Mathematica, 21 20 16 bytes
This solution is 1-indexed.
Sum[1./i,{i,#}]&
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
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))
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
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.
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
Python 3, 27 bytes
h=lambda n:n and 1/n+h(n-1)