logo
down
shadow

Find all natural number solutions in Prolog program


Find all natural number solutions in Prolog program

By : geek
Date : October 18 2020, 03:08 PM
it should still fix some issue You can use the Constraint Logic Programming library over Finite Domains (clpfd) for that:
code :
:- use_module(library(clpfd)).

sumsTo10(X,Y):-
    [X,Y] ins 1..10,
    X + Y #= 10,
    label([X,Y]).
?- sumsTo10(X,Y).
X = 1,
Y = 9 ;
X = 2,
Y = 8 ;
X = 3,
Y = 7 ;
X = 4,
Y = 6 ;
X = Y, Y = 5 ;
X = 6,
Y = 4 ;
X = 7,
Y = 3 ;
X = 8,
Y = 2 ;
X = 9,
Y = 1.


Share : facebook icon twitter icon
Prolog: Simple program outputting solutions

Prolog: Simple program outputting solutions


By : user2598665
Date : March 29 2020, 07:55 AM
it should still fix some issue The false/no answer results from the call M is Inter, which tries to establish equality between the totient Inter and the input number M. Since φ(n) is never equal to n except when n = 1, this almost always fails.
You may have intended to assign the value of Inter to M, but this impossible because M is always bound.
Prolog Program to Find Square of Natural Numbers

Prolog Program to Find Square of Natural Numbers


By : Thearnavut
Date : March 29 2020, 07:55 AM
I wish this helpful for you My code below is meant to generate the square of natural numbers in order , your nat/1 really seems to return a different sequence. Should be
code :
nat(0).
nat(X) :- nat(Y), X is Y+1.
sq(X) :- % call nat/1, square it...
How to tell prolog that N is a natural number if N - 1 is a natural number?

How to tell prolog that N is a natural number if N - 1 is a natural number?


By : rperez
Date : March 29 2020, 07:55 AM
hope this fix your issue I am very new to Prolog and I am trying to do a simple recursive example. My KB is: , You should do it like that:
code :
natural(0).
natural(N) :- M is N - 1, natural(M).
Find all natural divisors of a number (with Prolog)

Find all natural divisors of a number (with Prolog)


By : user2342759
Date : March 29 2020, 07:55 AM
this will help While the answer by William is nice and probably faster here is answer closer to what you were writing.
code :
divides(N,D) :-
    0 is N mod D.

divisors_0(N,D,R0,[D|R0]) :-
    divides(N,D).

divisors_0(N,D,R0,R0) :-
    \+ divides(N,D).

divisors(_,0,R,R).

divisors(N,D0,R0,R) :-
    divisors_0(N,D0,R0,R1),
    D is D0 - 1,
    divisors(N,D,R1,R).

divisors(X,R) :-
    X > 1,
    divisors(X,X,[],R), !.
?- between(1,15,N), divisors(N,Rs).
N = 2,
Rs = [1, 2] ;
N = 3,
Rs = [1, 3] ;
N = 4,
Rs = [1, 2, 4] ;
N = 5,
Rs = [1, 5] ;
N = 6,
Rs = [1, 2, 3, 6] ;
N = 7,
Rs = [1, 7] ;
N = 8,
Rs = [1, 2, 4, 8] ;
N = 9,
Rs = [1, 3, 9] ;
N = 10,
Rs = [1, 2, 5, 10] ;
N = 11,
Rs = [1, 11] ;
N = 12,
Rs = [1, 2, 3, 4, 6, 12] ;
N = 13,
Rs = [1, 13] ;
N = 14,
Rs = [1, 2, 7, 14] ;
N = 15,
Rs = [1, 3, 5, 15].
divisors(X,R) :-
  (
    var(X)
  ->
    false
  ;
    (
      var(R)
    ->
      X > 1,
      divisors(X,1,[],R)
    ;
      divisors_2(X,R), !
    )
  ).

divisors_2(_,[]).

divisors_2(X,[H|T]) :-
  divides(X,H),
  divisors_2(X,T).

divisors(X,D,R,R):-
  D>X.

divisors(N,D0,R0,R) :-
  divisors_0(N,D0,R0,R1),
  D is D0 + 1,
  divisors(N,D,R1,R).

divisors_0(N,D,R0,[D|R0]) :-
  divides(N,D).

divisors_0(_,_,R0,R0).

divides(N,D) :-
  0 is N mod D.
(
  var(R)
->
  X > 1,
  divisors(X,1,[],R)
;
  divisors_2(X,R), !
)
divisors_2(_,[]).

divisors_2(X,[H|T]) :-
  divides(X,H),
  divisors_2(X,T).
divisors(X,R) :-
  (
    var(X)
  ->
    false
  ;
    (
      var(R)
    ->
      X > 1,
      divisors(X,1,[],R)
    ;
      divisors_2(X,R), !
    )
  ).
How to remove solutions from a Prolog program

How to remove solutions from a Prolog program


By : user2899251
Date : March 29 2020, 07:55 AM
wish of those help Given this program: , You can encode a list of values for A and B you do not want, like:
code :
not_ab(1,2).
not_ab(3,1).
ab(A, B) :-
    a(A),
    b(B),
    \+ not_ab(A, B).
shadow
Privacy Policy - Terms - Contact Us © soohba.com