diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 679748d..6bffbda 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,8 +1,8 @@ -# CONTRIBUTING -To contribute, create a new folder for each problem (if it doesn't exist) and work on your code there. Ex: Problem_1's solution should be in the same directory as the question. -PR's will not be accepted otherwise. Make sure your GitHub username is at the top of your code in a comment. -Keep the code clean, simple and well commented so that anyone in the future can understand easily. Any of these languages are -fine: -> C (`GNU GCC 6+`), C++ (`GNU GCC 6+`), Java (`Java SE 8+`), Python (`Python 3.6.5+`) - -If you want to use another language, create a new issue and a maintainer will respond. +# CONTRIBUTING +To contribute, create a new folder for each problem (if it doesn't exist) and work on your code there. Ex: Problem_1's solution should be in the same directory as the question. +PR's will not be accepted otherwise. Make sure your GitHub username is at the top of your code in a comment. +Keep the code clean, simple and well commented so that anyone in the future can understand easily. Any of these languages are +fine: +> C (`GNU GCC 6+`), C++ (`GNU GCC 6+`), Java (`Java SE 8+`), Python (`Python 3.6.5+`) + +If you want to use another language, create a new issue and a maintainer will respond. diff --git a/Problems/Problem_1/Problem_1.txt b/Problems/Problem_1/Problem_1.txt index 90e633a..b65252e 100644 --- a/Problems/Problem_1/Problem_1.txt +++ b/Problems/Problem_1/Problem_1.txt @@ -1,6 +1,6 @@ -Let start off with a simple one... -Have you heard of the convex hull problem?, it involves a very simple problem statement with a deceptively simple solution, however depending on your approach and ingenuity your solution could take 15 seconds or 15 minutes, so code effectively (HINT: Brute force is not the answer). - -Problem : You are given a set of points on a 2-d plane, you have to come up with the points that form the boundary of the entire set of points, i.e find the points that make up the boundary of all the points given to you. - -Also pls dont look up the answers online, that kinda violates the purpose of the whole problem. +Let start off with a simple one... +Have you heard of the convex hull problem?, it involves a very simple problem statement with a deceptively simple solution, however depending on your approach and ingenuity your solution could take 15 seconds or 15 minutes, so code effectively (HINT: Brute force is not the answer). + +Problem : You are given a set of points on a 2-d plane, you have to come up with the points that form the boundary of the entire set of points, i.e find the points that make up the boundary of all the points given to you. + +Also pls dont look up the answers online, that kinda violates the purpose of the whole problem. diff --git a/Problems/Problem_1/Test_cases.txt b/Problems/Problem_1/Test_cases.txt index d73d9a0..e55393c 100644 --- a/Problems/Problem_1/Test_cases.txt +++ b/Problems/Problem_1/Test_cases.txt @@ -1,40 +1,40 @@ -0.3215348546593775 0.03629583077160248 -0.02402358131857918 -0.2356728797179394 -0.04590851212470659 -0.4156409924995536 -0.3218384001607433 0.1379850698988746 -0.11506479756447 -0.1059521474930943 -0.2622539999543261 -0.29702873322836 --0.161920957418085 -0.4055339716426413 -0.1905378631228002 0.3698601009043493 -0.2387090918968516 -0.01629827079949742 -0.07495888748668034 -0.1659825110491202 -0.3319341836794598 -0.1821814101954749 -0.07703635755650362 -0.2499430638271785 -0.2069242999022122 -0.2232970760420869 -0.04604079532068295 -0.1923573186549892 -0.05054295812784038 0.4754929463150845 --0.3900589168910486 0.2797829520700341 -0.3120693385713448 -0.0506329867529059 -0.01138812723698857 0.4002504701728471 -0.009645149586391732 0.1060251100976254 --0.03597933197019559 0.2953639456959105 -0.1818290866742182 0.001454397571696298 -0.444056063372694 0.2502497166863175 --0.05301752458607545 -0.06553921621808712 -0.4823896228171788 -0.4776170002088109 --0.3089226845734964 -0.06356112199235814 --0.271780741188471 0.1810810595574612 -0.4293626522918815 0.2980897964891882 --0.004796652127799228 0.382663812844701 -0.430695573269106 -0.2995073500084759 -0.1799668387323309 -0.2973467472915973 -0.4932166845474547 0.4928094162538735 --0.3521487911717489 0.4352656197131292 --0.4907368011686362 0.1865826865533206 --0.1047924716070224 -0.247073392148198 -0.4374961861758457 -0.001606279519951237 -0.003256207800708899 -0.2729194320486108 -0.04310378203457577 0.4452604050238248 -0.4916198379282093 -0.345391701297268 -0.001675087028811806 0.1531837672490476 --0.4404289572876217 -0.2894855991839297 +0.3215348546593775 0.03629583077160248 +0.02402358131857918 -0.2356728797179394 +0.04590851212470659 -0.4156409924995536 +0.3218384001607433 0.1379850698988746 +0.11506479756447 -0.1059521474930943 +0.2622539999543261 -0.29702873322836 +-0.161920957418085 -0.4055339716426413 +0.1905378631228002 0.3698601009043493 +0.2387090918968516 -0.01629827079949742 +0.07495888748668034 -0.1659825110491202 +0.3319341836794598 -0.1821814101954749 +0.07703635755650362 -0.2499430638271785 +0.2069242999022122 -0.2232970760420869 +0.04604079532068295 -0.1923573186549892 +0.05054295812784038 0.4754929463150845 +-0.3900589168910486 0.2797829520700341 +0.3120693385713448 -0.0506329867529059 +0.01138812723698857 0.4002504701728471 +0.009645149586391732 0.1060251100976254 +-0.03597933197019559 0.2953639456959105 +0.1818290866742182 0.001454397571696298 +0.444056063372694 0.2502497166863175 +-0.05301752458607545 -0.06553921621808712 +0.4823896228171788 -0.4776170002088109 +-0.3089226845734964 -0.06356112199235814 +-0.271780741188471 0.1810810595574612 +0.4293626522918815 0.2980897964891882 +-0.004796652127799228 0.382663812844701 +0.430695573269106 -0.2995073500084759 +0.1799668387323309 -0.2973467472915973 +0.4932166845474547 0.4928094162538735 +-0.3521487911717489 0.4352656197131292 +-0.4907368011686362 0.1865826865533206 +-0.1047924716070224 -0.247073392148198 +0.4374961861758457 -0.001606279519951237 +0.003256207800708899 -0.2729194320486108 +0.04310378203457577 0.4452604050238248 +0.4916198379282093 -0.345391701297268 +0.001675087028811806 0.1531837672490476 +-0.4404289572876217 -0.2894855991839297 diff --git a/Problems/Problem_2/Prob_2.txt b/Problems/Problem_2/Prob_2.txt index 0ca8cad..305e36c 100644 --- a/Problems/Problem_2/Prob_2.txt +++ b/Problems/Problem_2/Prob_2.txt @@ -1,3 +1,3 @@ -The Fibonacci series is an interesting series of numbers which are dependant on the past two values of the series, based on the recursive relation f(n) = f(n-1) + f(n-2). - -As asking a question based on the fibonacci sequence would be boring and cliche, your task is to write a recursive code solving the recursive relation f(n) = f(n-1)^(n-1) + f(n-2)^(n-2). +The Fibonacci series is an interesting series of numbers which are dependant on the past two values of the series, based on the recursive relation f(n) = f(n-1) + f(n-2). + +As asking a question based on the fibonacci sequence would be boring and cliche, your task is to write a recursive code solving the recursive relation f(n) = f(n-1)^(n-1) + f(n-2)^(n-2). diff --git a/Problems/Problem_2/a.out b/Problems/Problem_2/a.out new file mode 100644 index 0000000..fffc274 Binary files /dev/null and b/Problems/Problem_2/a.out differ diff --git a/Problems/Problem_2/prob2.c b/Problems/Problem_2/prob2.c new file mode 100644 index 0000000..2fa8f34 --- /dev/null +++ b/Problems/Problem_2/prob2.c @@ -0,0 +1,34 @@ +#include +#include +#include +int power(int a, int b) +{ + int val=1;; + while(b!=0) + { + val=val*a; + b--; + } + return val; +} +int recurse(int n) +{ + if(n==0) + { + return 0; + } + else if(n==1) + { + return 1; + } + else + { + return (power(recurse(n-1),n-1)+power(recurse(n-2),n-2)); + } + +} +void main(int argc,char** argv) +{ + printf("%d\n",power(5,5)); + printf("%d\n",recurse(atoi(argv[1]))); +} \ No newline at end of file diff --git a/Problems/Problem_2/problem_2.c b/Problems/Problem_2/problem_2.c index dcda0c6..c55f485 100644 --- a/Problems/Problem_2/problem_2.c +++ b/Problems/Problem_2/problem_2.c @@ -1,23 +1,23 @@ -#include -#include - -int fibonacci(int n){ - if (n==1 || n==2){ - return n-1; - } - else{ - return pow(fibonacci(n-1),n-1) + pow(fibonacci(n-2),n-2); - } -} - -int main(void){ - int N; - printf("\nRecursive fibonacci relation = f(n) = f(n-1)^(n-1) + f(n-2)^(n-2)\n\n"); - printf("Please enter a number (Nth term of the sequence): "); - scanf("%d", &N); - int result; - result = fibonacci(N); - printf("f(%d) = %d\n", N, result); - - return 0; -} +#include +#include + +int fibonacci(int n){ + if (n==1 || n==2){ + return n-1; + } + else{ + return pow(fibonacci(n-1),n-1) + pow(fibonacci(n-2),n-2); + } +} + +int main(void){ + int N; + printf("\nRecursive fibonacci relation = f(n) = f(n-1)^(n-1) + f(n-2)^(n-2)\n\n"); + printf("Please enter a number (Nth term of the sequence): "); + scanf("%d", &N); + int result; + result = fibonacci(N); + printf("f(%d) = %d\n", N, result); + + return 0; +} diff --git a/Problems/Problem_2/problem_2.py b/Problems/Problem_2/problem_2.py index 3ae148f..af0186d 100644 --- a/Problems/Problem_2/problem_2.py +++ b/Problems/Problem_2/problem_2.py @@ -1,25 +1,25 @@ -# -*- coding: utf-8 -*- -""" -Created on Sat Oct 26 18:53:36 2019 - -@author: ANANTA SRIKAR -""" - -def f(n): - - if n == 1 or n == 2: - """Assuming the first two values are same as that of fibonacci""" - return n-1 - else: - return (f(n-1)**(n-1) + f(n-2)**(n-2)) - -def main(): - - print('f(n) = f(n-1)^(n-1) + f(n-2)^(n-2)') - n = int(input('Enter Nth term : ')) - val = f(n) - print('f(' + str(n) + ') = ' + str(val)) - -if __name__ == '__main__': - main() +# -*- coding: utf-8 -*- +""" +Created on Sat Oct 26 18:53:36 2019 + +@author: ANANTA SRIKAR +""" + +def f(n): + + if n == 1 or n == 2: + """Assuming the first two values are same as that of fibonacci""" + return n-1 + else: + return (f(n-1)**(n-1) + f(n-2)**(n-2)) + +def main(): + + print('f(n) = f(n-1)^(n-1) + f(n-2)^(n-2)') + n = int(input('Enter Nth term : ')) + val = f(n) + print('f(' + str(n) + ') = ' + str(val)) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Problems/Problem_3/Prob_3.txt b/Problems/Problem_3/Prob_3.txt index 90fe181..a994be9 100644 --- a/Problems/Problem_3/Prob_3.txt +++ b/Problems/Problem_3/Prob_3.txt @@ -1 +1 @@ -Find an efficient way to find the Nth prime in the series of natural numbers, Brute force will take you far but will it help if I asked you to find the 42,000th prime? +Find an efficient way to find the Nth prime in the series of natural numbers, Brute force will take you far but will it help if I asked you to find the 42,000th prime? diff --git a/Problems/Problem_3/Problem_3.py b/Problems/Problem_3/Problem_3.py index 14f644e..fb69285 100644 --- a/Problems/Problem_3/Problem_3.py +++ b/Problems/Problem_3/Problem_3.py @@ -1,31 +1,31 @@ -def isPrime(n): - if n == 2: - return True - - # Even numbers above 2 can't be prime - if n % 2 == 0: - return False - - # All primes above 3 are neighbors of a multiple of 6 - if n>3 and (n+1) % 6 != 0 and (n-1) % 6 != 0: - return False - - # For the rest, we check up to sqrt(n) for factors - i = 3 - while i*i <= n: - if n % i == 0: - return False - i+=2 - - return True - -def main(): - n = int(input()) - p = 2 - for i in range(n,0,-1): - while not isPrime(p): - p+=1 - p+=1 - print(str(n)+"th prime: "+str(p-1)) - -main() +def isPrime(n): + if n == 2: + return True + + # Even numbers above 2 can't be prime + if n % 2 == 0: + return False + + # All primes above 3 are neighbors of a multiple of 6 + if n>3 and (n+1) % 6 != 0 and (n-1) % 6 != 0: + return False + + # For the rest, we check up to sqrt(n) for factors + i = 3 + while i*i <= n: + if n % i == 0: + return False + i+=2 + + return True + +def main(): + n = int(input()) + p = 2 + for i in range(n,0,-1): + while not isPrime(p): + p+=1 + p+=1 + print(str(n)+"th prime: "+str(p-1)) + +main() diff --git a/Problems/Problem_4/Problem_4.txt b/Problems/Problem_4/Problem_4.txt index 4ad0a05..f40c7cd 100644 --- a/Problems/Problem_4/Problem_4.txt +++ b/Problems/Problem_4/Problem_4.txt @@ -1,3 +1,3 @@ -Find the sum of the digits of 78! -(! is the factorial sign, meaning that you multiply all the numbers from 1 up till the number n. - EG; 4! = 4 x 3 x 2 x 1) +Find the sum of the digits of 78! +(! is the factorial sign, meaning that you multiply all the numbers from 1 up till the number n. + EG; 4! = 4 x 3 x 2 x 1) diff --git a/Problems/Problem_4/prblm4.c b/Problems/Problem_4/prblm4.c new file mode 100644 index 0000000..9975b9f --- /dev/null +++ b/Problems/Problem_4/prblm4.c @@ -0,0 +1,36 @@ +#include +#include + +int fact(int n) +{ + if(n==0) + { + return 1; + } + else if(n==1) + { + return 1; + } + else + { + return fact(n-1)*n; + } +} +int summed(int a) +{ + int num = fact(a); + int d,sum=0; + while(num>0) + { + d=num%10; + sum=sum+d; + num=num/10; + } + return sum; + +} +void main(int argc, char** argv) +{ + printf("%d\n",fact(atoi(argv[1]))); + printf("%d\n",summed(atoi(argv[1]))); +} \ No newline at end of file diff --git a/Problems/Problem_4/problem_4.py b/Problems/Problem_4/problem_4.py index d139699..aa1c873 100644 --- a/Problems/Problem_4/problem_4.py +++ b/Problems/Problem_4/problem_4.py @@ -1,30 +1,30 @@ -# -*- coding: utf-8 -*- -""" -Created on Sat Oct 26 20:56:42 2019 - -@author: ANANTA SRIKAR -""" - -def fact(n): - fact = 1 - while(n > 0): - fact *= n - n -= 1 - return fact - -def sumOfDigits(n): - SUM = 0 - while(n>0): - digit = n % 10 - SUM += digit - n = n // 10 - return SUM - -def main(): - - n = 78 #You can also input a number here - print(str(n) + '! = ' + str(fact(n))) - print('Sum of Digits = ' + str(sumOfDigits(fact(n)))) - -if __name__ == '__main__': +# -*- coding: utf-8 -*- +""" +Created on Sat Oct 26 20:56:42 2019 + +@author: ANANTA SRIKAR +""" + +def fact(n): + fact = 1 + while(n > 0): + fact *= n + n -= 1 + return fact + +def sumOfDigits(n): + SUM = 0 + while(n>0): + digit = n % 10 + SUM += digit + n = n // 10 + return SUM + +def main(): + + n = 78 #You can also input a number here + print(str(n) + '! = ' + str(fact(n))) + print('Sum of Digits = ' + str(sumOfDigits(fact(n)))) + +if __name__ == '__main__': main() \ No newline at end of file diff --git a/Problems/Problem_5/Prob_5.txt b/Problems/Problem_5/Prob_5.txt index cc590fa..8b77b3b 100644 --- a/Problems/Problem_5/Prob_5.txt +++ b/Problems/Problem_5/Prob_5.txt @@ -1,3 +1,3 @@ -You are given an Array containing many integers, find the subarray with the largest sum. - -(Make your own examples to test your code). +You are given an Array containing many integers, find the subarray with the largest sum. + +(Make your own examples to test your code). diff --git a/Problems/Problem_5/problem_5.cpp b/Problems/Problem_5/problem_5.cpp index 4336c7d..f50a099 100644 --- a/Problems/Problem_5/problem_5.cpp +++ b/Problems/Problem_5/problem_5.cpp @@ -1,62 +1,62 @@ -/*input -5 -2 3 4 5 7 -*/ - -//sometimes it's the people who no one imagines anything of -//who do the things that no one can imagine. - -//code author: iamxlr8 - -#include -using namespace std; - -int max(int a, int b, int c) { return max(max(a, b), c); } - -int maxCrossingSum(int arr[], int l, int m, int h) -{ - int sum = 0; - int left_sum = INT_MIN; - for (int i = m; i >= l; i--) - { - sum = sum + arr[i]; - if (sum > left_sum) - left_sum = sum; - } - - sum = 0; - int right_sum = INT_MIN; - for (int i = m+1; i <= h; i++) - { - sum = sum + arr[i]; - if (sum > right_sum) - right_sum = sum; - } - - return left_sum + right_sum; -} - - -int maxSubArraySum(int arr[], int l, int h) -{ - if (l == h) - return arr[l]; - int m = (l + h)/2; - return max(maxSubArraySum(arr, l, m), - maxSubArraySum(arr, m+1, h), - maxCrossingSum(arr, l, m, h)); -} - - -int main() -{ - int n; - cout<<"enter array size\n"; - cin>>n; - int arr[n]; - cout<<"enter array elements\n"; - for(int i=0;i>arr[i]; - cout<<"maximum subarray sum is "< +using namespace std; + +int max(int a, int b, int c) { return max(max(a, b), c); } + +int maxCrossingSum(int arr[], int l, int m, int h) +{ + int sum = 0; + int left_sum = INT_MIN; + for (int i = m; i >= l; i--) + { + sum = sum + arr[i]; + if (sum > left_sum) + left_sum = sum; + } + + sum = 0; + int right_sum = INT_MIN; + for (int i = m+1; i <= h; i++) + { + sum = sum + arr[i]; + if (sum > right_sum) + right_sum = sum; + } + + return left_sum + right_sum; +} + + +int maxSubArraySum(int arr[], int l, int h) +{ + if (l == h) + return arr[l]; + int m = (l + h)/2; + return max(maxSubArraySum(arr, l, m), + maxSubArraySum(arr, m+1, h), + maxCrossingSum(arr, l, m, h)); +} + + +int main() +{ + int n; + cout<<"enter array size\n"; + cin>>n; + int arr[n]; + cout<<"enter array elements\n"; + for(int i=0;i>arr[i]; + cout<<"maximum subarray sum is "<