Jump to content

Substring primes: Difference between revisions

Undo revision 359129 by Hakank (talk) Placed the entry at the wrong place.
(Undo revision 359129 by Hakank (talk) Placed the entry at the wrong place.)
Line 752:
2 3 5 7 23 37 53 73 373
 
 
=={{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|Phix}}==
495

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.