### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

+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?
Why should this post be closed?

+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?

+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?

+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?

+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?

+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?

+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.
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.)
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?

+0
−0

# Japt, 10 bytes

ÈvXìx}jU1ì


Try it

Why does this post require moderator attention? 