# How far should I sum?

The harmonic series is the "infinite sum" of all the fractions of the form $$\\frac1n\$$ for $$\n\$$ positive integer. I.e. the harmonic series is

$$\frac11 + \frac12 + \frac13 + \frac14 + \cdots$$

It is well-known that this sum diverges, which means that if you define

$$H_n = \frac11 + \frac12 + \cdots + \frac1n$$

Then the value of $$\H_n\$$ goes to infinity. This can also be stated in a different way: for any positive value $$\x\$$ you pick, there is some value $$\N\$$ such that, to the right of $$\N\$$, the $$\H\$$ values are bigger than $$\x\$$:

$$\forall\ x\ \exists\ N: n > N \implies H_n > x$$

# Your task

Write a program/function that takes as input a positive number x (not necessarily an integer) and outputs the first integer n for which

$$H_n \geq x$$

In theory your program must work for arbitrary x, even if in practice you are limited by the memory and/or precision of your programming language/computer.

# Test cases

1.5 -> 2
2 -> 4
3 -> 11
3.1415 -> 13
4 -> 31
5 -> 83
7.5 -> 1015
10 -> 12367

This is so shortest solution wins! Standard loopholes are forbidden by default. Happy golfing.

• Feb 9 '20 at 23:32
• @Arnauld included for completeness? Or do you think it might help someone in any way?
– RGS
Feb 9 '20 at 23:43
• Your recent challenges have been great! Keep up the good work! Feb 10 '20 at 0:48
• @S.S.Anne thanks :D I'll try to come up with more decent challenges.
– RGS
Feb 10 '20 at 7:27
• You ask: "how far should I go". I suggest anywhere between 6 to 8 metres. :P Feb 10 '20 at 10:26

# APL (Dyalog), 13 bytes

-1 bytes thanks to ngn

{⊃⍸⍵≤+\÷⍳⌈*⍵}

Try it online!

A dfn solution that takes a right argument.

### Explanation:

{           }   ⍝ dfn
⊃              ⍝ Take the first of
⍸             ⍝ The indexes of the truthy values of
⍵≤           ⍝ The right argument is smaller than or equal to
\+         ⍝ The cumulative sum
÷        ⍝ The reciprocal of each of
⍳       ⍝ The range 1 to
⌈      ⍝ The ceiling of
*⍵    ⍝ e to the power of the right argument
• 1⍳⍨ -> ⊃⍸­­
– ngn
Feb 10 '20 at 0:43
• What does dfn stand for? Also, be sure to upvote the challenge if you liked solving it :)
– RGS
Feb 10 '20 at 7:19
• @RGS dfn stands for Direct Function
– Jo King
Feb 10 '20 at 7:50
• Sorry, but the first thing I saw in your answer was this face: {⊃≥⍵≤⊃} Feb 10 '20 at 16:48

# Python 3, 35 bytes

f=lambda x,n=1:x>0and-~f(x-1/n,n+1)

Try it online!

• Thanks for the submission. When X <= 0 the LHS evaluates to false. How does the lambda return n at that point? Also be sure to upvote if you liked golfing this challenge.
– RGS
Feb 10 '20 at 7:29
• I don’t understand why the complement operator makes the and return the number instead of False. What happens there? Feb 10 '20 at 13:41
• @ÉmileJetzer The -~ is basically a shorter variation of +1. Since it's a recursive function that eventually results in False, the -~/+1 will interpret this as 0 in Python and adds the 1 (False+1 = 1 in Python). Here a step-by-step explanation of what happens for f(2). Feb 10 '20 at 16:49
• @KevinCruijssen that makes a whole lot'a sense! Thanks for explaining it and going through the effort of explaining it step by step.
– RGS
Feb 10 '20 at 22:04
• @KevinCruijssen Thanks for detailed breakdown! The idea of incrementing the recursive output starting from a base case of 0, as a replacement for outputting the final value n, is a pattern that I find to come up a lot. For example, when finding the n'th number meeting a condition p, as in my tip here, we can use f=lambda n,i=1:n and-~f(n-p(i),i+1), which looks very similar to this answer.
– xnor
Feb 11 '20 at 3:10

# Perl 6, 27 bytes

{+([\+](1 X/1..*)...*>=$_)} Try it online! ### Explanation: { } # Anonymous code block taking one argument +( ) # Return the length of [\+]( ) # The cumulative sum 1 X/ # Of the reciprocal of all of 1..* # 1 to infinity ... # Take from this until *>=$_     # It is larger than or equal to the input
• I should learn some Perl! +1 Thanks for including an explanation of the code.
– RGS
Feb 9 '20 at 23:00
• @RGS Note that these days Perl 6 is called Raku (TIO still uses Perl 6 hence most submissions use it). It was designed originally as a non-backwards compatible successor to Perl 5, but now both still live on with different names — Perl 5 is just Perl, and Perl 6 is Raku. I'm biased since I do a lot of (non golfing) work in it, but it's really one of the more beautiful and fun languages to write general code in, which sounds weird given the (unfair) image that Perl 5 had. Feb 12 '20 at 4:06
• @user0721090601 thank you for your testimonial. Tbh, I heard some "mean" things about Perl but I never programmed in it! Neither Perl <= 5, nor Perl 6 :)
– RGS
Feb 12 '20 at 7:12

Try it online!