use bigint;
my @memo = (0 => 1);
sub fact {
my ($n) = @_;
return $memo[$n] //= $n * fact
($n - 1); }
sub is_prime {
my ($n) = @_;
return (fact
($n - 1) + 1) % $n == 0; }
print sum
grep{ not is_prime
$_ } (1..10000);
dXNlIExpc3Q6OlV0aWwgcXcoc3VtKTsKdXNlIGJpZ2ludDsKCm15IEBtZW1vID0gKDAgPT4gMSk7CgpzdWIgZmFjdCB7CiAgICBteSAoJG4pID0gQF87CiAgICByZXR1cm4gJG1lbW9bJG5dIC8vPSAkbiAqIGZhY3QoJG4gLSAxKTsKfQoKc3ViIGlzX3ByaW1lIHsKICAgIG15ICgkbikgPSBAXzsKICAgIHJldHVybiAoZmFjdCgkbiAtIDEpICsgMSkgJSAkbiA9PSAwOwp9CgpwcmludCBzdW0gZ3JlcHsgbm90IGlzX3ByaW1lICRfIH0gKDEuLjEwMDAwKTs=