Rosetta Code
Harshad or Niven series
Find Harshad (Niven) numbers — integers divisible by the sum of their own digits.
Source
rosettacode/popular/harshad_numbers.vibe
# title: Harshad or Niven series
# source: https://rosettacode.org/wiki/Harshad_or_Niven_series
# category: Rosetta Code
# difficulty: Easy
# summary: Find Harshad (Niven) numbers — integers divisible by the sum of their own digits.
# tags: popular, math, sequences
# vibe: 0.2
def digit_sum(n)
total = 0
remaining = n
while remaining > 0
total = total + (remaining % 10)
remaining = remaining / 10
end
total
end
def harshad?(n)
n > 0 && n % digit_sum(n) == 0
end
def first_harshad(count)
results = []
n = 1
while results.size < count
if harshad?(n)
results = results.push(n)
end
n = n + 1
end
results
end
def first_harshad_above(threshold)
n = threshold + 1
while true
if harshad?(n)
return n
end
n = n + 1
end
end
def run
{
first_20: first_harshad(20),
first_above_1000: first_harshad_above(1000)
}
end
Output
Press run to execute run from this example.