Self-describing numbers: Difference between revisions
Content added Content deleted
Recombinant (talk | contribs) |
Recombinant (talk | contribs) m (→Alternative With "Optimisations": update @truncate() call) |
||
Line 3,593: | Line 3,593: | ||
3211000 |
3211000 |
||
42101000</pre> |
42101000</pre> |
||
===Alternative With " |
===Alternative With "Optimizations"=== |
||
Here is an alternative implementation of <em>isSelfDescribing()</em> that |
Here is an alternative implementation of <em>isSelfDescribing()</em> that |
||
illustrates additional computationally cheap ways of partially eliminating |
illustrates additional computationally cheap ways of partially eliminating |
||
Line 3,637: | Line 3,637: | ||
// should equal the number of digits in the number |
// should equal the number of digits in the number |
||
var sum: u32 = 0; |
var sum: u32 = 0; |
||
for (digits, 0..) |n, index| sum += n * @ |
for (digits, 0..) |n, index| sum += n * @as(u32, @truncate(index)); |
||
if (sum != digits.len) return false; |
if (sum != digits.len) return false; |
||
} |
} |