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();