Created
October 6, 2014 00:22
-
-
Save Radcliffe/2b5e2087bda070e1426b to your computer and use it in GitHub Desktop.
A144261: a(n) = smallest k such that k*n is a Niven (or Harshad) number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Harshad number (or Niven number) is a number that is divisible by its sum of digits. | |
def sum_of_digits(n): | |
s = 0 | |
while n > 0: | |
s += (n % 10) | |
n /= 10 | |
return s | |
# Alternative one-liner: | |
# sum_of_digits = lambda n: sum(map(int, str(n))) | |
# Note: will raise an error if n == 0. | |
def harshad(n): | |
return n % sum_of_digits(n) == 0 | |
first100terms = [1,1,1,1,1,1,1,1,1,1,10,1,9,3,2,3,6,1,6,1,1,5,9,1,2,6, | |
1,3,9,1,12,6,4,3,2,1,3,3,3,1,10,1,12,3,1,5,9,1,8,1,2, | |
3,18,1,2,2,2,9,9,1,12,6,1,3,3,2,3,3,3,1,18,1,7,3,2,2, | |
4,2,9,1,1,5,18,1,6,6,3,3,9,1,4,5,4,9,2,2,12,4,2,1] | |
print "# A144261" | |
for n in xrange(1, 10001): | |
k = 1 | |
while not harshad(k * n): | |
k += 1 | |
print n, k | |
if n <= 100: | |
assert k == first100terms[n-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment