Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics

Dashboard
Notifications
Mark all as read
Challenges

Partial Sums of Harmonic Series

+6
−0

Given an positive integer $n$, return an integer $k$ such that the $k$th partial sum of the harmonic series is greater than or equal to $n$. For example, if $n = 2$, then $k = 4$, because $1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} = \frac{25}{12} > 2$.

More Input/Output Examples

Input -> Output
1 -> 1
3 -> 11
4 -> 31

This is code-golf, so shortest code wins.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comments

6 answers

+2
−0

Raku, 27 bytes

{+([\+](1 X/1..*)...*>=$_)}

Try it online!

{                         }  # Anonymous code block
   [\+](        )            # Get the partial sum of
        1 X/                 # 1 over each of
            1..*             # All positive integers
                 ...*>=$_    # Take from the list until it is bigger than the input
 +(                      )   # And return the length of the list

This will start to suffer precision issues, but you can add .FatRat to work correctly (but much slower)

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment

+1 for FatRat (the link is broken) Razetime‭ 2 months ago

+2
−0

JavaScript (Node.js), 47 bytes

f=(a,b=0,c=a=>a&&1/a+c(a-1))=>c(b)<a?f(a,b+1):b

Try it online!

For large numbers, a rounding error will occur and yield incorrect result. The solution below works for any integer (no rounding errors):

93 bytes

f=(a,i=0n,p=a=>g(a)/c(a),g=(a,b=a)=>a&&c(b)/a+g(~-a,b),c=a=>a?a*c(~-a):1n)=>p(i)<a?f(a,-~i):i

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+2
−0

Japt, 11 bytes

@§XõpJ x}aÄ

Try it

Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

Out of curiosity, I've seen this in a few of your other answers now (using shortcuts instead of one character alternatives) - why Ä at the end instead of 1? Quintec‭ 2 months ago

Sometimes it's a hangover from a previous attempt at a solution, @Quintec, other times it's me trying to keep various tricks fresh in my mind and, occasionally, it's just to show off! Shaggy‭ 2 months ago

+2
−0

Python 3, 35 bytes

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

Try it online!

Subtracts each 1/i in turn from the initial value until the result is no longer positive.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+2
−0

AppleScript, 178 bytes

set n to text returned of (display dialog "" default answer "") as number
set k to 0
set h to 0
repeat
set k to k + 1
set h to h + (1 / k)
if h >= n then exit repeat
end repeat
k

It's really a shame that AppleScript doesn't support explicit stdin.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+0
−0

Stax, 9 bytes

Ç≈f♠É↔X+ö

Run and debug it

Explanation(Unpacked):

wii{um|+;<
w          iterate until a falsy result is reached:
 ii        push iteration number i twice 
   {um     map range [1..i] to their reciprocals
      |+   sum that list
        ;< compare to the input(gets popped)
           implicit output of top of stack
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!