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

Post History

75%
+4 −0
Challenges Partial Sums of Harmonic Series

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(_ &g...

posted 4y ago by user‭

Answer
#1: Initial revision by user avatar user‭ · 2021-03-10T03:25:04Z (almost 4 years ago)
# [Scala], 55 bytes

<!-- language-all: lang-scala -->

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

[Try it online!][TIO-km2vjrh4]

[Scala]: http://www.scala-lang.org/
[TIO-km2vjrh4]: https://tio.run/##JcuxCsIwEAbgvU/xjw1CbMBJrOBY0MnBsZztBSvNNSSHFMRnj6DTN315oJnKcn/yoLjQJOBVWcaMU4x4VxXwohl@j04U7fFPqa@amAJ8WgIcAkU429htHkjO7NU2pu43vZlk5PX24MTo7aEtQEyT6Cy1gy7Y/aY31ad8AQ "Scala – Try It Online"

Written in a more sane manner:
```scala
n => Stream.from(1).map(1.0/_).scanLeft(0.0)(_+_).indexWhere(_ >= n)
```
Explanation:
```scala
(
  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
```