Harshad or Niven series: Difference between revisions
Content added Content deleted
imported>Maxima enthusiast No edit summary |
|||
Line 3,093: | Line 3,093: | ||
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 |
<pre>1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 |
||
First harshad number larger than 1000 is 1002</pre> |
First harshad number larger than 1000 is 1002</pre> |
||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Function that returns a list of digits given a nonnegative integer */ |
|||
decompose(num) := block([digits, remainder], |
|||
digits: [], |
|||
while num > 0 do |
|||
(remainder: mod(num, 10), |
|||
digits: cons(remainder, digits), |
|||
num: floor(num/10)), |
|||
digits |
|||
)$ |
|||
/* Function that returns a list of the first len Harshad numbers */ |
|||
harshad_count(len):=block( |
|||
[i:1,count:0,result:[]], |
|||
while count<len do (if map(lambda([x],if mod(x,apply("+",decompose(x)))=0 then true),[i])=[true] then (result:endcons(i,result),count:count+1),i:i+1), |
|||
result)$ |
|||
/* Function that returns a list of the Harshad numbers up to len */ |
|||
first_count(len):=block( |
|||
[i:1,count:0,result:[]], |
|||
while i<=len do (if map(lambda([x],if mod(x,apply("+",decompose(x)))=0 then true),[i])=[true] then (result:endcons(i,result),count:count+1),i:i+1), |
|||
length(result))$ |
|||
/* Test cases */ |
|||
harshad_count(20); |
|||
block(first_count(1000),last(harshad_count(%%+1))); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1,2,3,4,5,6,7,8,9,10,12,18,20,21,24,27,30,36,40,42] |
|||
1002 |
|||
</pre> |
|||
=={{header|MMBasic}}== |
=={{header|MMBasic}}== |