| Bytes | Lang | Time | Link |
|---|---|---|---|
| 034 | TIBASIC TI83 Plus | 250516T145558Z | madeforl |
| 043 | Juby | 250502T031307Z | Jordan |
| 050 | Swift 6 | 250505T131116Z | macOSist |
| 043 | Python 3.8 prerelease | 250502T022730Z | Lucenapo |
| 014 | Uiua | 240701T145732Z | noodle p |
| 010 | Japt | 240822T213450Z | Shaggy |
| 012 | cQuents | 240609T233624Z | Stephen |
| 006 | Vyxal | 240615T012600Z | lyxal |
| 051 | PHP | 240614T222508Z | luciande |
| 034 | Python 3 | 240611T165355Z | Jonathan |
| 034 | Octave | 240611T203644Z | Ka Wa Yi |
| 036 | R | 240610T132146Z | Rui Barr |
| 027 | Ruby | 240611T075034Z | G B |
| 043 | Desmos | 240610T114309Z | Aiden Ch |
| 007 | 05AB1E | 240610T104528Z | Kevin Cr |
| 011 | Brachylog | 240610T083824Z | Fatalize |
| 043 | Wolfram Language Mathematica | 240610T065632Z | Greg Mar |
| 057 | Google Sheets | 240610T015955Z | z.. |
| 011 | MATL | 240609T223142Z | Luis Men |
| 036 | Python 3 | 240609T192506Z | xnor |
| 007 | Jelly | 240609T200905Z | Jonathan |
| 037 | PowerShell Core | 240609T204159Z | Julian |
| 041 | Arturo | 240609T200011Z | chunes |
| 041 | Python | 240609T185712Z | corvus_1 |
| 018 | Charcoal | 240609T184936Z | Neil |
| 031 | Haskell | 240609T184119Z | corvus_1 |
| 029 | JavaScript ES6 | 240609T172040Z | Arnauld |
| 008 | Vyxal 3 | 240609T171030Z | RubenVer |
TI-BASIC (TI-83 Plus), 34 bytes
While 1
While C≤N
I+1→I
C+1/I→C
End
Disp I
N+1→N
End
outputs forever
Swift 6, 50 bytes
let h={$0<1 ?$1-1.0:h($0-1.0/$1,$1+1)},f={h($0,2)}
f(_:) is the function to call.
Python 3.8 (pre-release), 43 bytes
f=lambda n,t=1,k=2:n<t or-~f(k*n-t,k*t,k+1)
A modification of Jonathan Allan's answer that does not have any floating point limitations (uses ints only).
Uiua, 15 14 bytes
⍢(⊙+⟜÷⤚+1)⋅≥∩0
Nobody tried to beat my Uiua score in the three weeks since I posted this challenge, so I decided to just leave it here for reference.
This leaves k at the top of the stack, with the actual harmonic number below it and the input below that.
Here's the code commented with a translation to traditional imperative code:
⍢(⊙+⟜÷⤚+1)⋅≥∩0
∩0 # k = 0, H = 0, n = input
⍢( )⋅≥ # while H >= n:
+1 # k += 1
⊙+⟜÷⤚ # H += 1/k
💎
Code explanation created with the help of Luminespire.
Uiua is a tacit language, so that translation isn't one-to-one, but it's close enough.
cQuents, 13 12 bytes
$0:#$<bN
;/$
-1 because I remembered I had added a unary / for reciprocal
Explanation
First line:
$0 set starting index to 0
: mode: sequence - given input n, return the nth term
each term equals
# the first value of N such that:
$ the current index
< <
b the second line ( )
N N
Second line:
; mode: series - given input n, return the sum of the first n terms
each term equals
/ 1 /
$ current index
Vyxal, 6 bytes
↵ʀ˦⌈ḟ
You know me, gotta be posting the most stupid and inefficent answers. Goes slow for inputs that make \$10^x\$ very large. 1-indexed.
Uses some lucky math coincidences to get 6 bytes instead of 7 doing it the normal way.
Explained
↵ʀ˦⌈ḟ
↵ʀ # Push the range [0, 10 ** n]. As noted on the OEIS,
# For n >= 1, log(n + 1/2) + gamma < H(n) < log(n + 1/2) + gamma + 1/(24n^2), where gamma is Euler's constant
# (source: https://oeis.org/A002387 under the comments section)
# Luckily, for n >= 1, 10 ** n > that. Therefore, it's guaranteed
# that the answer is contained within the range.
# This is also why the program takes a while for larger inputs.
Ė # Reciprocals of each number in that range
¦ # Cumulative sums of those reciprocals
⌈ # And take the floor of each cumulative sum.
ḟ # Find the first index of the input in that list.
# This is why the output is 1-indexed.
# It's essentially finding the point where the harmonic number is first > n - 1
💎
Created with the help of Luminespire.
Python 3, 34 bytes
f=lambda n,k=2:n<1or-~f(n-1/k,k+1)
A recursive function that accepts \$n\$ (0-indexed) and returns the required number of terms.
How?
Count the number of non-unit terms (\$\frac{1}{2}, \frac{1}{3}, ...\$) we need to subtract to reach a number below one and add one.
One may think that this could overshoot for some \$n\$, but that would only happen if some harmonic number other than \$0\$ or \$1\$ were an integer, which is not the case due to Bertrand's postulate - see "proof 2" here.
Uses the fact that in Python True quacks like 1.
f=lambda , : # f is a function that accepts
n # n
k=2 # and, optionally, k defaulting to 2:
n<1 # is n less than one?
or # if not...
f( , ) # call f with
n-1/k # n reduced by the current term (1/k)
k+1 # and k incremented -> C
~ # bitwise NOT -> -1 - C
- # negate -> -(-1 - C) = C + 1
Octave, 34 bytes
f=@(n)find(cumsum(1./(1:4^n))>n,1)
Explanation
Vectorization + cumulative sum. Can evaluate a(13) easily.
Use the 'power of two' inequality:
$$H_k = 1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\frac{1}{5}+\frac{1}{6}+\frac{1}{7}+\frac{1}{8}+ \cdots +\frac{1}{k}$$ is greater than $$T_k = 1+\frac{1}{2} + \left(\frac{1}{4}+\frac{1}{4}\right)+\left(\frac{1}{8}+\frac{1}{8}+\frac{1}{8}+\frac{1}{8}\right) + \cdots +\frac{1}{k}$$
So \$T_k > N\$ implies \$H_k > N\$. For the original question, the smallest \$k\$ cannot be greater than \$2^{2N}\$.
R, 36 bytes
\(n){k=0;while(F<=n)F=F+1/(k=k+1);k}
05AB1E, 7 bytes
∞.ΔLzO‹
Given a 0-based \$n\$, outputs the \$n^{th}\$ value.
Try it online or verify the infinite list.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
.Δ # Pop and find the first value which is truthy for:
L # Pop and push a list in the range [1,value]
z # Convert each inner value v to its reciprocal 1/v
O # Sum this list of reciprocals
‹ # Check if the (implicit) input-integer is smaller than this
# (after which the found result is output implicitly)
Brachylog, 11 bytes
≤.⟦₁/₁ᵐ+>?∧
Takes n as input.
Explanation
≤. n ≤ k, k is the output
.⟦₁ Take the range [1, …, k]
/₁ᵐ Map inverse: [1, …, 1/k]
+>? The sum of inverses must be greater than n
∧ (Implicitely find a value of k that fits these constraints)
Wolfram Language (Mathematica), 43 bytes
Ceiling@*InverseFunction[HarmonicNumber]@*N
Try it online! (for some reason TIO returns 1 instead of 2 for the input 1; it works correctly on my computer)
The inevitable built-in. Mathematica can calculate HarmonicNumber\$(x)\$ for any real number \$x>-1\$, and thus it can calculate the exact InverseFunction of that function. N makes it treat the input as a real number rather than an integer, and Ceiling rounds up to the nearest integer.
Yet again I wish I understood how to install and use Sledgehammer....
Google Sheets, 57 bytes
Expects \$n\$ in A1 (\$0\$-indexed)
=LET(R,LAMBDA(R,s,i,IF(s>A1,i-1,R(R,s+1/i,i+1))),R(R,,1))
Reaches calc limit starting from \$n=10\$ (\$a(n) = 12367\$).
MATL, 11 bytes
`1@:/sG>~}@
Inputs n, outputs a(n).
How it works
` % Do...while
1 % Push 1
@ % Push iteration index k, starting at 1
: % Range: gives [1 2 ... k]
/ % Inverse, element-wise
s % Sum: gives H_k
G % Push n
> % Greater than?
~ % Negate. This is the loop condition
} % Finally (execute on loop exit)
@ % Push k of the last iteration
% End (implicit)
% Display stack (implicit)
Python 3, 36 bytes
f=lambda n,k=1:n>=0and-~f(n-1/k,k+1)
Outputs zero-indexed, which is how the OEIS is defined.
43 bytes
t=k=1
while[t%1*k<=1!=print(k)]:k+=1;t+=1/k
Prints forever
Jelly, 7 bytes
İ€S>ð1#
A monadic Link that accepts a non-negative integer and yields a singleton list containing a positive integer. Or a full program that prints the result.
How?
İ€S>ð1# - Link: non-negative integer, N
# - starting with k=N increment k finding the...
1 - ...first 1 k for which:
ð - the dyadic chain - f(k, N):
İ€ - inverse each of {[1..k]}
S - sum
> - is greater than {N}?
Arturo, 41 bytes
g:$[n k][(n<1)?->k->g n-1//<=k+1]$=>[g&1]
A port of Arnauld's JavaScript answer. Arturo doesn't have default arguments, so two functions are necessary where the second calls the first with a fixed k.
Charcoal, 18 bytes
NθW¬‹θ⁰«→≧⁻∕¹ⅈθ»Iⅈ
Try it online! Based on @Arnauld's answer but I chose to make mine 0-indexed. Explanation:
Nθ
Input n.
W¬‹θ⁰«
Until n is negative...
→≧⁻∕¹ⅈθ
... subtract the next unit fraction from n.
»Iⅈ
Output k.
Haskell, 31 bytes
(0!)
k!n|n<1=k|k<-1+k=k!(n-1/k)
Ungolfed:
f n = (0!n)
(k!n)
| n<1 = k
| otherwise = let k' = 1+k in k' ! (n-1/k')
A port of @Arnauld's answer.
JavaScript (ES6), 29 bytes
Returns the \$n\$-th term, 1-indexed.
f=(n,k=0)=>n<1?k:f(n-1/++k,k)
Vyxal 3, 8 bytes
λɾė∑⁰>}“
λɾė∑⁰>}“
“ # Find first positive integer such that
∑ # the sum of
ė # the reciprocals of
ɾ # the numbers from 1 to the integer
> # is greater than
⁰ # the input
💎
Created with the help of Luminespire.