diff --git a/C-Plus-Plus/README.md b/C-Plus-Plus/README.md index 310511b406..763b72b1eb 100644 --- a/C-Plus-Plus/README.md +++ b/C-Plus-Plus/README.md @@ -226,6 +226,7 @@ - [Infix to Postfix conversion](stack/infixToPostfix.cpp) - [Next smallest element to the right](stack/nextSmallestToRight.cpp) - [Smallest element in an array](stack/smallest_element_in_an_array.cpp) +- [Sort stack using recursion](stack\Sort_stack_using_recursion.cpp) ## Heap based problems diff --git a/C-Plus-Plus/stack/Sort_stack_using_recursion.cpp b/C-Plus-Plus/stack/Sort_stack_using_recursion.cpp new file mode 100644 index 0000000000..f9a5a3c205 --- /dev/null +++ b/C-Plus-Plus/stack/Sort_stack_using_recursion.cpp @@ -0,0 +1,107 @@ +/* +Problem Statement: + Given a stack, sort the given stack using recursion such that the greatest element is on the top. +*/ + +#include +#include +#include + +using namespace std; + +void printStack(stack st) +{ + while (!st.empty()) + { + int x = st.top(); + st.pop(); + + cout << x << " "; + } + + return; +} + +void insert(stack &st, int temp) +{ + // base condition + if (st.size() == 0 || st.top() < temp) + { + st.push(temp); + return; + } + + int val = st.top(); + st.pop(); + + insert(st, temp); + st.push(val); + + return; +} + +void sortStack(stack &st) +{ + + // base condition + if (st.size() == 0) + return; + + int temp = st.top(); + st.pop(); + + // recursion + sortStack(st); + + // function to insert element in stack + insert(st, temp); + + return; +} + +int main() +{ + // input number of element in stack + int n; + cin >> n; + + stack st; + + // input stack elements + for (int i = 0; i < n; i++) + { + int x; + cin >> x; + + st.push(x); + } + + // function to sort stack + sortStack(st); + + // function to print stack element + printStack(st); + + cout << endl; + + return 0; +} + +/* + Time Complexity: O(N*N) + Space complexity: O(N) + +*/ + +/* + Test Case: + + Input: 10 + 9 2 5 6 0 1 7 3 4 8 + OutPut: 9 8 7 6 5 4 3 2 1 0 + + Input: 5 + 11 2 32 3 41 + Output: 41 32 11 3 2 + +*/ diff --git a/Java/README.md b/Java/README.md index 6d7544b3fb..4f64ce874d 100644 --- a/Java/README.md +++ b/Java/README.md @@ -175,6 +175,7 @@ _add list here_ - [Factorial Using Big Integer](math/Big_Integer_Factorial.java) - [Polynomial Sum & Product using Linked Lists](math/polynomial.java) - [Neon number](Neon_number.java) +- [Roots of Quadratic Equation](Quadratic_Equation.java) ## Dynamic Programming diff --git a/Java/math/Quadratic_Equation.java b/Java/math/Quadratic_Equation.java new file mode 100644 index 0000000000..9ce8c3e34b --- /dev/null +++ b/Java/math/Quadratic_Equation.java @@ -0,0 +1,98 @@ +/* +Problem: Solving Quadratic equations in Java- +A program that prints all real solutions to quadratic equation ax^2+bx+c=0, if discriminant is negative displays a message "roots are imaginary". +*/ +import java.util.*; +import java.io.*; + +public class Quadratic_Equation { + public static void quadraticroots() + { + // value a, b, and c + double a, b, c; + double root1, root2; + Scanner sc = new Scanner(System.in); + System.out.println("Enter coefficients of the equation:"); + System.out.print("Enter a: "); + a = sc.nextDouble(); + System.out.println(); + System.out.print("Enter b: "); + b = sc.nextDouble(); + System.out.println(); + System.out.print("Enter c: "); + c = sc.nextDouble(); + System.out.println(); + // If a is 0, then equation is not quadratic, but linear + if (a == 0) { + System.out.println("Its a linear equation,Not a quadratic equation!"); + return; + } + // calculate the discriminant (b^2 - 4ac) + double discriminant = b * b - 4 * a * c; + // discriminant is greater than 0, real and distinct roots + if (discriminant > 0) { + root1 = (-b + Math.sqrt(discriminant)) / (2 * a); + root2 = (-b - Math.sqrt(discriminant)) / (2 * a); + System.out.println("Roots are real and distinct"); + System.out.format("root1 = %.2f and root2 = %.2f", root1, root2); + } + // discriminant is equal to 0, real and equal roots + else if (discriminant == 0) { + root1 = root2 = -b / (2 * a); + System.out.println("Equal roots"); + System.out.format("root1 = root2 = %.2f;", root1); + } + // discriminant is less than zero, imaginary roots + else { + double real = -b / (2 * a); + double imaginary = Math.sqrt(-discriminant) / (2 * a); + System.out.println("Imaginary roots!"); + System.out.format("root1 = %.2f+%.2fi", real, imaginary); + System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary); + } + } + public static void main(String[] args) { + quadraticroots(); + } +} + +/* +Sample Test Cases: + +Test case 1: +Enter coefficients of the equation: +Enter a: 1 + +Enter b: -4 + +Enter c: 0 + +Roots are real and distinct +root1 = 4.00 and root2 = 0.00 + +Test case 2: +Enter coefficients of the equation: +Enter a: 7.2 + +Enter b: 5 + +Enter c: 9 + +Imaginary roots! +root1 = -0.35+1.06i +root2 = -0.35-1.06i + +Test case 3: +Enter coefficients of the equation: +Enter a: 1 + +Enter b: -2 + +Enter c: 1 + +Equal roots +root1 = root2 = 1.00; + +Time Complexity: O(1) +Space Complexity: O(1) +*/ diff --git a/Julia/README.md b/Julia/README.md index bd6b9ab157..ebb579a4c3 100644 --- a/Julia/README.md +++ b/Julia/README.md @@ -13,3 +13,4 @@ - [Contiguous Sub Array with Maximum Sum](cp/Maximum_subarray_sum.jl) - [Finding Prime numbers using Sieve](cp/Sieve_of_Eratosthenes.jl) +- [Prime Factorization using Sieve](cp/Prime_factorization.jl) diff --git a/Julia/cp/Prime_factorization.jl b/Julia/cp/Prime_factorization.jl new file mode 100644 index 0000000000..7f1db0a86f --- /dev/null +++ b/Julia/cp/Prime_factorization.jl @@ -0,0 +1,46 @@ +#= Given a number n, print its prime factorization. We could do it +normal way which will be having time complexity of O (sqrt(N)) +but we will be using Sieve to lessen the time complexity +We will make a sieve to get all the prime numbers and then get the +prime factorization of the number using that.=# + +## Function + +function PrimeFactor(n) + a = zeros(Int64, n) + for i = 2:n + a[i] = i + end + for i = 2:n + if (a[i] == i) + for j = (i*i):i:n + if (a[j] == j) + a[j] = i + end + end + end + end + while (n != 1) + println(a[n]) + n = div(n, a[n]) + end +end + +## Input + +n = readline() +n = parse(Int64, n) + +#Calling the function + +PrimeFactor(n) + +#= +Sample Test case +Input: + n = 495 +Output: + 3 3 5 11 + +Time Complexity: O( log N ) +=# \ No newline at end of file diff --git a/Python/README.md b/Python/README.md index 803c8cca79..a8b2067a58 100644 --- a/Python/README.md +++ b/Python/README.md @@ -75,6 +75,7 @@ - [Keyword Cipher](cryptography/Keyword_Cipher.py) - [Vernam Cipher](cryptography/Vernam_Cipher.py) - [Columnar Cipher](cryptography/Columnar_Cipher.py) +- [Rail Fence Cipher](cryptography/Rail_Fence_Cipher.py) ## Searching diff --git a/Python/cryptography/Rail_Fence_Cipher.py b/Python/cryptography/Rail_Fence_Cipher.py new file mode 100644 index 0000000000..75fd4a9a55 --- /dev/null +++ b/Python/cryptography/Rail_Fence_Cipher.py @@ -0,0 +1,102 @@ +''' +In rail-fence cipher, we are given a plaintext message and a +numeric key. It is also called a zigzag cipher and is a form +of transposition cipher. We re-arrange the order of alphabets +in the plaintext to obtain the ciphertext. In this algorithm, +the plaintext text is written diagonally downwards and upwards +alternatively. The length of the diagonal is the keyword given. +After each alphabet has been written, the individual rows are +combined to obtain the cipher-text. +''' + +import sys + +'''This is the encryption function, which takes plaintext +and key as input. It then returns the ciphertext.''' + +def encryption(message, keyword): + + # This is the matrix to store the plaintext + matrix = [[] for i in range(keyword)] + + i = 0 + count = 0 + + ''' After going diagonally downwards keyword number of + times, we need to go diangonally upwards. This rev + list contains those values after which the plaintext + is to be stored diagonally upwards.''' + rev = [] + for j in range(len(message)): + temp = keyword + j * (( 2 * keyword) - 2) + if temp <= len(message): + rev.append(temp) + + value = False + + while (i < keyword and count < len(message)): + matrix[i].append(message[count]) + count = count + 1 + + ''' As we find the reversing point, we set value to + be true. So, that we can decrease the value of list + index.''' + if count in rev: + value = True + dec = keyword - 1 + + ''' If value is true, we decrement the list index keyword + number of times''' + if value and dec > 0: + i = i - 1 + dec = dec - 1 + + # Else we go on storing the plaintext in downwards direction + else: + i = i + 1 + + # This string stores the final ciphertext + enc = "" + + # We read the value of matrix row by row to form the ciphertext + for j in range(len(matrix)): + enc += ''.join(matrix[j]) + + # We return the ciphertext + return enc + +# Calling the driver function +if __name__=='__main__': + + # Taking plaintext and key as input from the user + plaintext = input("Enter a message you want to encrypt : ") + key = int(input("Enter a key to encrypt the message : ")) + + if key <= 0: + print("Enter a valid key.") + sys.exit(0) + + elif key == 1: + encrypted_message = plaintext + print("Encrypted plaintext is", " "*12, ":", encrypted_message) + sys.exit(0) + + ''' Calling the encryption function on the given paintext + and key''' + encrypted_message = encryption(plaintext, key) + + # Printing the ciphertext we got using the encryption method + print("Encrypted plaintext is", " "*12, ":", encrypted_message) + +''' +Sample I/O: +a) + Enter a message you want to encrypt : This is Rail Fence Cipher + Enter a key to encrypt the message : 4 + Encrypted plaintext is : Ts rhi lFeCei Riecihsanp +b) + Enter a message you want to encrypt : thisisrisky + Enter a key to encrypt the message : 1 + Encrypted plaintext is : thisisrisky +''' +