+ 10
CHALLENGE: NIVEN NUMBER (only base 10)
So, what is a Niven Number? Well, let me explain with the help of an example. Consider a number 156. If 156 is perfectly divisible by the sum of its digits and the quotient is a natural number, then it is a Niven Number. i.e. 156/(1+5+6)==>156/12==>13 Since 13 is an integer, 156 is a Niven Number. Now, coming to the challenge: Input: An integer Output: Niven Numbers present in the range of 0 to input number (including 0 and the input number) All The Best!!!
22 Respuestas
+ 19
https://code.sololearn.com/WXi6hFTJctvx/?ref=app
+ 14
@Ng, as @Zephyr said, the reason is that "a" is a string and "(a - 0)" converts "a" in an integer number, same for b, so i can make the addition, this is not the only way to make the conversion.
+ 13
Here's my C# implementation! ✌
LINQ One-Liner〰
Enumerable.Range(1, maximum)
.Where(n => n % n.ToString().Sum(c => (int)Char.GetNumericValue(c)) == 0)
I don't see the reason to start with 0 instead of 1 as it's invalid for integer division. Anyway happy coding! 😄
https://code.sololearn.com/c3nGgDt09JCf/?ref=app
+ 13
https://code.sololearn.com/ckGzQuKN7Yy0/?ref=app
+ 12
https://code.sololearn.com/crUKCv5nXK7p/#cpp
I been little confused with condition for 0 but here is :) !
+ 11
+ 10
@Vengat No, num % sum of digits
https://code.sololearn.com/c4bB9Pi2n17k/?ref=app
+ 8
So
num/sum of digits
+ 8
n = input("Enter a positive number.\n")
nn = not int(n) % sum(int(d) for d in n)
print("{} is{} a Niven Number.".format(n, "" if nn else " not"))
https://code.sololearn.com/c6g6sRrrgRHp/?ref=app
[edit:] The second code makes a list of Niven Numbers from 1 to and including the entered number. (0 is not a Niven Number)
https://code.sololearn.com/chmEWp3UYS6I/?ref=app
+ 8
@Ng Ju Ping That's an interesting one as it treats the character (from string) as a number. 😉
+ 7
Here my version.
Edit: Second one is printing Niven numbers from 1 to entered number.
https://code.sololearn.com/cdZJKmyQaQZd/?ref=app
https://code.sololearn.com/c4mAh1NwXOBJ/?ref=app
+ 6
https://code.sololearn.com/cUQzTuEyiXrD/?ref=app
+ 6
this is my one-liner code! 😄
https://code.sololearn.com/cj2pn69AaWIq/?ref=app
+ 6
hey @Maz there can i ask why your function inside the .reduce() is
function(a,b){return (a-0) + (b-0)} instead of function(a,b){return a + b}?
When I used the second one the result is wrong...
BTW thanks for your code so that I can realize my failures :)
+ 6
thank you!!! @zephyl and maz
+ 4
https://code.sololearn.com/ce2SfS7pOZTO
+ 3
my practice on python https://code.sololearn.com/cl4m05Md88vb/#py