Self-describing numbers: Difference between revisions

Content added Content deleted
m (→‎Alternative With "Optimisations": update @truncate() call)
Line 3,593: Line 3,593:
3211000
3211000
42101000</pre>
42101000</pre>
===Alternative With "Optimisations"===
===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 * @truncate(u32, index);
for (digits, 0..) |n, index| sum += n * @as(u32, @truncate(index));
if (sum != digits.len) return false;
if (sum != digits.len) return false;
}
}