Data Structure & Algorithm: Level-1 Problem #51. Harshad number

In mathematics, a harshad number (or Niven number) in a given number base is an integer that is divisible by the sum of its digits when written in that base. Harshad numbers in base n are also known as n-harshad (or n-Nivennumbers.

For example, The number 18 is a harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9.

Given a number, check whether it is a harshad number or not.

Sample Input:

84

Sample Output:

True


数据结构和算法:初级练习题 #51 – 哈沙德数


哈沙德数是可以在某个固定的进位制中,被各位数字之和(数字和)整除的整数。哈沙德数又称尼云数,是因为伊万·尼云在1997年一个有关数论的会议发表的论文。

若一个数无论在任何进位制中都是哈沙德数,称为全哈沙德数(全尼云数)。只有四个全哈沙德数:1246。(12在除八进制以外的进制中均为哈沙德数)

所有在零和进位制的底数之间的数都是哈沙德数。在十进制中,100以内的哈沙德数:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100。

给定一个整数n,检查它是否是哈沙德数。

输入样例:

84

输出样例:

True


Python Solution

Harshad Number