Sum of the digits of n is substring of n: Difference between revisions

Add Miranda
(Add Miranda)
Line 1,382:
{{out}}
<pre>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 109, 119, 129, 139, 149, 159, 169, 179, 189, 199, 200, 300, 400, 500, 600, 700, 800, 900, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919}</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (table 5 10 taskresults)]
where taskresults = filter digit_sum_is_substring [0..999]
 
table :: num->num->[num]->[char]
table cw w ns = lay (map concat (split (map fmt ns)))
where split [] = []
split ls = take w ls : split (drop w ls)
fmt n = reverse (take cw ((reverse (shownum n)) ++ repeat ' '))
 
digit_sum_is_substring :: num->bool
digit_sum_is_substring n = (digitsum n) $infix n
 
digitsum :: num->num
digitsum 0 = 0
digitsum n = n mod 10 + digitsum (n div 10)
 
infix :: num->num->bool
infix n h = True, if n = h
= False, if h = 0
= True, if n $infix (h div 10)
= True, if n $infix (chop h)
= False, otherwise
 
chop :: num->num
chop n = 0, if n<10
= n mod mask, otherwise
where mask = last (takewhile (<n) (iterate (*10) 1))</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
109 119 129 139 149 159 169 179 189 199
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
 
=={{header|Nim}}==
2,114

edits