From 242c5a1540f1968aefe90fa55f5e4506da8a4c68 Mon Sep 17 00:00:00 2001 From: Ayush <63397021+frost-head@users.noreply.github.com> Date: Tue, 29 Sep 2020 19:58:30 +0530 Subject: [PATCH 1/6] Add files via upload --- Mathematics/Greatest_common_divisor.py | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Mathematics/Greatest_common_divisor.py diff --git a/Mathematics/Greatest_common_divisor.py b/Mathematics/Greatest_common_divisor.py new file mode 100644 index 0000000..bf12d2f --- /dev/null +++ b/Mathematics/Greatest_common_divisor.py @@ -0,0 +1,45 @@ +""" +Question : find the greatest common factor for given two numbers. + +by: frosthead, github: github.com/frost-head + +example: + input: + a = 2, b = 4 + output: + 2 + explaination: + 2 is the highest number that divides both 2 and 4 completely. +""" +def GCD(): + a = int(input("enter the first number: ")) + b = int(input("enter the second number: ")) + gcd = 1 + if a Date: Tue, 29 Sep 2020 20:15:32 +0530 Subject: [PATCH 2/6] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 52ea32a..e8ec5b0 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ ## What is it? #### This is a resource for the guys interested in Compitive programing or want to get a job in tech companies. Multiple solved programming questions are avilable (in python) here with explained solutions. Completely for free. +quadratic equation +## Pre Requisites +* Good Hand in Python ## How To use it Effectively? Questions are sorted by the topics Like Maths, Arrays,etc. in there seprate folder. open the folder you want to learn topics about. you'll find questions in there. From 3bd55807c2be8db152633c4e01bc0c3b377d5796 Mon Sep 17 00:00:00 2001 From: Ayush <63397021+frost-head@users.noreply.github.com> Date: Tue, 29 Sep 2020 22:19:26 +0530 Subject: [PATCH 3/6] Quadratic Roots --- Mathematics/QuadraticRoots.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Mathematics/QuadraticRoots.py diff --git a/Mathematics/QuadraticRoots.py b/Mathematics/QuadraticRoots.py new file mode 100644 index 0000000..6d20788 --- /dev/null +++ b/Mathematics/QuadraticRoots.py @@ -0,0 +1,41 @@ +""" +Question : + given the constants(a,b,c) of a Quadratic Eq find the roots of Eq. + +Example: + input: + a = 3 + b = 5 + c = -7 + output: + x = 0, y = -3 +""" + +# CODE +import math + +def quadraticRoots(): + a = int(input("Enter a :")) + b = int(input("Enter b :")) + c = int(input("Enter c :")) + d = (b**2) - (4*a*c) + if d<0: + print("Imaginary") + else: + x = int( ((-b) + (d**(1/2)) )//(2*a) ) + y = int( ((-b) - (d**(1/2)) )//(2*a) ) + print(f' x = {x}, y = {y}') + +quadraticRoots() + + +""" +Solution explaination: + In function quadraticRoots(). + We fist ask user the constants of the quation.(a,b,c) + then we check whether roots of eq are real or imaginary by b^2 - 4ac. you can look for the use of quadratic formula on internet. + if roots are imaginary we print roots are imaginary. + else we use quadratic formula((-b +- root(b^2 - 4ac))/2a) to print the values of x and y. +Time Complexity: + The given solution will have a time complexity of O(1), as we are using a formula that will take constant time. +""" From e2af91d4bb6ddf582235a6af3c898c87889f8d20 Mon Sep 17 00:00:00 2001 From: Ayush <63397021+frost-head@users.noreply.github.com> Date: Wed, 30 Sep 2020 16:51:04 +0530 Subject: [PATCH 4/6] Least Common Multiple added --- Mathematics/Least_Common_Multiple.py | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Mathematics/Least_Common_Multiple.py diff --git a/Mathematics/Least_Common_Multiple.py b/Mathematics/Least_Common_Multiple.py new file mode 100644 index 0000000..1bdd86b --- /dev/null +++ b/Mathematics/Least_Common_Multiple.py @@ -0,0 +1,37 @@ +""" +Question : + Find the least common multiple of two numbers. + +By : Ayush , github: https://github.com/frost-head + +Example: + Input: a = 6 , b = 4 + + Output: 12 +""" + +#Code +def LCM(): + a = int(input("Enter the first number :")) + b = int(input("Enter the second number :")) + + c = a*b + d = max(a,b) + for i in range(d,c+1): + if i%a == 0 and i%b == 0: + return i + +print(LCM()) + + +""" +Solution Explaination: + In function LCM. + we first take two numbers from user. + then we multiply them and store the value of bigger number in a variable. + now we traverse through the range of numbers between the bigger number and the value of there multiplication. Because LCM will be between them now we look for the number that is divisible by both "a" and "b". if yes then we return it. + +Time Complexity: + time complexity for given solution will be O(a*b - max(a,b)). +""" + From c18b85705e02046c518a0d4f4ec9345cea0965ae Mon Sep 17 00:00:00 2001 From: Ayush <63397021+frost-head@users.noreply.github.com> Date: Thu, 1 Oct 2020 06:08:17 +0530 Subject: [PATCH 5/6] is_it_prime added --- Mathematics/is_it_prime.py | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Mathematics/is_it_prime.py diff --git a/Mathematics/is_it_prime.py b/Mathematics/is_it_prime.py new file mode 100644 index 0000000..28e3207 --- /dev/null +++ b/Mathematics/is_it_prime.py @@ -0,0 +1,76 @@ +""" +Question: Given a number "n" return where its prime or not. + +By : Ayush, Github : https://github.com/frost-head + +Example: + Input: 3 + Output: Yes +""" + + +#Code - Brute force Solution +def IsItPrime_naive(): + n = int(input("Enter the number you want to test :")) + if n == 1: + return False + for i in range(2,n): + if n%i == 0: + return False + return True + +""" +Solution Explaination: + In function IsItPrime_naive(). + first we ask user for input of a number. + then we check whether its '1' if yes we straight return False as one is neither prime nor comosite + then we travse the range of '2' to n. + for 'i'(the number beign traversed) + we see whether n is divisible by i or not if yes we return False, because prime numbers do not have divisor other than one and themselves. + if there is no element in range that divides n: we return True. + +Time complexity for naive approch: + O(n) will be the time complexity for the worst case senario. +""" + +#Code - Efficent approch +def IsItPrime_efficient(): + n = int(input("Enter the the number you want to test :")) + if n == 1: + return False + i = 2 + while i*i <= n: + if n%i == 0: + return False + i += 1 + return True + +""" +Solution Explaination: + In function IsItPrime_efficient(). + as perious solution we ask for number and check whether its one + but this time we only trverse root of n.that is while the number(i) times itself that is 'i*i' is less than 'n'. because the factor appears in pairs. + +Time Complexity. + O(root of n) will be the time complexity. +""" + + + +# Driver Code +if __name__ == "__main__": + print("Choose the approch :\n 0 - Navive-approch \n 1 - Efficient-approch") + while True: + c = int(input("Enter your choice :")) + if c == 0 or c == 1: + break + else: + print("Enter a valid option") + continue + + if c == 0: + print(IsItPrime_naive()) + elif c == 1: + print(IsItPrime_efficient()) + + From 773f948b4cecdb6fa987c53987e1d3341efa0a65 Mon Sep 17 00:00:00 2001 From: Ayush <63397021+frost-head@users.noreply.github.com> Date: Thu, 1 Oct 2020 06:24:59 +0530 Subject: [PATCH 6/6] Update is_it_prime.py --- Mathematics/is_it_prime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mathematics/is_it_prime.py b/Mathematics/is_it_prime.py index 28e3207..17356e9 100644 --- a/Mathematics/is_it_prime.py +++ b/Mathematics/is_it_prime.py @@ -59,7 +59,7 @@ def IsItPrime_efficient(): # Driver Code if __name__ == "__main__": - print("Choose the approch :\n 0 - Navive-approch \n 1 - Efficient-approch") + print("Choose the approch :\n 0 - Naive-approch \n 1 - Efficient-approch") while True: c = int(input("Enter your choice :")) if c == 0 or c == 1: