Self-describing numbers: Difference between revisions

Content deleted Content added
m added whitespace and highlighting to the task's preamble.
Blue (talk | contribs)
No edit summary
Line 1,419: Line 1,419:
<pre>: (filter selfDescribing (range 1 4000000))
<pre>: (filter selfDescribing (range 1 4000000))
-> (1210 2020 21200 3211000)</pre>
-> (1210 2020 21200 3211000)</pre>

=={{header|PowerShell}}==
According to the Wiki definition, the sum of the products of the index and the
digit contained at the index should equal the number of digits in the number:
<lang PowerShell>
function Test-SelfDescribing ([int]$Number)
{
[int[]]$digits = $Number.ToString().ToCharArray() | ForEach-Object {[Char]::GetNumericValue($_)}
[int]$sum = 0

for ($i = 0; $i -lt $digits.Count; $i++)
{
$sum += $i * $digits[$i]
}

$sum -eq $digits.Count
}
</lang>
<lang PowerShell>
Test-SelfDescribing -Number 2020
</lang>
{{Out}}
<pre>
True
</pre>
It takes a very long while to test 100,000,000 numbers, and since they are already known just test a few:
<lang PowerShell>
11,2020,21200,321100 | ForEach-Object {
[PSCustomObject]@{
Number = $_
IsSelfDescribing = Test-SelfDescribing -Number $_
}
} | Format-Table -AutoSize
</lang>
{{Out}}
<pre>
Number IsSelfDescribing
------ ----------------
11 False
2020 True
21200 True
321100 False
</pre>


=={{header|Prolog}}==
=={{header|Prolog}}==