Answered step by step
Verified Expert Solution
Question
1 Approved Answer
primes (Limit, Ps). :integers (2, Limit, Is), sieve (Is, Ps). integers (Low, High, [Low Rest]) :- Low = < < High, !, M is
primes (Limit, Ps). :integers (2, Limit, Is), sieve (Is, Ps). integers (Low, High, [Low Rest]) :- Low = < < High, !, M is Low+1, integers (M, High, Rest). integers (Low, High, []). sieve ([], []). sieve ([I | Is], [I]Ps]) - remove (I, Is, New), remove (P, [], []). sieve (New, Ps). remove (P, [I | Is], [I|Nis]) :- not (0 is I mod P), !, remove (P, Is, Nis). remove (P, [I Is], Nis) :- 0 is I mod P, !, remove (P, Is, Nis). Figure 4.6 The Sieve of Eratosthenes in Prolog (adapted from Clocksin and Mellish [1994])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started