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

Add Cowgol
(Add C++)
(Add Cowgol)
Line 112:
{{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|Cowgol}}==
<lang cowgol>include "cowgol.coh";
 
sub digitSum(n: uint16): (s: uint16) is
s := 0;
while n != 0 loop
s := s + n % 10;
n := n / 10;
end loop;
end sub;
 
sub contains(haystack: [uint8], needle: [uint8]): (r: uint8) is
r := 0;
while [haystack] != 0 loop
var h := haystack;
var n := needle;
while [h] == [n] and [h] != 0 and [n] != 0 loop
h := @next h;
n := @next n;
end loop;
if [n] == 0 then
r := 1;
return;
end if;
haystack := @next haystack;
end loop;
end sub;
 
sub digitSumIsSubstring(n: uint16): (r: uint8) is
var s1: uint8[6];
var s2: uint8[6];
var dummy := UIToA(n as uint32, 10, &s1[0]);
dummy := UIToA(digitSum(n) as uint32, 10, &s2[0]);
r := contains(&s1[0], &s2[0]);
end sub;
 
var i: uint16 := 0;
while i < 1000 loop
if digitSumIsSubstring(i) != 0 then
print_i16(i);
print_char(' ');
end if;
i := i + 1;
end loop;
print_nl();</lang>
{{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|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
2,124

edits