FWIW my
solution just limits the range of witnesses:
for my $witness (2..int((10**length($i) - 1) / $i)) {
if ($i_sorted eq join '', sort split //, $i * $witness) {
if (++$pairs == $count) {
++$qualifying_integers;
next I;
}
}
}
# maximum witness can't be more than i's first digit
for my $witness (2..substr $i, 0, 1) {
if (!( $i % $witness ) && $i_sorted eq join '', sort split
+ //, $i / $witness) {
if (++$pairs == $count) {
++$qualifying_integers;
next I;
}
}
}
and tries to limit the ops executed in the loop body. To take advantage of other witness elimination, you'd unwrap the outer loop and have special code for e.g multiples of 3, but that if a lot of duplication. You'd get a much better speed bump using Inline for at least the "does it have the same digits" check. My
go solution is much much faster. But all the conversions from numbers to strings to runes make it a lot longer.
Python was interesting; it lacks the ability to continue an outer loop, so the two inner loop approach needed an additional flag variable. But Python's iterator design made a single inner loop possible, at a slight speed penalty but producing the tersest code.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.