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
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Partial Sums of Harmonic Series

+12
−0

Given an positive integer $n$, return the least positive 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

12 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

Vyxal, 7 bytes

ɾĖ∑?≥)ṅ

Try it Online! Vyxal has arbitrary-precision rationals, so this will never run into precision errors.

      ṅ # Find the first integer
-----)  # where
  ∑     # sum of
 Ė      # reciprocals of
ɾ       # range from 1 to n inclusive
    ≥   # is at least
   ?    # the input
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+6
−0

Japt, 11 10 bytes

>0©ÒßUÉ/°T

Try it

>0©ÒßUÉ/°T     :Implicit input of integer U
>0             :Greater than 0?
  ©            :Logical AND with
   Ò           :Negate the bitwise NOT of (i.e., increment)
    ß          :Recursive call with argument
     UÉ/       :  U minus 1 divided by ...
        °T     :  T (initially 0) prefix incremented
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+5
−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)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+5
−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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+5
−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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Scala, 55 bytes

(Stream from 1 map 1.0./scanLeft.0)(_+_)indexWhere _.<=

Try it online!

Written in a more sane manner:

n => Stream.from(1).map(1.0/_).scanLeft(0.0)(_+_).indexWhere(_ >= n)

Explanation:

(
  Stream from 1       //Make an infinite list of integers, starting at 1
    map 1.0./         //Find the reciprocal of each (divide 1.0)
    scanLeft .0)(_+_) //Sum each partial sequence
indexWhere            //Find the index where
_.<=                  //n (underscore) is less than or equal to a partial sum
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 8 bytes

İ€S<¬ʋ1#

Try it online!

How it works

İ€S<¬ʋ1# - Main link. Takes n on the left
     ʋ   - Group the previous 4 links into a dyad f(k, n):
 €       -   Over each integer 1 to k:
İ        -     Get its reciprocal
  S      -   Sum
   <     -   Less than n?
    ¬    -   Logical not
      1# - Find the first integer k ≥ n such that f(k, n) is true
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 23 bytes

>:@]^:(>1#.1%1+i.)^:_&1

Try it online!

>:@]^:(...)^:_&1   Increments from 1 while
>1#.1%1+i.         the harmonic sum up to that is less than the input
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript, 29 bytes

f=(n,x=0)=>n>0?f(n-1/++x,x):x

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Goruby, 38 bytes

->n{dw{$.+=1;$..mp{1.0/-~_1}.su<n};$.}

This was not as much fun as I thought it would be, but it was fun enough to use once.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »