Greetings,
I was thinking of a problem to solve in order to practise prolog.
Problem: There is a given number N, I would like to generate list of primes that are (<=N).
So i wrote a few predicats in order to solve this but i dont know how to put them together, everything i tried failed.
Here are my predicates:
Primality test:
prime(N):-T is N//2, not_dividable_by_all(N,T).
not_dividable_by_all(N,T):-T>1, T1 is T-1, N mod T=\=0,not_dividable_by_all(N,T1).
not_dividable_by_all(N,1).
Adding to a list:
There are probably 3 ways, add on the begining add on the end or use concatenation i guess, got all 3 predicates but i will put concatenation here only for now if you think other predicates would be better i will change.
conc([],List,List).
conc([Head|Tail],List2,[Head|Result]):-conc(Tail,List2,Result).
Ok my idea is to recure through N, n1,n2,n3,...,N check if n1 is prime add to a list countinue for n2,n3,...,N
Regards,Vlad
I was thinking of a problem to solve in order to practise prolog.
Problem: There is a given number N, I would like to generate list of primes that are (<=N).
So i wrote a few predicats in order to solve this but i dont know how to put them together, everything i tried failed.
Here are my predicates:
Primality test:
prime(N):-T is N//2, not_dividable_by_all(N,T).
not_dividable_by_all(N,T):-T>1, T1 is T-1, N mod T=\=0,not_dividable_by_all(N,T1).
not_dividable_by_all(N,1).
Adding to a list:
There are probably 3 ways, add on the begining add on the end or use concatenation i guess, got all 3 predicates but i will put concatenation here only for now if you think other predicates would be better i will change.
conc([],List,List).
conc([Head|Tail],List2,[Head|Result]):-conc(Tail,List2,Result).
Ok my idea is to recure through N, n1,n2,n3,...,N check if n1 is prime add to a list countinue for n2,n3,...,N
Regards,Vlad