I played with the idea in base 10. Note that sometimes, the machine wins, but sometimes it does not:
#!/usr/bin/perl use warnings; use strict; use Test::More; use Benchmark qw(cmpthese); my %dispatch = ( simple => sub { my ($n, $d) = @_; 0 == $n % $d }, 1 => sub { 1 }, 2 => sub { shift =~ /[02468]$/ }, 3 => sub { my $n = shift; while (1 < length $n) { my $sum = 0; $sum += $_ for split //, $n; $n = $sum; } return grep $_ == $n, 0, 3, 6, 9; }, 4 => sub { my ($n) = shift =~ /(..)$/; 0 == $n % 4; }, 5 => sub { shift =~ /[05]$/ }, 7 => sub { my $n = shift; while (1 < length $n) { my $last = substr $n, -1, 1, q(); $n = abs($n - 2 * $last); } return (0 == $n or 7 == $n); }, 10 => sub { shift =~ /0$/ }, 11 => sub { my $n = shift; while (2 < length $n) { my $check = 0; my $i = 1; for (split //, $n) { $check += $i * $_; $i *= -1; } $n = abs $check; } return ($n == 0 or $n =~ /^(.)\1$/); }, ); my @divisors = sort { $a <=> $b } grep 'simple' ne $_, keys %dispatch; my @test_data = map int rand 1e12, 1 .. 1e7; for my $divisor (@divisors) { cmp_ok($dispatch{simple}->($_, $divisor), '==', $dispatch{$divisor}->($_)) for @test_data[0 .. 99]; } for my $divisor (@divisors) { print "\n$divisor\n"; my ($mogi, $magi) = (0, 0); cmpthese(1e7, { mod => sub { $dispatch{simple}->($test_data[$mogi++],$divi +sor) }, machine => sub { $dispatch{$divisor}->($test_data[$magi++])}, }); } done_testing();

The benchmark on my machine:

1 Rate mod machine mod 1949318/s -- -55% machine 4329004/s 122% -- 2 Rate machine mod machine 1428571/s -- -23% mod 1851852/s 30% -- 3 Rate machine mod machine 204666/s -- -89% mod 1848429/s 803% -- 4 Rate machine mod machine 974659/s -- -48% mod 1858736/s 91% -- 5 Rate mod machine mod 1855288/s -- -9% machine 2044990/s 10% -- 7 Rate machine mod machine 224517/s -- -88% mod 1845018/s 722% -- 10 Rate mod machine mod 1851852/s -- -39% machine 3058104/s 65% -- 11 Rate machine mod machine 209820/s -- -88% mod 1801802/s 759% --
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: A mod2 Machine. by choroba
in thread A mod2 Machine. by code-ninja

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.