# Exercise 4 - Logic Programming Using SymPy
## a) Checking for Prime numbers

### AIM:
To write a python program for checking prime numbers using SymPy.

### FUNCTIONS USED:
```
sympy.prime(n)
    Input : n - A number
   Output : nth prime number

sympy.isprime(n)
    Input : n - A number
   Output : Bool - whether n is a prime

sympy.primerange(start, end)
    Input : start - start of the prime range
            end - end of the prime range
   Output : Generator yeilding prime numbers in
            the range [start,end)
            
sympy.sieve.primerange(n) 
    Same as sympy.primerange
    But uses a Sieve instance to store the 
    previously computed values which is 
    more efficient and consumes more space

sympy.randprime(start, end)
    Input : start - start of the  range
            end - end of the  range
   Output : A random prime number within 
            the range [start,end)
            
sympy.prevprime(n)
    Input : n - A number
   Output : largest prime number less than n
            
sympy.nextprime(n)
    Input : n - A number
   Output : smallest prime number greater than n
``` 

### SOURCE CODE:


```python
import sympy
if __name__ == "__main__":
    print("The 5th prime is:",sympy.prime(5))
    print("The 13th prime is:",sympy.prime(13))
    print("Is 5 a prime number:",sympy.isprime(5))
    print("Is 6 a prime number:",sympy.isprime(6))
    print(
        "The prime numbers between 0 & 100 are:",
        list(sympy.primerange(0, 100)),sep="\n"
    )
    print(
        "The prime numbers between 0 & 100 are(Using Sieve):",
        list(sympy.sieve.primerange(0, 100)),sep="\n"
    )
    print("A random prime between 0 & 100:", sympy.randprime(0, 100))
    print("A random prime between 0 & 100:", sympy.randprime(0, 100))
    print("The largest prime less than 50 is:", sympy.prevprime(50))
    print("The smallest prime greater than 50 is:", sympy.nextprime(50))
```

    The 5th prime is: 11
    The 13th prime is: 41
    Is 5 a prime number: True
    Is 6 a prime number: False
    The prime numbers between 0 & 100 are:
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    The prime numbers between 0 & 100 are(Using Sieve):
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    A random prime between 0 & 100: 37
    A random prime between 0 & 100: 19
    The largest prime less than 50 is: 47
    The smallest prime greater than 50 is: 53


---

## b) Matching Mathematical Exprressions

### AIM:
To write a python program for matching mathematical exprressions using SymPy.

### FUNCTIONS USED:
```
sympy.diff(f, *variables)
    Input : f - A mathematical function
            variables - Mathematical variables
   Output : The deriviative of the function f 
            with respect to the variables

sympy.integrate(f, *variables)
    Input : f - A mathematical function
            variables - Mathematical variables
   Output : The anti-deriviative or integral of the function f 
            with respect to the variables

sympy.sin(x)
    Input : x - A number 
   Output : Mathematical sine value of the number x 

sympy.cos(x)
    Input : x - A number 
   Output : Mathematical cosine value of the number x 

sympy.exp(x)
    Input : x - A number 
   Output : Mathematical exponential  value = e^x

sympy.limit(e, x, x0, dir)
    Input : e - expression, the limit of which is to be taken
            x -  variable in the limit.
            x0 - the value toward which x tends. 
            dir - direction of approach of the limit
                  accepts the values ["+", "-", "+-"]
   Output : The mathematical limit of the expression e as 
            the variable x tends to x0
            
sympy.solve(f,*variables)
    Input : f   - polynomial
                - transcendental
                - piecewise combinations of the above
                - systems of linear and polynomial equations
                - systems containing relational expressions
            variables - Mathematical variables
   Output : The required mathematical solution 
            for the input(varies according to the input)
``` 

### SOURCE CODE:


```python
from sympy import (
    diff, integrate, sin, cos, 
    exp, limit, solve, oo
)
from sympy.abc import x
if __name__ == "__main__":
    print("The derivative of sin(x)*e^x is :", diff(sin(x)*exp(x), x))
    print(
        "The indefinite integration of e^x*sin(x)+e^x*cos(x) is :",
        integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
    )
    print(
        "The definite integration of sin(x^2) from -oo to oo is :",
        integrate(sin(x**2), (x, -oo, oo))
    )
    print(
        "The limit of sin(x)/x as x tends to 0 is :",
        limit(sin(x)/x, x, 0)
    )
    print(
        "The roots of the equation x^2-2 = 0 are :",
        solve(x**2 - 2, x)
    )
```

    The derivative of sin(x)*e^x is : exp(x)*sin(x) + exp(x)*cos(x)
    The indefinite integration of e^x*sin(x)+e^x*cos(x) is : exp(x)*sin(x)
    The definite integration of sin(x^2) from -oo to oo is : sqrt(2)*sqrt(pi)/2
    The limit of sin(x)/x as x tends to 0 is : 1
    The roots of the equation x^2-2 = 0 are : [-sqrt(2), sqrt(2)]


---

f7294b9a132799b2b67425cf313c7d1515ade87f
ipynb
Jupyter Notebook
notebooks/Exercise 4.ipynb
livinNector/artificial-intelligence-lab
90eed0b76111af1a07724e1d43b6855e5d436de3
MIT
notebooks/Exercise 4.ipynb
livinNector/artificial-intelligence-lab
90eed0b76111af1a07724e1d43b6855e5d436de3
MIT
notebooks/Exercise 4.ipynb
livinNector/artificial-intelligence-lab
90eed0b76111af1a07724e1d43b6855e5d436de3
MIT
Qwen/Qwen-72B
1. YES
2. YES