Self-describing numbers: Difference between revisions
Content added Content deleted
Recombinant (talk | contribs) m (→{{header|Zig}}: update @intCast() call) |
Not a robot (talk | contribs) (Add Cowgol) |
||
Line 899: | Line 899: | ||
3211000 |
3211000 |
||
NIL</syntaxhighlight> |
NIL</syntaxhighlight> |
||
=={{header|Cowgol}}== |
|||
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
|||
sub Length(n: uint32): (l: uint8) is |
|||
l := 0; |
|||
while n > 0 loop |
|||
n := n/10; |
|||
l := l+1; |
|||
end loop; |
|||
end sub; |
|||
sub IsSelfDescribing(n: uint32): (r: uint8) is |
|||
var positions: uint8[10]; |
|||
var digitCounts: uint8[10]; |
|||
MemSet(&positions[0], 0, @bytesof positions); |
|||
MemSet(&digitCounts[0], 0, @bytesof digitCounts); |
|||
var pos: uint8 := Length(n) - 1; |
|||
while n > 0 loop |
|||
var digit := (n % 10) as uint8; |
|||
positions[pos] := digit; |
|||
digitCounts[digit] := digitCounts[digit] + 1; |
|||
pos := pos - 1; |
|||
n := n / 10; |
|||
end loop; |
|||
r := 1; |
|||
pos := 0; |
|||
while pos < 10 loop |
|||
if positions[pos] != digitCounts[pos] then |
|||
r := 0; |
|||
break; |
|||
end if; |
|||
pos := pos + 1; |
|||
end loop; |
|||
end sub; |
|||
var n: uint32 := 1; |
|||
while n < 100000000 loop |
|||
if IsSelfDescribing(n) != 0 then |
|||
print_i32(n); |
|||
print_nl(); |
|||
end if; |
|||
n := n + 1; |
|||
end loop;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1210 |
|||
2020 |
|||
21200 |
|||
3211000 |
|||
42101000</pre> |
|||
=={{header|Crystal}}== |
=={{header|Crystal}}== |