Substring primes: Difference between revisions
Content added Content deleted
Line 791: | Line 791: | ||
6 innocent bystanders falsly accused of being prime (15 tests in total): {237,27,3737,537,57,737} |
6 innocent bystanders falsly accused of being prime (15 tests in total): {237,27,3737,537,57,737} |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
===Checking via substrings=== |
|||
<lang Picat>% get all the substrings of a number |
|||
subs(N) = findall(S, (append(_Pre,S,_Post,N.to_string), S.len > 0) ). |
|||
go => |
|||
Ps = [], |
|||
foreach(Prime in primes(500)) |
|||
(foreach(N in subs(Prime) prime(N) end -> Ps := Ps ++ [Prime] ; true) |
|||
end, |
|||
println(Ps).</lang> |
|||
{{out}} |
|||
<pre>[2,3,5,7,23,37,53,73,373]</pre> |
|||
===Checking via predicate=== |
|||
{{trans|AWK}} |
|||
Here we must use cut (<code>!</code>) to ensure that the test does not continue after a satisfied test. This is a "red cut" (i.e. removing it would change the logic of the program) and these should be avoided if possible. |
|||
<lang Picat>t(N,false) :- |
|||
not prime(N),!. |
|||
t(N,true) :- |
|||
N < 10,!. |
|||
t(N,false) :- |
|||
not prime(N mod 100), !. |
|||
t(N,false) :- |
|||
not prime(N mod 10),!. |
|||
t(N,false) :- |
|||
not prime(N // 10),!. |
|||
t(N,true) :- |
|||
N < 100,!. |
|||
t(N,false) :- |
|||
not prime(N // 100),!. |
|||
t(N,false) :- |
|||
not prime((N mod 100) // 10),!. |
|||
t(_N,true). |
|||
go2 => |
|||
println(findall(N,(member(N,1..500),t(N,Status),Status==true))).</lang> |
|||
{{out}} |
|||
<pre>[2,3,5,7,23,37,53,73,373]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |