


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



listFromTo is a predicate which returns in List a list of all integers from M to N. */
listFromTo(M, N, []) :- M > N. listFromTo(M, M, [M]). listFromTo(M, N, [M | M_Rest]) :- M < N, M1 is M + 1, listFromTo(M1, N, M_Rest).
Output
| ?- listFromTo(2, 5, List).
List = [2,3,4,5]
yes | ?- listFromTo(-1, -10, List).
List = []
yes | ?- listFromTo(-10, -1, List).
List = [-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
yes
/* multiple is a predicate which is true if N is a multiple of M. */
multiple(N, M) :- Mod is N mod M, Mod = 0.
/* removeMultiples is a predicate which returns in List2 the list List1 with all elements which are divisible by integer N removed. */
removeMultiples([], N, []). removeMultiples([List1_Head | List1_Tail], N, List2) :- multiple(List1_Head, N), removeMultiples(List1_Tail, N, List2). removeMultiples([List1_Head | List1_Tail], N, [List1_Head | List2_Tail]) :- + multiple(List1_Head, N), removeMultiples(List1_Tail, N, List2_Tail).
Output
| ?- removeMultiples([2, 3, 4, 5], 2, List).
List = [3,5]
yes | ?- removeMultiples([5, 10, 15], 5, List).
List = []
yes | ?- removeMultiples([2, 3, 5, 7], 9, List).
List = [2,3,5,7]
yes
sieveOfEratosthenes is a predicate which returns a list of all prime numbers between 2 and N. The predicate uses the Sieve of Eratosthenes to compute the prime numbers as follows:
Begin with a list of integers from 2 to N. The first integer, 2, is the first prime. Remove every multiple of this prime. Repeatedly take the next integer in the list as the next prime and remove every multiple of the prime. */
sieveOfEratosthenes(N, []) :- N < 2. sieveOfEratosthenes(N, Primes) :- listFromTo(2, N, List_2_N), removePrimeMultiples(List_2_N, Primes).
/* removePrimeMultiples removes every multiple of each successive prime number in the list L. */
removePrimeMultiples([], []). removePrimeMultiples([Head | Tail], [Head | Primes_Tail]) :- removeMultiples(Tail, Head, List), removePrimeMultiples(List, Primes_Tail).
Output
| ?- sieveOfEratosthenes(1, Primes).
Primes = []
yes
(b) {sum = ( odd+1 2 )
2 & odd ≥ 1 & odd mod 2 = 1} while (odd <> 2 ∗ n − 1) loop odd := odd + 2; sum := sum + odd; end loop; {sum = n^2 } To apply the Loop rule, we need the postcondition to be in the form I & ¬(odd 6 = 2 × n − 1) ≡ sum = n^2. Now we may apply the Loop rule
{I & odd 6 = 2 ∗ n − 1 }odd := odd + 2; sum := sum + odd{I} {I}S 3 {I & odd = 2 × n − 1 }
followed by the Composition rule.
{I & odd 6 = 2 × n − 1 }odd := odd + 2{P 3 }, {P 3 }sum := sum + odd{I} {I & odd 6 = 2 × n − 1 }odd := odd + 2; sum := sum + odd{I}
To determine P 3 , we apply the Assignment axiom.
{sum + odd = (
odd + 1 2
2 & odd ≥ 1 & odd mod 2 = 1} sum := sum + odd (8)
{sum = (
odd + 1 2
2 & odd ≥ 1 & odd mod 2 = 1}
Therefore, P 3 ≡ sum + odd = ( odd+1 2 )
2 & odd ≥ 1 & odd mod 2 = 1. We apply the Assignment axiom again to prove
{sum + odd + 2 = (
odd + 3 2
2 & odd + 2 ≥ 1 & odd + 2 mod 2 = 1} odd := odd + 2 (9)
{sum + odd = (
odd + 1 2
2 & odd ≥ 1 & odd mod 2 = 1}
Note that sum + odd + 2 = ( odd+3 2 ) 2 & odd + 2 ≥ 1 & odd + 2 mod 2 = 1 ≡ sum = ( odd+1 2 ) 2 & odd ≥ 1 & odd mod 2 = 1 ≡ I. Applying the Consequence-2 rule completes the proof.
I & odd 6 = 2 × n − 1 ⇒ I, {I}odd := odd + 2{sum + odd = ( odd+1 2 ) 2 & odd ≥ 1 & odd mod 2 = 1} {I & odd 6 = 2 × n − 1 }odd := odd + 2{sum + odd = ( odd+1 2 ) 2 & odd ≥ 1 & odd mod 2 = 1}
Thus the partial correctness proof of the given program consists of steps 1-10.