Search code examples
artificial-intelligenceclipsexpert-system

How to create rules of divisibility in Clips?


I'm very new to Clips Expert System, I would like to know if some of you can help to implement rules of divisibility for numbers 7, 11 and 13.

This is what I used for divisibility of 2 but I can't do the same for 7, 11 and 13

;Facts for divisibility of 2
(deffacts lastnumbers 
(firstnum 0)
(secondnum 2)
(thirdnum 4)
(fourthnum 6)
(fifth 8))

I'm trying to get the out like this:

Number 886782 is divisible by 13

Thanks in advance.


Solution

  • In general, you cannot determine if one number is evenly divisible by another just looking at the last digit of the dividend. Instead look at the remainder of the integer division and if it's zero, then the dividend was evenly divisible by the divisor.

             CLIPS (6.31 6/12/19)
    CLIPS>    
    (defrule get-dividend
       (not (dividend ?))
       =>
       (printout t "Dividend? ")
       (assert (dividend (read))))
    CLIPS> 
    (defrule get-divisor
       (dividend ?dividend&:(integerp ?dividend))
       (not (divisor ?))
       =>
       (printout t "Divisor? ")
       (assert (divisor (read))))
    CLIPS> 
    (defrule bad-response
       (or ?f <- (dividend ?d)
           ?f <- (divisor ?d))
       (test (not (integerp ?d)))
       =>
       (retract ?f))
    CLIPS>    
    (defrule is-divisible
       (dividend ?dividend&:(integerp ?dividend))
       (divisor ?divisor&:(integerp ?divisor))
       =>
       (printout t "Dividend " ?dividend " is"
                   (if (= (mod ?dividend ?divisor) 0)
                      then " "
                      else " not ")
                   "divisible by " ?divisor crlf)) 
    CLIPS> (reset)
    CLIPS> (run)
    Dividend? 17
    Divisor? 3
    Dividend 17 is not divisible by 3
    CLIPS> (reset)
    CLIPS> (run)
    Dividend? 886782
    Divisor? 13
    Dividend 886782 is divisible by 13
    CLIPS>