Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Maths Section #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Maths/catalan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def catalan(n):
if (n == 0 or n == 1):
return 1
catalan = [0]*(n+1)
catalan[0] = 1
catalan[1] = 1
for i in range(2, n + 1):
catalan[i] = 0
for j in range(i):
catalan[i] = catalan[i] + catalan[j] * catalan[i-j-1]
return catalan[n]

print("Enter value of n:")
a=int(input())
print(f"Catalan Number(nth) is : {catalan(a)}")
18 changes: 18 additions & 0 deletions Maths/check_prime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def check(n):
if(n==1):
return False
if(n==2 or n==3):
return True
i=2
while i*i<=n:
if(n%i==0):
return False
i+=1
return True

print("Enter number :")
a=int(input())
if(check(a)):
print(f"{a} is a prime number")
else:
print(f"{a} is not a prime number")
9 changes: 9 additions & 0 deletions Maths/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def computeGCD(x, y):
while(y):
x, y = y, x % y

return x

print("Enter two numbers:")
a,b=map(int,input().split())
print(f"Gcd of {a} and {b} is {computeGCD(a,b)}")
21 changes: 21 additions & 0 deletions Maths/prime_factors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def prime_factors(n):
i=2
ans=[]
while i*i<=n:
if(n%i==0):
ans.append(i)
while(n%i==0):
n=n//i
i+=1
if(n!=1):
ans.append(n)
if(len(ans)==0):
print("No prime factors")
else:
print("Prime factors are : ",end=" ")
print(*ans)


print("Enter number:")
a=int(input())
prime_factors(a)
18 changes: 18 additions & 0 deletions Maths/sieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def sieve(n):
prime=[1]*(n+1)
prime[0],prime[1]=0,0
i=2
while i*i<=n:
if(prime[i]):
for j in range(i*i,n+1,i):
prime[j]=0
i+=1
print("Primes are : ")
for i in range(0,n+1):
if(prime[i]):
print(i,end=" ")
print()

print("Enter upper bound of prime:")
a=int(input())
sieve(a)
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Pune, Maharashtra, India.<br />
* [Finding the length of linked list](Linked%20Lists/P01_FindingLengthOfLinkedList.py)
* [Reversing the linked list](Linked%20Lists/P02_ReversingLinkedList.py)

# Maths

* [Maths Concept](Maths)
* [Catalan Number](Maths/catalan.py)
* [Check Prime](Maths/check_prime.py)
* [GCD](Maths/gcd.py)
* [Prime Factors](Maths/prime_factors.py)
* [Sieve of eratosthenes](Maths/sieve.py)


# Stack

* [Stack Concept](https://github.com/OmkarPathak/Data-Structures-using-Python/tree/master/Stack/Stack.ipynb)
Expand Down