Numbers divisible by the sum and product of their digits
05AB1E, 13 12 bytes
Thanks to Emigna for saving a byte!
µNNSONSP‚ÖP½
Explanation:
µ ½ # Get the nth number for which the following holds:
NSO # The sum of digits of the current number
NSP # And the products of digits of the current number
N ‚ÖP # Divides the current number
# If the nth number has been reached, quit and implicitly print N
Uses the CP-1252 encoding. Try it online!
Pyke, 14 bytes (non-competitive) (1-indexed)
~1IY'sB]im%X)@
Try it here!
My god what a lot of new features.
~1 - infinite list of natural numbers
IY'sB]im%X) - filter(^, V) - remove if any truthiness
Y - digits(i)
'sB] - [sum(^), product(^)]
im% - map(^, %i)
X - splat(^)
@ - ^[input]
Of which are non-competitive
- a bugfix in
I
where it would only check if the first item on the stack was truthy digits
- return a list of digits in the number@
used to get the nth item of an infinite list
Of which were being used for the first time:
- all of the above
- infinite lists
Remove the last 2 bytes to get all of these numbers.
C#, 118 bytes
n=>{int x=0,c=0;for(;;){int s=0,p=1,i=++x;while(i>0){s+=i%10;p*=i%10;i/=10;}if((c+=p>0&&x%s+x%p<1?1:0)==n)return x;}};
Full program with ungolfed function and test cases:
using System;
public class Program
{
public static void Main()
{
// x - output number
// c - counter
// s - sum
// p - product
// i - iterator
Func<int,int>f= n=>
{
int x=0, c=0;
for ( ; ; )
{
int s=0, p=1, i=++x;
while (i > 0)
{
s += i%10;
p *= i%10;
i /= 10;
}
if ( (c += p> 0&& x%s+x%p<1 ? 1 : 0) == n)
return x;
}
};
// tests:
Console.WriteLine(f(1)); //1
Console.WriteLine(f(5)); //5
Console.WriteLine(f(10)); //12
Console.WriteLine(f(20)); //312
Console.WriteLine(f(42)); //6912
Console.WriteLine(f(50)); //11313
}
}