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

Comments on Find n Niven Numbers

Parent

Find n Niven Numbers

+7
−0

Challenge

A Niven number is a positive integer which is divisible by the sum of its digits.

For example, 81 -> 8+1=9 -> 81%9=0.

Your task is to find the first n Niven numbers, given n.

Tests

Reference implementation(takes input)

first 16 values:

0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
6 => 7
7 => 8
8 => 9
9 => 10
10 => 12
11 => 18
12 => 20
13 => 21
14 => 24
15 => 27
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?

2 comment threads

tangent: Niven? (2 comments)
Alternate output formats (2 comments)
Post
+2
−0

Python, 98 bytes

from itertools import*
f=lambda n:[*islice(filter(lambda k:k%sum(map(int,str(k)))==0,count(1)),n)]
  • itertools.count() generates integer numbers starting from 1,
  • filter() filters out the Niven numbers,
  • itertools.islice() limits the result to n items,
  • and [*...] collects the numbers in a list.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Without itertools (3 comments)
Without itertools
Peter Taylor‭ wrote about 2 years ago

The sum(map(int trick is shorter than I'd managed, but itertools is expensive. As a one-liner, a recursive lambda gets 76: f=lambda n,i=1,a=[]:a if a[n-1:]else f(n,i+1,a+[i][i%sum(map(int,str(i))):]). But if you don't insist on a list, the best I've found is an explicit loop for 73: def f(n,i=0): while n: i+=1 if i%sum(map(int,str(i)))<1:yield i;n-=1 where the tabs also indicate newlines

__blackjack__‭ wrote about 2 years ago

Why a comment and not an answer?

Peter Taylor‭ wrote about 2 years ago

I'm used to a culture of code golf which is collaborative.