P40 - 哥德巴赫猜想。
作者:Philip Potter
规范
P40 (**) Goldbach's conjecture.
Goldbach's conjecture says that every positive even number greater
than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is
one of the most famous facts in number theory that has not been proved
to be correct in the general case. It has been numerically confirmed
up to very large numbers (much larger than we can go with our Prolog
system). Write a predicate to find the two prime numbers that sum up
to a given even integer.示例
> say ~goldbach 28 5 23
源代码: P40-rhebus.pl
use v6;
subset Even of Int where * %% 2;
sub goldbach (Even $n where * > 2 ) {
for 2..$n/2 -> $k {
if $k.is-prime && ($n-$k).is-prime {
return ($k, $n-$k);
}
}
return; # fail
}
say ~ goldbach $_ for 28, 36, 52, 110, 62710560;
Perl 6 示例