zxcisnoias
zxcisnoias

Reputation: 732

Prolog, given N and find all numbers not divisible by 3 and 5 and these numbers must be smaller than N

I have problem with find a solution to the problem.
Divisible/2 predicate examines whether a number N is divisible by one of numbers in the list

divisible([H|_],N) :- N mod H =:= 0.
divisible([H|T],N) :- N mod H =\= 0, divisible(T,N).

I need to build a predicate find that will find Number < N that are not divisible by the list of numbers
example input/output:

?- find(5, [3,5],Num).
output is : 
Num = 4; Num = 2; Num = 1. False
Here N is 5 and list of number is [3,5]

Current Code:

findNum(1, LN, Num) :- \+ divisible(LN,1),
                        Num is 1.

findNum(Rank, LN, Num) :- Rank > 1,
                        Num1 is Rank - 1,
                        ( \+ divisible(LN,Num1) -> Num is Num1;
                        findNum(Num1,LN, Num) ).

It only prints Num = 4; It never prints 2 and 1 for some reasons
And I am not sure where goes wrong.. Any help is appreciated...

Upvotes: 1

Views: 631

Answers (2)

Richard Ardelean
Richard Ardelean

Reputation: 285

Try to modify the findNum predicate into:

findNum(Rank, LN, Num) :- Rank > 1,
                          Num1 is Rank - 1,
                          \+ divisible(LN,Num1) -> Num is Num1.
findNum(Rank, LN, Num) :-  Rank > 1, 
                           Num1 is Rank - 1, 
                           findNum(Num1,LN, Num).

For me, it gives the requested answer.

Upvotes: 1

Guy Coder
Guy Coder

Reputation: 24986

Done three different ways


  1. Using recursion
find_rec(0,_,[]) :- !.
find_rec(N0,Possible_divisors,[N0|Successful_divisors]) :-
    divisible(Possible_divisors,N0),
    N is N0 - 1,
    find_rec(N,Possible_divisors,Successful_divisors).
find_rec(N0,Possible_divisors,Successful_divisors) :-
    \+ divisible(Possible_divisors,N0),
    N is N0 - 1,
    find_rec(N,Possible_divisors,Successful_divisors).

Example run

?- find_rec(5,[3,5],Num).
Num = [5, 3] ;
false.

  1. Using partition
find_par(N,Possible_divisors,Successful_divisors) :-
    findall(Ns,between(1,N,Ns),List),
    partition(partition_predicate(Possible_divisors),List,Successful_divisors,_).

partition_predicate(L,N) :-
    divisible(L,N).

Example run

?- find_par(5,[3,5],Num).
Num = [3, 5].

  1. Using conditional ( -> ; )
find_con(0,_,[]) :- !.
find_con(N0,Possible_divisors,Result) :-
    (
        divisible(Possible_divisors,N0)
    ->
        Result = [N0|Successful_divisors]
    ;
        Result = Successful_divisors
    ),
    N is N0 - 1,
    find_con(N,Possible_divisors,Successful_divisors).

Example run

?- find_con(5,[3,5],Num).
Num = [5, 3].

It would be nice to see some test cases for divisible/2 to quickly understand how it works.

:- begin_tests(divisible).

divisible_test_case_generator([13,1],13).
divisible_test_case_generator([20,10,5,4,2,1],20).
divisible_test_case_generator([72,36,24,18,12,9,8,6,4,3,2,1],72).
divisible_test_case_generator([97,1],97).
divisible_test_case_generator([99,33,11,9,3,1],99).

test(1,[nondet,forall(divisible_test_case_generator(List,N))]) :-
    divisible(List,N).

:- end_tests(divisible).

Running of tests

?- make.
% c:/users/groot/documents/projects/prolog/so_question_177 compiled 0.00 sec, 0 clauses
% PL-Unit: divisible ..... done
% All 5 tests passed
true.

Some feedback about your code.

  1. Typically the formatting of a predicate starts a new line after :-
  2. When using a ; operator, it is better to put it on a line by itself so that it is very obvious, many programmers have spent hours looking for bugs because a ; was seen as a , and not understood correctly.
findNum(1, LN, Num) :-
    \+ divisible(LN,1),
    Num is 1.

findNum(Rank, LN, Num) :-
    Rank > 1,
    Num1 is Rank - 1,
    (
        \+ divisible(LN,Num1)
    ->
        Num is Num1
    ;
        findNum(Num1,LN, Num)
    ).

Where the bug is in your code is here for the <true case>

    ->
        Num is Num1

you did not recurse for the next value like you did for the <false case>

    ;
        findNum(Num1,LN, Num)

Upvotes: 2

Related Questions