
Last chance! 50% off unlimited learning
Sale ends in
This function tests whether the input is a Harshad number in the specified base. If the input divided by the sum of the digits (in the base) is an integer, then it is a Harshad number.
harshad(x, base = 10)
A single item equal to one if the input is Harshad and equal to zero if not.
The integer, bigz
or character string representing the integer to test.
The base of the input x
. Bases 2 through 36 are supported.
Carl Witthoft, carl@witthoft.com
The harshad sequence follows simple rules: Take the digits of a number in the specified base and sum them. Divide the input value by that sum. If the result is an integer, i.e. the sum divides the input, it's a Harshad number.
harshad(20)
# [1] 1
# 2 + 0 = 2, divides 20
harshad(37,base = 8)
#[1] 0
# 3 + 7 = 12 base8 , does not divide 37 base8 (in base 10, 10 does not divide 31)
Run the code above in your browser using DataLab