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
Notifications
Mark all as read
Challenges

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
Why does this post require moderator attention?
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)

7 answers

+4
−0

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)

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 taken from the infinite list.

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

0 comment threads

+3
−0

Vyxal, 4 bytes

‡∑Ḋȯ

Try it Online!

Explained

‡∑Ḋȯ
‡  ȯ # Find the first input numbers where:
 ∑   #  The sum
  Ḋ  #  Divides that number  
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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.
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Without itertools (3 comments)
+1
−0

Ruby, 56 bytes

p *(1..).lazy.filter{_1%_1.digits.sum<1}.take(gets.to_i)

Attempt This Online!

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

0 comment threads

+1
−0

Factor, 147 bytes

USING: kernel math math.text.utils sequences ;
IN: n
: n ( n -- s ) 0 swap [
[ 1 + dup dup 1 digit-groups sum mod 0 > ] loop dup
] replicate nip ;
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

x86-64 machine code, 36 bytes

31 C0 8D 48 0A FF C0 50 99 89 17 48 F7 F1 01 17 99 85 C0 75 F6 58 50 F7 37 58 85 D2 75 E7 AB FF CE 75 E2 C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address of an array of n 32-bit integers to be filled in with the Nuven numbers, and takes n in ESI.

In assembly:

f:	xor eax, eax        # Set EAX to 0. EAX will hold the currently tried number.
	lea ecx, [rax+10]   # Set ECX to 10.
l:	inc eax             # Increase EAX by 1.
	push rax            # Push RAX (64-bit register containing EAX) onto the stack.
	cdq                 # Sign-extend, making EDX 0 provided EAX hasn't overflowed.
	mov [rdi], edx      # Place 0 at address RDI.
il:	div rcx             # Divide EDX:EAX (equal to EAX, as EDX is 0) by 10.
                            #  The quotient goes in EAX and the remainder goes in EDX.
	add [rdi], edx      # Add the remainder to the value at address RDI.
	cdq                 # Sign-extend, making EDX 0 provided EAX hasn't overflowed.
	test eax, eax       # Check the value in EAX.
	jnz il              # Jump back, to repeat the division loop, if it's nonzero.
	pop rax             # Restore the value of RAX from the stack.
	push rax            # Push it onto the stack again.
	div DWORD PTR [rdi] # Divide EDX:EAX (equal to EAX, as EDX is 0) by
                            #  the value at address RDI, which is the digit sum.
                            #  The quotient goes in EAX and the remainder goes in EDX.
	pop rax             # Restore the value of RAX from the stack.
	test edx, edx       # Check the value of the remainder in EDX.
	jnz l               # Jump back, repeating the outer loop, if it's nonzero.
                            #  (Otherwise, the value in EAX is a Niven number.)
	stosd               # Put EAX's value at address RDI, advancing the pointer.
	dec rsi             # Subtract 1, counting down from the input number in ESI.
	jnz l               # Jump back, repeating the outer loop, if it's nonzero.
	ret                 # Return.
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Japt, 10 bytes

ÈvXìx}jU1ì

Try it

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

0 comment threads

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!

Like what we're doing? Support us! Donate