Prolog Program: Write a Prolog program that finds all possible arrangements for
ID: 3775620 • Letter: P
Question
Prolog Program:
Write a Prolog program that finds all possible arrangements for placing these 3 symbols: in a 3 times 3 grid, so that each symbol appears only once in each row and once in each column. For example, this would be a possible valid arrangement:| Write a program that finds if a given number is prime or not. Write the program using either a structured or object oriented approach and then rewrite it using a logic programming paradigm. For the structured or object, oriented approach feel free to use Java or C++. For the logic paradigm version, you must use Prolog.Explanation / Answer
Answer for Question 3 :
// Program for finding a number is Prime or Not
import java.util.*
public class Prime
{
public static void main(String args[])
{
System.out.println("Please enter the number to check:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n<2)
System.out.println("Please Enter the value above 2");
for(int i=2;i<=n;i++) // Logic to check whether the Number is Prime or Not
{
if(n%i==0 && n!=i)
continue;
if(n%i==0 && n==i)
System.out.println("Entered Number is a Prime Number ");
}
}
}
// Prolog Logic to find a Number is Prime or Not
divisible(X,Y) : - 0 is X mod Y, ! .
divisible(X,Y) : - X > Y+1,divisble(X,Y+1)
isPrime(2) :- true,! .
isPrime(X):- X < 2 ,!,false
isPrime(X):- not(divisible(X,2)).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.