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 Find n Niven Numbers

Scala, 52 bytes Stream.from(1).filter(x=>x%(x+"":\0)(_+_-48)<1).take Try it in Scastie! Woo, 3 deprecation warnings! (okay, one of them could be fixed but I like to live dangerously) St...

posted 2y ago by user‭  ·  edited 2y ago by user‭

Answer
#2: Post edited by user avatar user‭ · 2022-08-12T13:19:42Z (over 2 years ago)
  • # Scala, 52 bytes
  • ```scala
  • Stream.from(1).filter(x=>x%(x+"":\0)(_+_-48)<1).take
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/oMucQkxdSB23HjP0mYI8Pg)
  • Woo, 3 deprecation warnings! (okay, one of them could be fixed but I like to live dangerously)
  • # Scala, 52 bytes
  • ```scala
  • Stream.from(1).filter(x=>x%(x+"":\0)(_+_-48)<1).take
  • ```
  • [Try it in Scastie!](https://scastie.scala-lang.org/oMucQkxdSB23HjP0mYI8Pg)
  • Woo, 3 deprecation warnings! (okay, one of them could be fixed but I like to live dangerously)
  • `Stream.from(1)` constructs an infinite list of positive integers. Then we `filter` the `x`'s that are Niven numbers. `(x+"":\0)(_+_-48)` finds the sum of the `x`'s digits. A clearer way to write that would be `x.toString.foldRight(0)((sum, char) => sum + char.toInt - '0'.toInt)`. At the end, only the first `n` Niven numbers are `take`n from the infinite list.
#1: Initial revision by user avatar user‭ · 2022-08-12T13:16:09Z (over 2 years ago)
# Scala, 52 bytes

```scala
Stream.from(1).filter(x=>x%(x+"":\0)(_+_-48)<1).take
```

[Try it in Scastie!](https://scastie.scala-lang.org/oMucQkxdSB23HjP0mYI8Pg)

Woo, 3 deprecation warnings! (okay, one of them could be fixed but I like to live dangerously)