Find n Niven Numbers
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
Scala, 52 bytes ```scala S …
2y ago
Vyxal, 4 bytes ``` ‡∑Ḋȯ ``` …
2y ago
Python, 98 bytes ```python …
2y ago
x86-64 machine code, 36 bytes …
2y ago
Ruby, 56 bytes p (1..).lazy …
2y ago
Factor, 147 bytes ``` USIN …
2y ago
Japt, 10 bytes ÈvXìx}jU …
2y ago
7 answers
Scala, 52 bytes
Stream.from(1).filter(x=>x%(x+"":\0)(_+_-48)<1).take
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.
0 comment threads
Vyxal, 4 bytes
‡∑Ḋȯ
Explained
‡∑Ḋȯ
‡ ȯ # Find the first input numbers where:
∑ # The sum
Ḋ # Divides that number
0 comment threads
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 ton
items, - and
[*...]
collects the numbers in a list.
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 ;
0 comment threads
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
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.
2 comment threads