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

23rd Sep 2017, 1:00 PM
Kartik
Kartik - avatar
22 Antworten
23rd Sep 2017, 2:21 PM
Tim Thuma
Tim Thuma - avatar
+ 19
https://code.sololearn.com/WXi6hFTJctvx/?ref=app
23rd Sep 2017, 2:54 PM
Maz
Maz - avatar
+ 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.
24th Sep 2017, 12:39 PM
Maz
Maz - avatar
+ 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
23rd Sep 2017, 2:18 PM
Zephyr Koo
Zephyr Koo - avatar
+ 13
https://code.sololearn.com/ckGzQuKN7Yy0/?ref=app
23rd Sep 2017, 4:35 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 12
https://code.sololearn.com/crUKCv5nXK7p/#cpp I been little confused with condition for 0 but here is :) !
24th Sep 2017, 1:36 AM
Vukan
Vukan - avatar
23rd Sep 2017, 9:16 PM
David Akhihiero
David Akhihiero - avatar
+ 10
@Vengat No, num % sum of digits https://code.sololearn.com/c4bB9Pi2n17k/?ref=app
23rd Sep 2017, 1:59 PM
Ekansh
+ 8
So num/sum of digits
23rd Sep 2017, 1:19 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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
23rd Sep 2017, 7:20 PM
David Ashton
David Ashton - avatar
+ 8
@Ng Ju Ping That's an interesting one as it treats the character (from string) as a number. 😉
24th Sep 2017, 12:09 PM
Zephyr Koo
Zephyr Koo - avatar
23rd Sep 2017, 3:13 PM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 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
23rd Sep 2017, 5:27 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 6
https://code.sololearn.com/cUQzTuEyiXrD/?ref=app
23rd Sep 2017, 8:24 PM
Jonas Schröter
Jonas Schröter - avatar
+ 6
this is my one-liner code! 😄 https://code.sololearn.com/cj2pn69AaWIq/?ref=app
24th Sep 2017, 5:51 AM
m abrate
m abrate - avatar
+ 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 :)
24th Sep 2017, 11:08 AM
Ng Ju Ping
Ng Ju Ping - avatar
+ 6
thank you!!! @zephyl and maz
24th Sep 2017, 12:48 PM
Ng Ju Ping
Ng Ju Ping - avatar
+ 4
https://code.sololearn.com/ce2SfS7pOZTO
24th Sep 2017, 7:40 PM
Zeke Williams
Zeke Williams - avatar
25th Sep 2017, 1:32 AM
ysraelcon
ysraelcon - avatar