├── .gitignore ├── Euler_Totient_Function └── README.md ├── .github ├── ISSUE_TEMPLATE │ └── gssoc.md └── pull_request_template.md ├── Sleep_Sort ├── Sleep sort.jl ├── Sleep_Sort.coffee ├── Sleep_Sort.sh ├── Sleep_Sort.rb ├── sleep_sort.erl ├── Sleep_Sort.c └── Sleep_Sort.cpp ├── Euclidean_Algorithm ├── Euclidean_Algorithm.clj ├── Euclidean_Algorithm.php ├── Euclidean_Algorithm.coffee ├── Euclidean_Algorithm.py ├── Euclidean_Algorithm.scala ├── Euclidean_Algorithm.ts ├── Euclidean_Algorithm.java ├── Euclidean_Algorithm.rb ├── Euclidean_Algorithm.js ├── Euclidean_Algorithm.kt └── Euclidean_Algorithm.c ├── Birthday_Paradox ├── Birthday_Paradox.py ├── Birthday_Paradox.cpp └── Birthday_Paradox.java ├── Bubble_Sort ├── Bubble_Sort.sh ├── Bubble_Sort.coffee ├── Bubble_Sort.go ├── Bubble_Sort.rb ├── Bubble_Sort.jl ├── Bubble_Sort.py ├── Bubble_Sort.js ├── Bubble_Sort.php └── Modified_Bubble_Sort.py ├── Josephus_Problem ├── Josephus_Problem.c ├── Josephus_Problem.cpp └── Josephus_Problem.py ├── Linear_Search ├── Linear_Search.jl ├── Linear_Search.coffee ├── Linear_Search.js ├── Linear_Search.py ├── Linear_Search.php ├── Linear_Search.rb ├── Linear_Search.c ├── Linear_Search.cpp └── Linear_Search.go ├── Anagram ├── Anagram.py └── Anagram.js ├── Insertion_Sort ├── Insertion_Sort.js ├── Insertion_Sort.coffee ├── Insertion_Sort.rb └── Insertion_Sort.py ├── AKS_Primarility_Test ├── AKS.js ├── AKS.py └── AKS.cpp ├── Leap Year Code in JavaScript └── script.js ├── Longest_Increasing_Subsequence └── LIS.rb ├── Number_Conversion ├── Python │ ├── Decimal_To_Octal.py │ ├── Octal_To_Decimal.py │ ├── Binary_To_Decimal.py │ └── Decimal_To_Hexadecimal.py ├── C │ ├── Decimal_To_Octal.c │ ├── Octal_To_Decimal.c │ ├── Binary_To_Decimal.c │ └── Decimal_To_Hexadecimal.c ├── C++ │ ├── Decimal_To_Octal.cpp │ ├── Binary_To_Decimal.cpp │ └── Octal_To_Decimal.cpp └── Java │ └── Decimal_To_Octal.java ├── Compound_Interest ├── Compound_Interest.py ├── Compound_Interest.c ├── Compound_Interest.cpp ├── Compound_Interest.go └── Compound_Interest.java ├── Digit_Count ├── Digit_Count.py ├── Digit_Count.kt ├── Digit_Count.cpp ├── Digit_Count.c ├── Digit_Count.go ├── Digit_Count.rb └── Digit_Count.java ├── Sum_Of_Squares ├── Sum_Of_Squares.kt ├── Sum_Of_Squares.py ├── Sum_O_Squares.go ├── Sum_Of_Squares.c ├── Sum_Of_Squares.cpp └── Sum_Of_Squares.java ├── Naive_String_Matching ├── Naive_Approach.php ├── Naive_Approach.rb ├── Naive_Approach.coffee └── Naive_String_Matching.py ├── Bead_Sort └── Bead_Sort.py ├── Selection_Sort ├── Selection_Sort.coffee ├── Selection_Sort.js ├── Selection_Sort.py ├── Selection_Sort.rb └── Selection_Sort.kt ├── Perfect_Number ├── Perfect_Number.py ├── Perfect_Number.go ├── Perfect_Number.c ├── Perfect_Number.cpp └── Perfect_Number.java ├── Stacks_Using_Arrays └── Stacks_Using_Arrays.js ├── Logarithmic_Exponent ├── Logarithmic_Exponent.coffee ├── Logarithmic_Exponent.py ├── Logarithmic_Exponent.c └── Logarithmic_Exponent.cpp ├── Simple_Interest ├── Simple_Interest.py ├── Simple_Interest.cpp ├── Simple_Interest.c ├── Simple_Interest.go └── Simple_Interest.java ├── Array_Rotation ├── Array_Rotation.py └── Array_Rotation.js ├── Sum_Of_Digits ├── Sum_Of_Digits.kt ├── Sum_Of_Digits.py ├── Sum_Of_Digits.c ├── Sum_Of_Digits.go ├── Sum_Of_Digits.js └── Sum_Of_Digits.cpp ├── Activity_Selection └── Activity_Selection.rb ├── Shell_Sort ├── Shell_Sort.coffee ├── Shell_Sort.py └── Shell_Sort.c ├── Natural_Numbers_Sum ├── Natural_Numbers_Sum.py ├── Natural_Numbers_Sum.c ├── Natural_Numbers_Sum.go ├── Natural_Numbers_Sum.cpp └── Natural_Numbers_Sum.java ├── Sum_Of_Cubes ├── Sum_Of_Cubes.py ├── Sum_Of_Cubes.cpp ├── Sum_Of_Cubes.c ├── Sum_Of_Cubes.go ├── Sum_Of_Cubes.js └── Sum_Of_Cubes.java ├── Extended_Euclidean_Algorithm ├── Extended_Euclidean_Algorithm.coffee └── Extended_Euclidean_Algorithm.ts ├── Automorphic_Number ├── Automorphic_Number.kt ├── Automorphic_Number.java └── Automorphic_Number.c ├── Kadane_Algorithm ├── Kadane_Algorithm.rb └── Kadane_Algorithm.js ├── Gcd ├── gcd.py └── gcd.js ├── Nonagonal_Number ├── Nonagonal_Number.py ├── Nonagonal_Number.c ├── Nonagonal_Number.cpp ├── Nonagonal_Number.go └── Nonagonal_Number.java ├── Next_Perfect_Square ├── Next_Perfect_Square.py ├── Next_Perfect_Square.c ├── Next_Perfect_Square.cpp ├── Next_Perfect_Square.go └── Next_Perfect_Square.java ├── Centered_Square_Number ├── Centered_Square_Number.py ├── Centered_Square_Number.go ├── Centered_Square_Number.c ├── Centered_Square_Number.cpp └── Centered_Square_Number.java ├── Fibonacci_Words ├── Fibonacci_Words.py ├── Fibonacci_Words.cpp └── Fibonacci_Words.go ├── Decagonal_Numbers ├── Decagonal_Numbers.py ├── Decagonal_Numbers.go ├── Decagonal_Numbers.c └── Decagonal_Numbers.cpp ├── Babylonian_Method ├── Babylonian_Method.kt ├── Babylonian_Method.py └── Babylonian_Method.js ├── Rotation_Of_Numbers ├── Rotation_Of_Numbers.py ├── Rotation_Of_Numbers.c └── Rotation_Of_Numbers.cpp ├── Factorial └── Factorial.java ├── Tower_Of_Hanoi ├── Tower_Of_Hanoi.js ├── Tower_Of_Hanoi.py ├── Tower_Of_Hanoi.c └── Tower_Of_Hanoi.cpp ├── 1-D_Array ├── Python │ └── Frequency_Of_A_Element.py └── Ruby │ └── Max_And_Next_Max_Number.rb ├── Inheritance(Java) └── Single_Level.java ├── Prefix_Sum ├── Prefix_Sum.cpp └── Prefix_Sum.js ├── Tree_Inorder_Traversal ├── Tree_Inorder_Traversal.go ├── Tree_Inorder_Traversal.py ├── Tree_Inorder_Traversal.rb └── Tree_Inorder_Traversal.php ├── Tree_Postorder_Traversal ├── Tree_Postorder_Traversal.go ├── Tree_Postorder_Traversal.py └── Tree_Postorder_Traversal.rb ├── Tree_Preorder_Traversal ├── Tree_Preorder_Traversal.go ├── Tree_Preorder_Traversal.py ├── Tree_Preorder_Traversal.rb └── Tree_Preorder_Traversal.php ├── Dynamic_Programming_Rod_Cutting ├── Dynamic_Programming_Rod_Cutting.js ├── Dynamic_Programming_Rod_Cutting.py └── Dynamic_Programming_Rod_Cutting.php ├── Set_Bits_Till_N ├── Set_Bits_Till_N.py ├── Set_Bits_Till_N.js ├── Set_Bits_Till_N.c ├── Set_Bits_Till_N.cpp └── Set_Bits_Till_N.go ├── Tribonacci_Numbers ├── Tribonacci_Numbers.py ├── Tribonacci_Numbers.c ├── Tribonacci_Numbers.cpp └── Tribonacci_Numbers.go ├── Root_Mean_Square ├── Root_Mean_Square.py ├── Root_Mean_Square.c └── Root_Mean_Square.cpp ├── Bitwise_Addition ├── bitwise_addition.c └── bitwise_addition.cpp ├── Centered_Decagonal_Number ├── Centered_Decagonal_Number.py ├── Centered_Decagonal_Number.go ├── Centered_Decagonal_Number.c └── Centered_Decagonal_Number.cpp ├── Sieve_Of_Eratosthenes ├── Sieve_Of_Eratosthenes.py ├── Sieve_Of_Eratosthenes.php ├── Sieve_Of_Eratosthenes.coffee └── Sieve_Of_Eratosthenes.kt ├── Tribonacci_Words ├── Tribonacci_Words.py ├── Tribonacci_Words.cpp └── Tribonacci_Words.go ├── Ugly_Number ├── Ugly_Number.py └── Nth_Ugly_Number.rb ├── Fibonacci_Number ├── Fibonacci_Number.php └── Fibonacci_Number.py ├── Knapsack_Unbounded └── Knapsack_Unbounded.py ├── Reverse_Of_Number ├── Reverse_Of_Number.py ├── Reverse_Of_Number.c ├── Reverse_Of_Number.go └── Reverse_Of_Number.cpp ├── Chocolate_Distribution └── README.md ├── Modular_Exponentiation ├── Modular_Exponentiation.py ├── Iterative_Modular_Exponentiation.c └── Recursive_Modular_Exponentiation.c ├── Pythagorean_Triplets └── Pythagorean_Triplets.py ├── Lucas_Numbers ├── Lucas_Number.py └── Lucas_Number.c ├── Geometric_Progression ├── Sum_Of_Infinite_GP.py ├── Sum_Of_Infinite_GP.cpp └── Sum_Of_Infinite_GP.c ├── Prime_Number └── Prime_Number.py ├── Power_Of_Two ├── Power_Of_Two.py ├── Power_Of_Two.js └── Power_Of_Two.c ├── Inheritance(C++) └── Single_Level.cpp ├── Taylor_series_Repo └── Taylor_Series_Horner_Rule.cpp ├── Binary_Search ├── Binary_Search.jl ├── Binary_Search.coffee └── Binary_Search.php ├── Delannoy_Number └── Delannoy_Number.py ├── Dearrangements └── Dearrangements.py ├── Divisible_Pairs_In_Array └── Divisible_Pairs_In_Array.py ├── Leaders_Of_Array └── Leaders_Of_Array.py ├── Palindrome ├── palindrome_string.py └── palindrome_no.py ├── Prefix_Function └── Prefix_Function.cpp ├── Armstrong_Number └── Armstrong_Number.py ├── Pascal_Triangle ├── Pascal_Triangle.c └── Pascal_Triangle.cpp ├── Cyclic_Permutation └── Cyclic_Permutation.py ├── Fermat_Little_Theorem ├── Fermat_Little_Theorem.coffee ├── Fermat_Little_Theorem.rb ├── Fermat_Little_Theorem.cpp └── Fermat_Little_Theorem.py ├── Lucas_Theorem ├── Lucas_Theorem.py └── Lucus_Theorem.cpp ├── Karatsuba_Algorithm └── karatsuba.py ├── Palindromic_Array └── Palindromic_Array.py ├── Jolly_Jumper_Sequence └── Jolly_Jumper_Sequence.py ├── Coprime_Numbers └── Coprime_Numbers.py ├── Quick_Sort └── Quick_Sort.coffee ├── Pythagorean_Quadruples └── Pythagorean_Quadruples.py ├── Interpolation_Search ├── Interpolation_Search.coffee └── Interpolation_Search.py ├── Boyer_Moore_Algorithm └── Boyer_Moore.py ├── Subarray_Sum └── Subarray_Sum.py ├── Leap_Year └── Leap_Year.py ├── Merge_Sort └── Merge_Sort.go └── Spiral_Array └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .vscode/* -------------------------------------------------------------------------------- /Euler_Totient_Function/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jainaman224/Algo_Ds_Notes/HEAD/Euler_Totient_Function/README.md -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/gssoc.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: GSSoC 3 | about: Describe this issue template's purpose here. 4 | title: Do not create issue 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 11 | -------------------------------------------------------------------------------- /Sleep_Sort/Sleep sort.jl: -------------------------------------------------------------------------------- 1 | input = [3,2,4,7,3,6,9,1] 2 | output = Int[] 3 | 4 | @sync for i in input 5 | @async begin 6 | sleep(i) 7 | push!(output, i) 8 | end 9 | end 10 | 11 | @assert output == sort(input) 12 | println(output) 13 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.clj: -------------------------------------------------------------------------------- 1 | (defn GCD [num1 num2] 2 | (loop [a num1 3 | b num2] 4 | (if (= b 0) 5 | a 6 | (recur b (mod a b))))) 7 | 8 | (GCD 30 105) ;; => 15 9 | (GCD 105 30) ;; => 15 10 | (GCD 2 3);; => 1 11 | (GCD -3 3);; => -3 12 | -------------------------------------------------------------------------------- /Birthday_Paradox/Birthday_Paradox.py: -------------------------------------------------------------------------------- 1 | #Python3 program to approximate the number of people in Birthday Paradox problem 2 | import math 3 | 4 | def find(p): 5 | return math.ceil(math.sqrt(2*365*math.log(1/(1-p)))) 6 | 7 | print(find(0.70)) 8 | 9 | """ 10 | Output 11 | 30 12 | """ -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Sleep_Sort/Sleep_Sort.coffee: -------------------------------------------------------------------------------- 1 | After = (s, f) -> setTimeout f, s*1000 2 | 3 | Sleep_Sort = (array) -> 4 | for n in array 5 | do (n) -> After n, -> console.log n 6 | 7 | do -> 8 | array = [2, 4, 3, 1, 6, 8, 4] 9 | Sleep_Sort array 10 | 11 | ### Output 12 | 13 | 1 14 | 2 15 | 3 16 | 4 17 | 4 18 | 6 19 | 8 20 | 21 | ### 22 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.coffee: -------------------------------------------------------------------------------- 1 | gcdFunction = (x, y) -> 2 | if x is 0 3 | y 4 | else 5 | gcdFunction y % x, x 6 | 7 | x = 462 8 | y = 780 9 | 10 | console.log "GCD of numbers " + x + " and " + y + " is " + gcdFunction x, y 11 | 12 | 13 | ### Output 14 | 15 | GCD of numbers 462 and 780 is 6 16 | 17 | ### 18 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.sh: -------------------------------------------------------------------------------- 1 | A=(54 34 55 67 11) 2 | 3 | for ((i=0;i<5;i++)) 4 | do 5 | for((j=0;j<4;j++)) 6 | do 7 | if ((${A[j]} > ${A[$((j+1))]})) 8 | then 9 | a=${A[$j]} 10 | A[$j]=${A[$((j+1))]} 11 | A[$((j+1))]=$a 12 | fi 13 | done 14 | done 15 | echo ${A[*]} 16 | 17 | 18 | #------ OUTPUT ------- 19 | # 11 34 54 55 67 -------------------------------------------------------------------------------- /Josephus_Problem/Josephus_Problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | int josephus(int no,int h) 3 | { 4 | if(no==1) 5 | return 1; 6 | else 7 | return (josephus(no - 1,h)+ h-1) % no + 1; 8 | } 9 | int main() 10 | { 11 | int a = 20; 12 | int b = 5; 13 | printf("The chosen place is %d",josephus(a,b)); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.jl: -------------------------------------------------------------------------------- 1 | function Linear_Search(x,key) 2 | if in(key,x) == true #checking for key 3 | println("Key $key is found at postion $(find(x .==key))") 4 | else 5 | println("Key $key not found") 6 | end 7 | end 8 | 9 | 10 | x = [1,23,3,45,6,67] 11 | 12 | Linear_Search(x,3) 13 | -------------------------------------------------------------------------------- /Anagram/Anagram.py: -------------------------------------------------------------------------------- 1 | def check_anagram(s1, s2): 2 | if( sorted(s1) != sorted(s2) ): #sort both the strings and check if they are same 3 | print("It is not an anagram") 4 | else: 5 | print("It is an anagram") 6 | 7 | String1 = input("Enter input string 1 = ") 8 | String2 = input("Enter input string 2 = ") 9 | check_anagram(String1,String2) 10 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.coffee: -------------------------------------------------------------------------------- 1 | Bubble_Sort = (list) -> 2 | for i in [0...list.length] 3 | for j in [0...list.length - i] 4 | [list[j], list[j+1]] = [list[j + 1], list[j]] if list[j] > list[j + 1] 5 | list 6 | 7 | array = [2, 4, 3, 1, 6, 8, 4] 8 | 9 | console.log Bubble_Sort array 10 | 11 | ### Output 12 | 13 | [ 1, 2, 3, 4, 4, 6, 8 ] 14 | 15 | ### 16 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.py: -------------------------------------------------------------------------------- 1 | def gcdFunction(x, y): 2 | if x == 0: 3 | return y 4 | else: 5 | return gcdFunction(y % x, x) 6 | 7 | x = 462 8 | y = 780 9 | 10 | print("GCD of numbers " + str(x) + " and " + str(y) + " is " + str(gcdFunction(x, y))) 11 | 12 | 13 | ''' Output 14 | 15 | GCD of numbers 462 and 780 is 6 16 | 17 | ''' 18 | -------------------------------------------------------------------------------- /Josephus_Problem/Josephus_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int josephus(int no,int h) 4 | { 5 | if(no==1) 6 | return 1; 7 | else 8 | return (josephus(no - 1,h)+ h-1) % no + 1; 9 | } 10 | int main() 11 | { 12 | int a = 20; 13 | int b = 5; 14 | cout<<"The chosen place is "<= 0 && array[j] > temp; j-- ){ 6 | array[j+1] = array[j]; 7 | } 8 | array[j+1] = temp; 9 | } 10 | return array; 11 | } 12 | 13 | array = [4, 3, 6, 9, 5, 1, 8, 7, 2]; 14 | 15 | console.log(Insertion_Sort(array)); 16 | -------------------------------------------------------------------------------- /AKS_Primarility_Test/AKS.js: -------------------------------------------------------------------------------- 1 | var isprm = function(n) { 2 | var i=5; 3 | var w=2; 4 | if(n==2 || n== 3) 5 | return 1; 6 | if(n%2==0 || n%3==0 || n==1) 7 | return 0; 8 | while(i*i <= n){ 9 | if(n%i == 0) 10 | return 0; 11 | i +=w; 12 | w=6-w; 13 | } 14 | return 1; 15 | } 16 | 17 | var i=0; 18 | for(; i<1000000; i++){ 19 | if(isprm(i)==1) 20 | console.log(i + '\n'); 21 | } 22 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.scala: -------------------------------------------------------------------------------- 1 | object GCD extends App { 2 | def GCD(num1: Int, num2: Int): Int = { 3 | def iterate(a: Int, b: Int): Int = { 4 | if (b == 0) { 5 | return a 6 | } else { 7 | return iterate(b, a % b) 8 | } 9 | } 10 | return iterate(num1, num2) 11 | } 12 | 13 | GCD(30, 105) // res1: Int = 15 14 | GCD(105, 30) // res2: Int = 15 15 | } 16 | -------------------------------------------------------------------------------- /Leap Year Code in JavaScript/script.js: -------------------------------------------------------------------------------- 1 | function leapyear() { 2 | var year; 3 | 4 | year = document.getElementById("year").value; 5 | 6 | if((0 == year % 4) && (0 != year % 100) || (0 == year % 400)){ 7 | alert(year + " is a leap year"); 8 | } 9 | else { 10 | alert(year + " is not a leap year"); 11 | } 12 | } 13 | 14 | //Sample IP = 2020 15 | //Sample OP = 2020 is a leap year -------------------------------------------------------------------------------- /Longest_Increasing_Subsequence/LIS.rb: -------------------------------------------------------------------------------- 1 | def lis(arr) 2 | n=arr.size 3 | lis=Array.new(n, 1) 4 | 5 | ans=-1<<30 6 | 7 | n.times do |i| 8 | i.times do |j| 9 | if arr[j] 4 | #include 5 | using namespace std; 6 | 7 | // Returns approximate number of people for a given probability 8 | int find(double p) 9 | { 10 | return ceil(sqrt(2*365*log(1/(1-p)))); 11 | } 12 | 13 | int main() 14 | { 15 | printf("%d",find(0.70)); 16 | } 17 | 18 | /* Output 19 | 3o 20 | */ 21 | -------------------------------------------------------------------------------- /Compound_Interest/Compound_Interest.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Compound Interest = P(1 + R/100)^T 3 | 4 | P is principle amount 5 | R is the rate and 6 | T is the time span 7 | ''' 8 | 9 | import math 10 | 11 | p = int(input("Enter Principal: ")) 12 | r = int(input("Enter Rate: ")) 13 | t = int(input("Enter Time: ")) 14 | print(p * (math.pow((1 + r / 100), t))) 15 | 16 | ''' 17 | INPUT : p = 1000, r = 8, t = 2 18 | OUTPUT : 1166.40 19 | ''' 20 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.py: -------------------------------------------------------------------------------- 1 | ''' 2 | DIGIT COUNT OF A NUMBER 3 | 4 | The task is to count the number of digits present in the given 5 | number. 6 | ''' 7 | 8 | def getDigits(n): 9 | ans = 0 10 | while (n): 11 | ans = ans + 1 12 | n = n // 10 13 | return ans 14 | 15 | n = int(input("Enter N: ")) 16 | print("Number of digits is :", getDigits(n)) 17 | 18 | ''' 19 | INPUT : n = 6534 20 | OUTPUT: Number of digits is : 4 21 | ''' 22 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_Of_Squares.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main(args: Array) { 4 | val read = Scanner(System.`in`) 5 | print("Enter the Number : ") 6 | var num = read.nextInt() 7 | var ans = ((num) * (num + 1) * (2 * num + 1))/6 8 | println("The sum of square from 1 to N number is : ${ans}") 9 | } 10 | 11 | /* 12 | Input : Enter the Number : 223 13 | Output : The sum of square from 1 to N number is : 140 14 | */ 15 | -------------------------------------------------------------------------------- /Naive_String_Matching/Naive_Approach.php: -------------------------------------------------------------------------------- 1 | " ; 17 | } 18 | ?> 19 | -------------------------------------------------------------------------------- /Bead_Sort/Bead_Sort.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def beadsort(series, max_num=10): 4 | len_series = len(series) 5 | array = np.array([[True]*num + [False]*(max_num-num) for num in series]) 6 | array = np.array([[True]*sum(array[:,idx]) + [False]*(len_series-sum(array[:,idx])) for idx in range(max_num)]) 7 | return [sum(array[:,idx]) for idx in range(len_series)] 8 | 9 | a = [2,4,1,3,3] 10 | print(beadsort(a)) 11 | 12 | #[4, 3, 3, 2, 1] 13 | 14 | -------------------------------------------------------------------------------- /Insertion_Sort/Insertion_Sort.coffee: -------------------------------------------------------------------------------- 1 | Insertion_Sort = (array) -> 2 | for i in [0...array.length] 3 | temp = array[i] 4 | j = i - 1 5 | 6 | while j >= 0 and array[j] > temp 7 | array[j + 1] = array[j] 8 | j-- 9 | 10 | array[j + 1] = temp 11 | array 12 | 13 | array = [2, 4, 3, 1, 6, 8, 4] 14 | 15 | console.log Insertion_Sort(array) 16 | 17 | 18 | ### Output 19 | 20 | [ 1, 2, 3, 4, 4, 6, 8 ] 21 | 22 | ### 23 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.coffee: -------------------------------------------------------------------------------- 1 | Linear_Search = (array, desired) -> 2 | for x in array 3 | return true if x is desired 4 | 5 | false 6 | 7 | array = [2, 4, 6, 7, 3, 1, 5] 8 | 9 | if Linear_Search array, 4 10 | console.log "Found" 11 | else 12 | console.log "Not Found" 13 | 14 | if Linear_Search array, 9 15 | console.log "Found" 16 | else 17 | console.log "Not Found" 18 | 19 | ### Output 20 | 21 | Found 22 | Not Found 23 | 24 | ### 25 | -------------------------------------------------------------------------------- /AKS_Primarility_Test/AKS.py: -------------------------------------------------------------------------------- 1 | def expandX1(m): 2 | c = [1] 3 | for i in range(m): 4 | c.append(c[-1] * -(m-i) / (i+1)) 5 | return c[::-1] 6 | 7 | def isPrime(m): 8 | if m < 2: return False 9 | c = expandX1(m) 10 | c[0] += 1 11 | return not any(mul % m for mul in c[0:-1]) 12 | 13 | 14 | #----DRIVER PROGRAM---- 15 | print('\n# [for small primes]AKS TEST GAVE THE FOLLOWING AS PRIMES') 16 | print([m for m in range(1000) if isPrime(m)]) 17 | 18 | 19 | -------------------------------------------------------------------------------- /Selection_Sort/Selection_Sort.coffee: -------------------------------------------------------------------------------- 1 | Selection_Sort = (array) -> 2 | for i in [0...array.length] 3 | min_index = i 4 | for j in [i + 1...array.length] 5 | min_index = j if array[j] < array[min_index] 6 | 7 | [array[i], array[min_index]] = [array[min_index], array[i]] 8 | array 9 | 10 | array = [2, 4, 3, 1, 6, 8, 4] 11 | 12 | console.log Selection_Sort array 13 | 14 | 15 | ### Output 16 | 17 | [ 1, 2, 3, 4, 4, 6, 8 ] 18 | 19 | ### 20 | -------------------------------------------------------------------------------- /Perfect_Number/Perfect_Number.py: -------------------------------------------------------------------------------- 1 | # Perfect number is a positive integer that is equal to the sum of its proper divisors. 2 | 3 | num = int(input()) 4 | sum1 = 0 5 | 6 | # Calculating the sum of divisors 7 | for i in range(1, num): 8 | if (num % i == 0): 9 | sum1 = sum1 + i 10 | 11 | if (sum1 == num): 12 | print("It is a perfect number") 13 | else: 14 | print("It is not a perfect number") 15 | 16 | # INPUT: 28 17 | # OUTPUT: It is a perfect number 18 | -------------------------------------------------------------------------------- /Stacks_Using_Arrays/Stacks_Using_Arrays.js: -------------------------------------------------------------------------------- 1 | class stack{ 2 | constructor() 3 | { 4 | this.array=[]; 5 | } 6 | peek() 7 | { 8 | return this.array[this.array.length-1]; 9 | } 10 | push(value) 11 | { 12 | this.array.push(value); 13 | return this; 14 | } 15 | pop() 16 | { 17 | this.array.pop(); 18 | return this; 19 | } 20 | } 21 | const mystack=new stack(); 22 | mystack.push(1); 23 | mystack.push(2); 24 | mystack.peek(); 25 | mystack.pop(); 26 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // function for Bubble Sort 6 | func BubbleSort(array[] int)[]int { 7 | for i:=0; i< len(array)-1; i++ { 8 | for j:=0; j < len(array)-i-1; j++ { 9 | if (array[j] > array[j+1]) { 10 | array[j], array[j+1] = array[j+1], array[j] 11 | } 12 | } 13 | } 14 | return array 15 | } 16 | 17 | // Driver Function 18 | func main() { 19 | array:= []int{2, 4, 3, 1, 6, 8, 4}; 20 | fmt.Println(BubbleSort(array)) 21 | } 22 | -------------------------------------------------------------------------------- /Birthday_Paradox/Birthday_Paradox.java: -------------------------------------------------------------------------------- 1 | //Java program to approximate number of people in Birthday Paradox problem 2 | class Birthday_Paradox 3 | { 4 | 5 | static double find(double p) // Returns approximate number of people for a given probability 6 | { 7 | return Math.ceil(Math.sqrt(2*365*Math.log(1/(1-p)))); 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | System.out.println(find(0.70)); 13 | } 14 | } 15 | 16 | /* Output 17 | 30.0 18 | */ 19 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main(args: Array) { 4 | val read = Scanner(System.`in`) 5 | print("Enter the Number : ") 6 | var num = read.nextInt() 7 | var digits = 0 8 | while(num > 0) 9 | { 10 | digits++ 11 | num /= 10 12 | } 13 | println("The no. of digits in given number is : ${digits}") 14 | } 15 | 16 | /* 17 | Input : Enter the Number : 3456 18 | Output : The no. of digits in given number is : 4 19 | */ 20 | -------------------------------------------------------------------------------- /Sleep_Sort/Sleep_Sort.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | def sleep_sort(args) 4 | threads = [] 5 | mutex = Mutex.new 6 | 7 | args.each do |value| 8 | threads << Thread.new do 9 | sleep value.to_f / 1000 10 | mutex.synchronize do 11 | print value.to_s + " " 12 | end 13 | end 14 | end 15 | 16 | threads.each do |thread| 17 | thread.join 18 | end 19 | end 20 | 21 | x = [2, 4, 3, 1, 6, 8, 4] 22 | sleep_sort(x) 23 | 24 | 25 | # Output 26 | # 1 2 3 4 4 6 8 27 | -------------------------------------------------------------------------------- /Logarithmic_Exponent/Logarithmic_Exponent.coffee: -------------------------------------------------------------------------------- 1 | exponent = (a, x) -> 2 | result = 1 3 | value = a 4 | 5 | while x isnt 0 6 | if x & 1 7 | result *= value 8 | value *= value 9 | 10 | x = Math.floor(x / 2) 11 | 12 | result 13 | 14 | base = 3 15 | power = 5 16 | 17 | console.log "Value of " + base + " raise to the power " + power + " is " + exponent base, power 18 | 19 | 20 | ### Output 21 | 22 | Value of 3 raise to the power 5 is 243 23 | 24 | ### 25 | -------------------------------------------------------------------------------- /Simple_Interest/Simple_Interest.py: -------------------------------------------------------------------------------- 1 | ''' 2 | SIMPLE INTEREST 3 | 4 | Simple Interest in Accounting is calculated as : 5 | 6 | S.I. = (p * r * t) / 100 7 | where p = principal amount 8 | r = rate of interest 9 | t = time 10 | ''' 11 | 12 | p = int(input("Enter Principal: ")) 13 | r = int(input("Enter Rate: ")) 14 | t = int(input("Enter Time: ")) 15 | print("Simple Interest is:", ((p * r * t) / 100)) 16 | 17 | 18 | ''' 19 | INPUT : p = 1000, r = 8, t = 2 20 | OUTPUT : Simple Interest is: 160.00 21 | ''' 22 | -------------------------------------------------------------------------------- /Array_Rotation/Array_Rotation.py: -------------------------------------------------------------------------------- 1 | //Program to rotate array Arr[] of size n by d Elements 2 | def leftRotation(arr,n): 3 | temp = arr[0] 4 | for i in range(n-1): 5 | arr[i] = arr[i+1] 6 | arr[n-1] = temp 7 | 8 | def Rotate(arr,d,n): 9 | for i in range(d): 10 | leftRotation(arr,n) 11 | for i in range(n): 12 | print(arr[i],end=" ") 13 | 14 | arr = [1,2,3,4,5,6,7] 15 | Rotate(arr,2,7) 16 | 17 | /* 18 | Input array: {1, 2, 3, 4, 5, 6, 7 } 19 | Expected Output 20 | 3 4 5 6 7 1 2 21 | */ 22 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.ts: -------------------------------------------------------------------------------- 1 | // Euclidean Algorithm to find GCD of two numbers 2 | 3 | export {} 4 | 5 | // Recursive Function to return gcd of a and b 6 | function gcd(a:number , b:number) : number 7 | { 8 | return (b === 0) ? a : gcd(b, a % b); 9 | } 10 | 11 | let x : number = 24; 12 | let y : number = 60; 13 | let ans : number = gcd(x,y); 14 | console.log("The GCD of " + x + " and " + y + " is " + ans); 15 | 16 | /* 17 | INPUT 18 | x = 24 19 | y = 60 20 | 21 | OUTPUT 22 | The GCD of 24 and 60 is 12 23 | */ -------------------------------------------------------------------------------- /Josephus_Problem/Josephus_Problem.py: -------------------------------------------------------------------------------- 1 | #Josephus Problem 2 | 3 | #Total number of people=N 4 | #Number of people to be skipped=k 5 | 6 | N=int(input("\nEnter the total number of people:")) 7 | k=int(input("\nEnter the number of people to be skipped:")) 8 | 9 | #Recursive Problem with time complexity O(n) 10 | def josephus(n,m): 11 | if(n==1): 12 | return 0 13 | else: 14 | n1=int(n)-1 15 | return (josephus(n1,m)+m)%n 16 | 17 | print("\nThe solution of the Problem is:",josephus(N,k)+1) 18 | -------------------------------------------------------------------------------- /Selection_Sort/Selection_Sort.js: -------------------------------------------------------------------------------- 1 | function Selection_Sort(array){ 2 | for( var i = 0; i < array.length; i++ ){ 3 | var small = i; 4 | for( var j = i + 1; j < array.length; j++ ){ 5 | if( array[j] < array[small]){ 6 | small = j; 7 | } 8 | } 9 | if(i !== small){ 10 | var temp = array[i]; 11 | array[i] = array[small]; 12 | array[small] = temp; 13 | } 14 | } 15 | return array; 16 | } 17 | 18 | array = [4, 3, 6, 9, 5, 1, 8, 7, 2]; 19 | console.log(Selection_Sort(array)); 20 | -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main(args: Array) { 4 | val read = Scanner(System.`in`) 5 | print("Enter the Number : ") 6 | var num = read.nextInt() 7 | var sum = 0 8 | while(num > 0) 9 | { 10 | var a = num % 10 11 | sum += a 12 | num /= 10 13 | } 14 | println("The sum of digits of given number is : ${sum}") 15 | } 16 | 17 | /* 18 | Input : Enter the Number : 223 19 | Output : The sum of digits of given number is : 7 20 | */ 21 | -------------------------------------------------------------------------------- /Sleep_Sort/sleep_sort.erl: -------------------------------------------------------------------------------- 1 | -module(sleep_sort). 2 | -compile(export_all). 3 | 4 | sorter(Num) -> 5 | timer:sleep(Num * 1000), 6 | io:format("~p~n", [Num]). 7 | 8 | sort(Num_list) -> 9 | lists:foreach(fun (N) -> spawn_link(?MODULE, sorter, [N]) end, Num_list). 10 | 11 | 12 | % Input: 13 | %Eshell V5.10.4 (abort with ^G) 14 | %1> c(sleep_sort). 15 | %{ok,sleep_sort} 16 | %2> sleep_sort:sort([2,35,56,32,44,43,5,6]). 17 | 18 | % Output: 19 | %ok 20 | %2 21 | %5 22 | %6 23 | %32 24 | %35 25 | %43 26 | %44 27 | %56 28 | %3> 29 | -------------------------------------------------------------------------------- /Activity_Selection/Activity_Selection.rb: -------------------------------------------------------------------------------- 1 | start = [1,3,1,5,8,6] 2 | finish = [2,6,6,7,10,8] 3 | 4 | n = start.length() 5 | def print_activities(start , finish , n) 6 | print "Following activities are selected \n " 7 | i = 0 8 | print (i.to_s + "\t") 9 | for j in 1..n-1 10 | if start[j]>finish[i] 11 | print (j.to_s + "\t") 12 | i = j 13 | end 14 | end 15 | end 16 | 17 | print_activities(start, finish , n) 18 | 19 | =begin 20 | input = [1,3,1,5,8,6] 21 | Following activities are selected 22 | 0 1 4 23 | =end 24 | -------------------------------------------------------------------------------- /Shell_Sort/Shell_Sort.coffee: -------------------------------------------------------------------------------- 1 | # Function for selection sort 2 | Selection_Sort = (array) -> 3 | for i in [0...array.length - 1] 4 | min_index = i 5 | 6 | for j in [i + 1...array.length] 7 | if array[j] < array[min_index] 8 | min_index = j 9 | 10 | [array[i], array[min_index]] = [array[min_index], array[i]] 11 | array 12 | 13 | array = [2, 4, 3, 1, 6, 8, 4] 14 | 15 | console.log Selection_Sort array 16 | 17 | 18 | ### Output 19 | 20 | [ 1, 2, 3, 4, 4, 6, 8 ] 21 | 22 | ### 23 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | import java.lang.*; 4 | import java.io.*; 5 | 6 | 7 | class Euclid 8 | { 9 | public static void main (String[] args) throws java.lang.Exception 10 | { 11 | int a = 30; 12 | int b = 105; 13 | int c = EuclidGCD(a,b); 14 | System.out.println(c); 15 | // prints 15 16 | // GCD of 30 and 105 is 15 17 | } 18 | 19 | public static int EuclidGCD(int a, int b) 20 | { 21 | if(b == 0) 22 | return a; 23 | else 24 | return EuclidGCD(b,a%b); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Natural_Numbers_Sum/Natural_Numbers_Sum.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Sum of First N Natural Numbers 3 | 4 | The task is to find sum of first n natural numbers, where n is 5 | the number input to us. We use the well known formula, 6 | sum = n * (n + 1)/2 to compute the sum 7 | 8 | ''' 9 | 10 | print("Enter N: ") 11 | num = int(input()) 12 | ans = num * (num + 1) / 2 13 | print("The sum of first", num, "natural numbers is :", int(ans)) 14 | 15 | ''' 16 | Input : num = 6 17 | Output : The sum of first 6 natural numbers is : 21 18 | ''' 19 | -------------------------------------------------------------------------------- /Number_Conversion/C/Decimal_To_Octal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num; 6 | scanf("%d", &num); 7 | 8 | // To store octal number 9 | int octal[100]; 10 | 11 | int i = 0, j; 12 | while (num != 0) 13 | { 14 | octal[i] = num % 8; 15 | num = num / 8; 16 | i++; 17 | } 18 | 19 | for (j = i - 1; j >= 0; j--) 20 | printf("%d", octal[j]); 21 | 22 | return 0; 23 | } 24 | 25 | /* 26 | Input: 27 | 20 28 | Output: 29 | 24 30 | */ 31 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.py: -------------------------------------------------------------------------------- 1 | ''' 2 | The sum of cubes of first N natural numbers is given 3 | by the equation (((N) * (N + 1)) / 2 ) ^ 2 i.e. 4 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3.........N^3 sums upto 5 | (((N) * (N + 1)) / 2 ) ^ 2 6 | ''' 7 | 8 | print("Enter the value of N: ") 9 | N = int(input()) 10 | ans = pow(((N * (N + 1)) / 2 ), 2) 11 | print(ans) 12 | 13 | ''' 14 | Input: 15 | N = 5 16 | 17 | Output: 18 | ans = 225 19 | 20 | Verification: 21 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3 22 | = 1 + 8 + 27 + 64 + 125 23 | = 225 24 | ''' 25 | -------------------------------------------------------------------------------- /Logarithmic_Exponent/Logarithmic_Exponent.py: -------------------------------------------------------------------------------- 1 | def exponent(a, x): 2 | result = 1 3 | value = a 4 | while x != 0: 5 | if x & 1: 6 | result *= value 7 | value *= value 8 | x = int(x / 2) 9 | return result 10 | 11 | base = int(input()) 12 | power = int(input()) 13 | print("Value of " + str(base) + " raise to the power " + str(power) + " is " + str(exponent(base, power))) 14 | 15 | ''' 16 | Input : 17 | base = 2, power = 32 18 | Output: 19 | Value of 2 raised to the power 32 is 4294967296 20 | ''' 21 | -------------------------------------------------------------------------------- /Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.coffee: -------------------------------------------------------------------------------- 1 | gcdFunction = (a, b, x, y) -> 2 | if a is 0 3 | x = 0 4 | y = 1 5 | return b 6 | 7 | x1 = 0 8 | y1 = 0 9 | gcd = gcdFunction b % a, a, x1, y1 10 | 11 | x = y1 - Math.floor (b / a) * x1 12 | y = x1 13 | 14 | gcd 15 | 16 | a = 98 17 | b = 21 18 | x = 0 19 | y = 0 20 | 21 | console.log "GCD of numbers " + a + " and " + b + " is " + gcdFunction a, b, x, y 22 | 23 | 24 | ### Output 25 | 26 | GCD of numbers 98 and 21 is 7 27 | 28 | ### 29 | -------------------------------------------------------------------------------- /Number_Conversion/C++/Decimal_To_Octal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num; 7 | cin>>num; 8 | 9 | // To store octal number 10 | int octal[100]; 11 | 12 | int i = 0, j; 13 | while (num != 0) 14 | { 15 | octal[i] = num % 8; 16 | num = num / 8; 17 | i++; 18 | } 19 | 20 | for (j = i - 1; j >= 0; j--) 21 | cout< 0) { 8 | if(num % 10 != sqre % 10) { 9 | count++ 10 | } 11 | num /= 10 12 | sqre /= 10 13 | } 14 | if(count >= 1) { 15 | println("Not an Automorpic Number") 16 | } 17 | else { 18 | println("Automorpic Number") 19 | } 20 | } 21 | 22 | //Output:- Automorphic Number 23 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_Of_Squares.py: -------------------------------------------------------------------------------- 1 | ''' 2 | The sum of squares of first N natural numbers is given 3 | by the equation ((N) * (N + 1) * (2 * N + 1)) / 6 i.e. 4 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2.........N^2 sums upto 5 | ((N) * (N + 1) * (2 * N + 1)) / 6 6 | ''' 7 | 8 | print("Enter the value of N: ") 9 | N = int(input()) 10 | ans = ((N) * (N + 1) * (2 * N + 1)) / 6 11 | print(ans) 12 | 13 | ''' 14 | Input: 15 | N = 5 16 | 17 | Output: 18 | ans = 55 19 | 20 | Verification: 21 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2 22 | = 1 + 4 + 9 + 16 + 25 23 | = 55 24 | ''' 25 | -------------------------------------------------------------------------------- /Kadane_Algorithm/Kadane_Algorithm.rb: -------------------------------------------------------------------------------- 1 | #Kadane's algorithm with Ruby 2 | 3 | def kadane(array) 4 | current_max = max_so_far = array[0] 5 | 6 | for i in 1 .. array.length - 1 7 | current_max += array[i] 8 | if current_max < 0 9 | current_max = 0 10 | end 11 | if max_so_far < current_max 12 | max_so_far = current_max 13 | end 14 | end 15 | 16 | return max_so_far 17 | end 18 | 19 | array = [-2, 1, -6, 4, -1, 2, 1, -5, 4] 20 | 21 | max_subarray_sum = kadane(array) 22 | 23 | print(max_subarray_sum) 24 | 25 | # Output 26 | # 6 27 | -------------------------------------------------------------------------------- /Naive_String_Matching/Naive_Approach.rb: -------------------------------------------------------------------------------- 1 | def search (text, pattern) 2 | for i in (0 .. text.length - pattern.length) 3 | j = 0 4 | while j < pattern.length 5 | if text[i+j] != pattern[j] 6 | break 7 | end 8 | j += 1 9 | end 10 | if j == pattern.length 11 | puts("Pattern found at " + (i+1).to_s) 12 | end 13 | end 14 | end 15 | 16 | text = "random variables are so random" 17 | pattern = "ran" 18 | search(text,pattern) 19 | 20 | # Output 21 | # Pattern found at 1 22 | # Pattern found at 25 23 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.rb: -------------------------------------------------------------------------------- 1 | 2 | def bubbleSort (array, size) 3 | array.each do 4 | for j in 0 .. (size-2) 5 | if array[j] > array[j+1] 6 | temp = array[j] 7 | array[j] = array[j+1] 8 | array[j+1] = temp 9 | end 10 | end 11 | end 12 | array 13 | end 14 | 15 | 16 | array = [0,9,1,4,2] 17 | 18 | print(array) 19 | 20 | bubbleSort(array, array.length) 21 | 22 | puts("\nAfter sorting: ") 23 | 24 | print(array) 25 | 26 | #Output 27 | # [0, 9, 1, 4, 2] 28 | # After Sorting: 29 | # [0, 1, 2, 4, 9] 30 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | DIGIT COUNT OF A NUMBER 3 | 4 | The task is to count the number of digits present in the given 5 | number. 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int getDigits(int n) { 12 | int ans = 0; 13 | while (n) { 14 | ans ++; 15 | n /= 10; 16 | } 17 | return ans; 18 | } 19 | 20 | int main() { 21 | cout<<"Enter N: "; 22 | int n; 23 | cin>>n; 24 | cout<<"Number of digits is : "< 9 | #include 10 | 11 | int getDigits(int n) { 12 | int ans = 0; 13 | while (n) { 14 | ans ++; 15 | n /= 10; 16 | } 17 | return ans; 18 | } 19 | 20 | int main() { 21 | printf("Enter N: "); 22 | int n; 23 | scanf("%d", &n); 24 | printf("Number of digits is : %d", getDigits(n)); 25 | } 26 | 27 | /* 28 | INPUT : n = 6534 29 | OUTPUT: Number of digits is : 4 30 | */ 31 | -------------------------------------------------------------------------------- /Gcd/gcd.py: -------------------------------------------------------------------------------- 1 | # Python Implementation of GCD Program using Euclidean Algorithm 2 | def gcd(m, n): 3 | # Assume m >= n 4 | if m < n: 5 | (m, n)=(n, m) # in case n>m then we will swap and create m>n 6 | if(m % n) == 0: 7 | return n 8 | else: 9 | return(gcd(n, m % n)) 10 | 11 | # Main Program 12 | if __name__ == '__main__': 13 | n1 = int(input("Enter Number 1 ")) 14 | n2 = int(input("Enter Number 2 ")) 15 | print(f"Gcd of {n1}, {n2} = {gcd(n1, n2)}") 16 | 17 | # Output 18 | # Enter number 1 14 19 | # Enter number 2 63 20 | # Gcd of 14, 63 = 7 21 | -------------------------------------------------------------------------------- /Nonagonal_Number/Nonagonal_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | NONAGONAL NUMBER 3 | 4 | These numbers define the number of dots(or, verices), when a few nonagons 5 | (9-sided polygons) sharing a common corner are placed together. 6 | 7 | The nth Nonagonal Number has the formula : n * (7 * n - 5) / 2 8 | ''' 9 | 10 | def nonagon(num): 11 | return (num * ((7 * num) - 5)) // 2 12 | 13 | num = int(input()) 14 | print("The Nonagonal Number at position" , num , "is:" , nonagon(num)) 15 | 16 | ''' 17 | Input : num = 5 18 | Output : The Nonagonal Number at position 5 is: 75 19 | ''' 20 | -------------------------------------------------------------------------------- /Compound_Interest/Compound_Interest.c: -------------------------------------------------------------------------------- 1 | /* 2 | Compound Interest = P(1 + R/100)^T 3 | 4 | P is principle amount 5 | R is the rate and 6 | T is the time span 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | float p, r, t; 14 | printf("Enter Principal: "); 15 | scanf("%f", &p); 16 | printf("Enter Rate: "); 17 | scanf("%f", &r); 18 | printf("Enter Time: "); 19 | scanf("%f", &t); 20 | printf("%0.2f", p * (pow((1 + r / 100), t))); 21 | } 22 | 23 | /* 24 | INPUT : p = 1000, r = 8, t = 2 25 | OUTPUT : 1166.40 26 | */ 27 | -------------------------------------------------------------------------------- /Next_Perfect_Square/Next_Perfect_Square.py: -------------------------------------------------------------------------------- 1 | ''' 2 | NEXT PERFECT SQUARE 3 | 4 | A perfect square is a number which can be expressed as a product of 5 | two equal numbers. The task is to find the first such number greater 6 | than the given number. 7 | ''' 8 | 9 | import math 10 | 11 | n = int(input("Enter N: ")) 12 | x = math.floor(math.sqrt(n)) + 1 13 | print("The next perfect square is ", x * x) 14 | 15 | ''' 16 | INPUT : n = 1091 17 | OUTPUT: The next perfect square is 1156 18 | VERIFICATION : 1156 = 34 * 34 19 | Hence it is a perfect square. 20 | ''' 21 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.jl: -------------------------------------------------------------------------------- 1 | function bubble_sort(x) #function declaration 2 | for i in range(1,length(x)-1) #outer for loop 3 | for j in range(1,length(x)-1) #inner for loop 4 | if x[j]>x[j+1] #comparison to test acesending 5 | t=x[j] #exchange of variable 6 | x[j]=x[j+1] # ... 7 | x[j+1]=t #using a second one 8 | end 9 | end 10 | end 11 | for i in range(1,length(x)) #loop for printing 12 | println(x[i]) 13 | end 14 | end 15 | 16 | 17 | x = [1,4,6,8,7,9,5,6] 18 | bubble_sort(x) 19 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.py: -------------------------------------------------------------------------------- 1 | # function for bubble sort 2 | def Bubble_Sort(list): 3 | for i in range(0, len(list) - 1): 4 | for j in range(0, len(list) - i - 1): 5 | # do swapping 6 | if list[j] > list[j + 1]: 7 | list[j], list[j + 1] = list[j + 1], list[j] 8 | 9 | # function to print list 10 | def Print_list(list): 11 | for i in range(0, len(list)): 12 | print(list[i], end = " ") 13 | 14 | print() 15 | 16 | list = [2, 4, 3, 1, 6, 8, 4] 17 | 18 | Bubble_Sort(list) 19 | 20 | Print_list(list) 21 | 22 | # Output 23 | # 1 2 3 4 4 6 8 24 | -------------------------------------------------------------------------------- /Compound_Interest/Compound_Interest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Compound Interest = P(1 + R/100)^T 3 | 4 | P is principle amount 5 | R is the rate and 6 | T is the time span 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | float p, r, t; 15 | cout<<"Enter Principal: "; 16 | cin>>p; 17 | cout<<"Enter Rate: "; 18 | cin>>r; 19 | cout<<"Enter Time: "; 20 | cin>>t; 21 | cout<

2 | using namespace std; 3 | 4 | int isprm(long long int n) 5 | { 6 | long long int i=5, w=2; 7 | if(n==2 || n== 3) 8 | return 1; 9 | if(n%2==0 || n%3==0 || n==1) 10 | return 0; 11 | while(i*i<= n) 12 | { 13 | if(n%i ==0) 14 | return 0; 15 | i +=w; 16 | w=6-w; 17 | } 18 | return 1; 19 | } 20 | 21 | int main() 22 | { 23 | int i=0; 24 | for(int i; i<1000000;i++) 25 | { 26 | if(isprm(i)==1) 27 | printf("%d\n",i); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.go: -------------------------------------------------------------------------------- 1 | /* 2 | DIGIT COUNT OF A NUMBER 3 | 4 | The task is to count the number of digits present in the given 5 | number. 6 | */ 7 | 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | ) 13 | 14 | func getDigits(n int) int{ 15 | var ans int = 0 16 | for n > 0 { 17 | ans = ans + 1 18 | n /= 10 19 | } 20 | return ans 21 | } 22 | 23 | func main() { 24 | fmt.Print("Enter N: ") 25 | var n int 26 | fmt.Scan(&n) 27 | fmt.Print("Number of digits is : ", getDigits(n)) 28 | } 29 | 30 | /* 31 | INPUT : n = 6534 32 | OUTPUT: Number of digits is : 4 33 | */ 34 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.js: -------------------------------------------------------------------------------- 1 | function Bubble_Sort(array) 2 | { 3 | var temp; 4 | 5 | for (var i = 0; i < array.length; i++) 6 | { 7 | for (var j = 0; j < array.length - i; j++) 8 | { 9 | if (array[j] > array[j + 1]) 10 | { 11 | temp = array[j]; 12 | array[j] = array[j + 1]; 13 | array[j + 1] = temp; 14 | } 15 | } 16 | } 17 | } 18 | 19 | 20 | array = [2, 4, 3, 1, 6, 8, 4]; 21 | 22 | Bubble_Sort(array); 23 | console.log(array); 24 | 25 | 26 | /* Output 27 | 28 | [ 1, 2, 3, 4, 4, 6, 8 ] 29 | 30 | */ 31 | -------------------------------------------------------------------------------- /Decagonal_Numbers/Decagonal_Numbers.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A decagonal number is a figurate number that extends the concept of 3 | triangular and square numbers to the decagon (a ten-sided polygon). 4 | 5 | The n-th decagonal number is given by the formula: 6 | Dn = 4n^2 - 3n. 7 | ''' 8 | 9 | # To calculate decagonal number 10 | def decagonalnumber (num): 11 | # Using formula 12 | return 4 * num * num - 3 * num 13 | 14 | # Driver code 15 | num = int(input()) 16 | print(num, "decagonal number :", decagonalnumber(num)) 17 | 18 | ''' 19 | Input: 20 | 6 21 | output: 22 | 6 decagonal number : 126 23 | ''' 24 | -------------------------------------------------------------------------------- /Compound_Interest/Compound_Interest.go: -------------------------------------------------------------------------------- 1 | /* 2 | Compound Interest = P(1 + R/100)^T 3 | 4 | P is principle amount 5 | R is the rate and 6 | T is the time span 7 | */ 8 | 9 | package main 10 | 11 | import( 12 | "fmt" 13 | "math" 14 | ) 15 | 16 | func main() { 17 | var p, r, t float64 18 | fmt.Print("Enter Principal: ") 19 | fmt.Scan(&p) 20 | fmt.Print("Enter Rate: ") 21 | fmt.Scan(&r) 22 | fmt.Print("Enter Time: ") 23 | fmt.Scan(&t) 24 | fmt.Print(p * (math.Pow((1 + r / 100), t))) 25 | } 26 | 27 | /* 28 | INPUT : p = 1000, r = 8, t = 2 29 | OUTPUT : 1166.40 30 | */ 31 | -------------------------------------------------------------------------------- /Natural_Numbers_Sum/Natural_Numbers_Sum.c: -------------------------------------------------------------------------------- 1 | /* 2 | Sum of First N Natural Numbers 3 | 4 | The task is to find sum of first n natural numbers, where n is 5 | the number input to us. We use the well known formula, 6 | sum = n * (n + 1)/2 to compute the sum 7 | 8 | */ 9 | 10 | #include 11 | 12 | int main() { 13 | int num; 14 | printf("Enter N: "); 15 | scanf("%d", &num); 16 | int sum = num * (num + 1) / 2; 17 | printf("The sum of first %d natural numbers is : %d", num, sum); 18 | } 19 | 20 | /* 21 | Input : num = 6 22 | Output : The sum of first 6 natural numbers is : 21 23 | */ 24 | -------------------------------------------------------------------------------- /Babylonian_Method/Babylonian_Method.kt: -------------------------------------------------------------------------------- 1 | import java.util.Scanner 2 | 3 | fun square_root(num: Float): Float { 4 | var number = num 5 | var guess = 1F 6 | var accuracy = 0.000001 7 | while (number - guess > accuracy) { 8 | number = (number + guess) / 2 9 | guess = num / number 10 | } 11 | return number 12 | } 13 | 14 | fun main() { 15 | // Taking Input from user. 16 | val scanner = Scanner(System.`in`) 17 | var num = scanner.nextInt() 18 | println("Square Root of $num is ${square_root(num.toFloat())}") 19 | } 20 | 21 | // Input :- 90 22 | // Output: Square Root of 90 is 9.486834 23 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.py: -------------------------------------------------------------------------------- 1 | # Function for Linear Search 2 | def linearSearch(list, desired): 3 | for i in range(0, len(list)): 4 | # return positon if found 5 | if(list[i] == desired): 6 | return i 7 | 8 | return -1 9 | 10 | num = int(input()) 11 | list = [] 12 | for i in range(0, num): 13 | list.append(int(input())) 14 | desired = int(input()) 15 | if(linearSearch(list,desired) != -1): 16 | print("Found") 17 | else: 18 | print("Number not found") 19 | 20 | 21 | ''' 22 | Input : 23 | num = 5 24 | arr = [1,4,5,6,3] 25 | desired = 3 26 | 27 | Output : 28 | Number not found 29 | ''' 30 | -------------------------------------------------------------------------------- /Rotation_Of_Numbers/Rotation_Of_Numbers.py: -------------------------------------------------------------------------------- 1 | 2 | # Function to rotations of a number 3 | def rotation (num): 4 | # Finding the number of digits 5 | digit = 0 6 | temp = num 7 | while temp > 0: 8 | digit += 1 9 | temp //= 10 10 | 11 | power = pow(10, digit - 1) 12 | 13 | for i in range(digit - 1): 14 | lastDigit = num % 10 15 | x = num // 10 16 | num = lastDigit * power + x 17 | print(num, end = " ") 18 | 19 | # Driver code 20 | num = int(input()) 21 | rotation(num) 22 | 23 | ''' 24 | Input: 25 | 1234 26 | Output: 27 | 4123 3412 2341 28 | ''' 29 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.rb: -------------------------------------------------------------------------------- 1 | # Given two numbers, we need to find the greatest common divisor of those two numbers 2 | 3 | def gcd(x, y) 4 | # every number divides 0 5 | if (x == 0) 6 | return y 7 | else 8 | return gcd(y % x, x) 9 | end 10 | end 11 | 12 | # Driver Code 13 | puts "Enter first number:" 14 | x = gets.to_i 15 | 16 | puts "Enter second number:" 17 | y = gets.to_i 18 | 19 | puts "GCD of numbers #{x} and #{y} is #{gcd(x, y)}" 20 | 21 | =begin 22 | Enter first number: 23 | 462 24 | 25 | Enter second number: 26 | 780 27 | 28 | Output: 29 | GCD of numbers 462 and 780 is 6 30 | =end 31 | -------------------------------------------------------------------------------- /Factorial/Factorial.java: -------------------------------------------------------------------------------- 1 | /* This code is used to find factorial of large numbers*/ 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.math.*; 6 | class Factorial 7 | { 8 | public static void main(String[] args) throws java.lang.Exception 9 | { 10 | Scanner s = new Scanner(System.in); 11 | int n = s.nextInt(); 12 | BigInteger f = new BigInteger("1"); 13 | for (int j = 1; j <= n; j++) 14 | { 15 | f = f.multiply(BigInteger.valueOf(j)); 16 | } 17 | System.out.println(f); 18 | } 19 | } 20 | 21 | /*ENTER THE NUMBER 22 | 5 23 | Answer is : 120*/ 24 | -------------------------------------------------------------------------------- /Natural_Numbers_Sum/Natural_Numbers_Sum.go: -------------------------------------------------------------------------------- 1 | /* 2 | Sum of First N Natural Numbers 3 | 4 | The task is to find sum of first n natural numbers, where n is 5 | the number input to us. We use the well known formula, 6 | sum = n * (n + 1)/2 to compute the sum 7 | 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | ) 15 | 16 | func main() { 17 | var num int 18 | fmt.Scan(&num) 19 | sum := num * (num + 1) / 2 20 | fmt.Print("The sum of first ", num, " natural numbers is : ", sum) 21 | } 22 | 23 | /* 24 | Input : num = 6 25 | Output : The sum of first 6 natural numbers is : 21 26 | */ 27 | -------------------------------------------------------------------------------- /Tower_Of_Hanoi/Tower_Of_Hanoi.js: -------------------------------------------------------------------------------- 1 | //Tower of Hanoi problem using recursion 2 | 3 | function TowerOfHanoi(num, source, destination, auxillary){ 4 | if(num === 1){ 5 | console.log(`Move disk 1 from rod ${source} to ${destination}`); 6 | return 0; 7 | } 8 | 9 | TowerOfHanoi(num-1, source, auxillary, destination); 10 | console.log(`Move disk ${num} from rod ${source} to ${destination}`); 11 | TowerOfHanoi(num-1, auxillary, destination, source); 12 | } 13 | 14 | var input = 3; 15 | TowerOfHanoi (input, 'A', 'C', 'B'); //A, C, B are the name of rods 16 | // A is source rod, B is auxillary rod, C is destination rod 17 | -------------------------------------------------------------------------------- /Tower_Of_Hanoi/Tower_Of_Hanoi.py: -------------------------------------------------------------------------------- 1 | # Tower of Hanoi problem using recursion 2 | 3 | def TowerOfHanoi(num, source, destination, auxillary): 4 | if num == 1: 5 | print("Move disk 1 from rod", source, "to rod", destination) 6 | return 7 | TowerOfHanoi(num - 1, source, auxillary, destination) 8 | print("Move disk", num, "from rod", source, "to rod", destination) 9 | TowerOfHanoi(num - 1, auxillary, destination, source) 10 | 11 | # Main code 12 | num = int(input("Enter the number")) 13 | TowerOfHanoi(num, 'A', 'C', 'B') 14 | # A, C, B are the name of rods 15 | # A is source rod, B is auxillary rod, C is destination rod 16 | -------------------------------------------------------------------------------- /1-D_Array/Python/Frequency_Of_A_Element.py: -------------------------------------------------------------------------------- 1 | # Python program to find the frequency of an element 2 | 3 | num = int(input("Enter number of elements in the array")) 4 | arr = [] 5 | f = 0 6 | for i in range(0, num): 7 | arr.insert(i, int(input())) 8 | 9 | #Finding frequency of an element 10 | n = int(input("Enter the number whose occurence is to be found: ")) 11 | for i in arr: 12 | if i == n: 13 | f = f + 1 14 | print("Frequency is ") 15 | print(f) 16 | 17 | ''' 18 | 19 | Enter number of elements in the array5 20 | 1 21 | 1 22 | 2 23 | 2 24 | 3 25 | Enter the number whose occurence is to be found: 3 26 | Frequency is 27 | 1 28 | ''' 29 | -------------------------------------------------------------------------------- /Gcd/gcd.js: -------------------------------------------------------------------------------- 1 | /* 2 | JS Implementation of GCD Program using Euclidean Algorithm 3 | */ 4 | 5 | function gcd(m, n) 6 | { 7 | if(m < n) 8 | { 9 | var temp = m; 10 | m = n; 11 | n = temp; 12 | } 13 | if(m % n === 0) 14 | { 15 | return n; 16 | } 17 | else 18 | { 19 | return gcd(n, m % n); 20 | } 21 | } 22 | 23 | var n1 = prompt("Enter number 1:"); 24 | var n2 = prompt("Enter number 2:"); 25 | console.log("GCD of " + n1 + " and " + n2 + " is " + gcd(n1, n2)); 26 | 27 | /* 28 | Input: 29 | Enter number 1: 14 30 | Enter number 2: 63 31 | Output: 32 | GCD of 14 and 63 is 7 33 | */ 34 | -------------------------------------------------------------------------------- /Inheritance(Java)/Single_Level.java: -------------------------------------------------------------------------------- 1 | class Rectangle 2 | { 3 | float length; 4 | float breadth; 5 | 6 | Rectangle() 7 | { 8 | length = 5; 9 | breadth = 6; 10 | } 11 | } 12 | 13 | class Area extends Rectangle 14 | { 15 | public float calculate() 16 | { 17 | return length * breadth; 18 | } 19 | } 20 | 21 | class Single_Level 22 | { 23 | public static void main(String args[]) 24 | { 25 | Area a = new Area(); 26 | 27 | System.out.println("Area = " + a.calculate() + " square meters"); 28 | } 29 | } 30 | 31 | /* Output 32 | 33 | Area = 30 square meters 34 | 35 | */ 36 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.php: -------------------------------------------------------------------------------- 1 | " ; 20 | else 21 | echo $val1." Not Found
"; 22 | 23 | if(linear_search($list,$val2,count($list)) != -1) 24 | echo $val2." Found
" ; 25 | else 26 | echo $val2." Not Found
"; 27 | 28 | ?> 29 | -------------------------------------------------------------------------------- /Prefix_Sum/Prefix_Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | vector prefix_sum(vector v) 5 | { 6 | int n=(int)v.size(); 7 | for(int i=1; i> n; 17 | vector arr; 18 | for (int i = 0; i < n; i++) { 19 | int c; 20 | cin >> c. 21 | arr.push_back(c); 22 | } 23 | vector v=prefix_sum(arr); 24 | cout<<"The prefix sum array for the given array is :\n"; 25 | for(auto x:v) 26 | cout< 13 | using namespace std; 14 | 15 | int main() { 16 | float p, r, t; 17 | cout<<"Enter Principal: "; 18 | cin>>p; 19 | cout<<"Enter Rate: "; 20 | cin>>r; 21 | cout<<"Enter Time: "; 22 | cin>>t; 23 | cout<<"Simple Interest is: "<<((p * r * t) / 100); 24 | } 25 | 26 | /* 27 | INPUT : p = 1000, r = 8, t = 2 28 | OUTPUT : Simple Interest is: 160.00 29 | */ 30 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of cubes of first N natural numbers is given 3 | by the equation (N * (N + 1) / 2) ^ 2 i.e. 4 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3.........N^3 sums upto 5 | (N * (N + 1) / 2) ^ 2 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int N; 14 | cout<<"Enter the value of N: "<>N; 16 | int ans = pow((N * (N + 1) / 2), 2); 17 | cout< 13 | 14 | int main() { 15 | float p, r, t; 16 | printf("Enter Principal: "); 17 | scanf("%f", &p); 18 | printf("Enter Rate: "); 19 | scanf("%f", &r); 20 | printf("Enter Time: "); 21 | scanf("%f", &t); 22 | printf("Simple Interest is: %0.2f", (p * r * t) / 100); 23 | } 24 | 25 | /* 26 | INPUT : p = 1000, r = 8, t = 2 27 | OUTPUT : Simple Interest is: 160.00 28 | */ 29 | -------------------------------------------------------------------------------- /Tree_Inorder_Traversal/Tree_Inorder_Traversal.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type node struct { 8 | data int 9 | left *node 10 | right *node 11 | } 12 | 13 | func newNode(i int) *node { 14 | return &node{ 15 | data: i, 16 | } 17 | } 18 | 19 | func (n *node) printInorder() { 20 | if n == nil { 21 | return 22 | } 23 | 24 | n.left.printInorder() 25 | fmt.Printf("%d ", n.data) 26 | n.right.printInorder() 27 | } 28 | 29 | func main() { 30 | root := newNode(5) 31 | root.left = newNode(2) 32 | root.right = newNode(3) 33 | root.left.left = newNode(4) 34 | root.left.right = newNode(5) 35 | root.printInorder() 36 | } 37 | -------------------------------------------------------------------------------- /Natural_Numbers_Sum/Natural_Numbers_Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Sum of First N Natural Numbers 3 | 4 | The task is to find sum of first n natural numbers, where n is 5 | the number input to us. We use the well known formula, 6 | sum = n * (n + 1)/2 to compute the sum 7 | 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | int num; 15 | cout << "Enter N: "; 16 | cin >> num; 17 | int sum = num * (num + 1) / 2; 18 | cout << "The sum of first " << num << " natural numbers is : " << sum; 19 | } 20 | 21 | /* 22 | Input : num = 6 23 | Output : The sum of first 6 natural numbers is : 21 24 | */ 25 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.c: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of cubes of first N natural numbers is given 3 | by the equation (N * (N + 1) / 2) ^ 2 i.e. 4 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3.........N^3 sums upto 5 | (N * (N + 1) / 2) ^ 2 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main() 12 | { 13 | int N; 14 | printf("Enter the value of N: \n"); 15 | scanf("%d", &N); 16 | int ans = pow((N * (N + 1) / 2), 2); 17 | printf("%d", ans); 18 | return 0; 19 | } 20 | 21 | /* 22 | Input: 23 | N = 5 24 | 25 | Output: 26 | ans = 225 27 | 28 | Verification: 29 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3 30 | = 1 + 8 + 27 + 64 + 125 31 | = 225 32 | */ 33 | -------------------------------------------------------------------------------- /Naive_String_Matching/Naive_Approach.coffee: -------------------------------------------------------------------------------- 1 | search = (text, pattern) -> 2 | lengthText = text.length 3 | lengthPattern = pattern.length 4 | 5 | for i in [0...lengthText - lengthPattern] 6 | j = 0 7 | 8 | while j < lengthPattern 9 | break if text[i + j] isnt pattern[j] 10 | 11 | j += 1 12 | 13 | if j is lengthPattern 14 | console.log "Pattern found at " + (i + 1) 15 | 16 | 0 # Return 0 17 | 18 | text = "namanchamanbomanamansanam" 19 | pattern = "aman" 20 | search text, pattern 21 | 22 | 23 | ### Output 24 | 25 | Pattern found at 2 26 | Pattern found at 8 27 | Pattern found at 17 28 | 29 | ### 30 | -------------------------------------------------------------------------------- /Nonagonal_Number/Nonagonal_Number.c: -------------------------------------------------------------------------------- 1 | /* 2 | NONAGONAL NUMBER 3 | 4 | These numbers define the number of dots(or, verices), when a few nonagons 5 | (9-sided polygons) sharing a common corner are placed together. 6 | 7 | The nth Nonagonal Number has the formula : n * (7 * n - 5) / 2 8 | */ 9 | 10 | #include 11 | 12 | int nonagon(int num) { 13 | return (num * ((7 * num) - 5)) / 2; 14 | } 15 | 16 | int main() { 17 | int num; 18 | scanf("%d", &num); 19 | printf("The Nonagonal Number at position %d is: %d", num, nonagon(num) ); 20 | } 21 | 22 | /* 23 | Input : num = 5 24 | Output : The Nonagonal Number at position 5 is: 75 25 | */ -------------------------------------------------------------------------------- /Perfect_Number/Perfect_Number.go: -------------------------------------------------------------------------------- 1 | // Perfect number is a positive integer that is equal to the sum of its proper divisors. 2 | 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | ) 8 | 9 | func main() { 10 | var num int 11 | fmt.Scan(&num) 12 | var sum = 0 13 | 14 | // Calculating the sum of divisors 15 | for i := 1; i < num; i++ { 16 | if (num % i == 0) { 17 | sum = sum + i 18 | } 19 | } 20 | if (sum == num) { 21 | fmt.Print("It is a perfect number") 22 | } else { 23 | fmt.Print("It is not a perfect number") 24 | } 25 | } 26 | 27 | // INPUT: 28 28 | // OUTPUT: It is a perfect number 29 | -------------------------------------------------------------------------------- /Tree_Postorder_Traversal/Tree_Postorder_Traversal.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type node struct { 8 | data int 9 | left *node 10 | right *node 11 | } 12 | 13 | func newNode(i int) *node { 14 | return &node{ 15 | data: i, 16 | } 17 | } 18 | 19 | func (n *node) PrintPostorder() { 20 | if n == nil { 21 | return 22 | } 23 | 24 | n.left.PrintPostorder() 25 | n.right.PrintPostorder() 26 | fmt.Printf("%d ", n.data) 27 | } 28 | 29 | func main() { 30 | root := newNode(1) 31 | root.left = newNode(2) 32 | root.right = newNode(3) 33 | root.left.left = newNode(4) 34 | root.left.right = newNode(5) 35 | root.PrintPostorder() 36 | } 37 | -------------------------------------------------------------------------------- /Tree_Preorder_Traversal/Tree_Preorder_Traversal.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | type node struct { 8 | data int 9 | left *node 10 | right *node 11 | } 12 | 13 | func newNode(i int) *node { 14 | return &node{ 15 | data: i, 16 | } 17 | } 18 | 19 | func (n *node) PrintPreorder() { 20 | if n == nil { 21 | return 22 | } 23 | 24 | fmt.Printf("%d ", n.data) 25 | n.left.PrintPreorder() 26 | n.right.PrintPreorder() 27 | 28 | } 29 | 30 | func main() { 31 | root := newNode(1) 32 | root.left = newNode(2) 33 | root.right = newNode(3) 34 | root.left.left = newNode(4) 35 | root.left.right = newNode(5) 36 | root.PrintPreorder() 37 | } 38 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.rb: -------------------------------------------------------------------------------- 1 | #Digit count of given number 2 | 3 | puts "Enter the number : " 4 | num = gets.chomp.to_i 5 | 6 | temp = num 7 | count = 0 8 | 9 | if temp == 0 10 | count = 1 11 | end 12 | 13 | if temp < 0 14 | temp = temp * -1 15 | end 16 | 17 | #Loop to determine the number of digits 18 | while (temp > 0) 19 | count += 1 20 | temp = temp / 10 21 | end 22 | 23 | puts "The given number #{num} has #{count} digits in it" 24 | 25 | #SAMPLE INPUT AND OUTPUT 26 | =begin 27 | #SAMPLE 1 28 | Enter the number : 1235 29 | The given number 1235 has 4 digits 30 | #SAMPLE 2 31 | Enter the number : -87 32 | The given number -87 has 2 digits 33 | =end 34 | 35 | -------------------------------------------------------------------------------- /Dynamic_Programming_Rod_Cutting/Dynamic_Programming_Rod_Cutting.js: -------------------------------------------------------------------------------- 1 | // Find max function 2 | function max(a, b) 3 | { 4 | return (a > b) ? a : b; 5 | } 6 | 7 | // Find max money function 8 | function money(arr , size ) 9 | { 10 | if(size <= 0) 11 | return 0; 12 | else 13 | { 14 | var max_val = Number.MIN_VALUE; 15 | for(var i = 0; i < size; i++) 16 | max_val = max(max_val, arr[i] + money(arr, size - i - 1)); 17 | return max_val; 18 | } 19 | } 20 | 21 | // Given money 22 | var arr = [1, 5, 8, 9, 10, 17, 17, 20]; 23 | var size = arr.length; 24 | 25 | console.log("Maximum Cost : " + money(arr, size)); 26 | 27 | 28 | // Output 29 | // Maximum Cost :22 30 | -------------------------------------------------------------------------------- /Simple_Interest/Simple_Interest.go: -------------------------------------------------------------------------------- 1 | /* 2 | SIMPLE INTEREST 3 | 4 | Simple Interest in Accounting is calculated as : 5 | 6 | S.I. = (p * r * t) / 100 7 | where p = principal amount 8 | r = rate of interest 9 | t = time 10 | */ 11 | 12 | package main 13 | 14 | import "fmt" 15 | 16 | func main() { 17 | var p, r, t float64 18 | fmt.Print("Enter Principal: ") 19 | fmt.Scan(&p) 20 | fmt.Print("Enter Rate: ") 21 | fmt.Scan(&r) 22 | fmt.Print("Enter Time: ") 23 | fmt.Scan(&t) 24 | fmt.Print("Simple Interest is: ", ((p * r * t) / 100)) 25 | } 26 | 27 | /* 28 | INPUT : p = 1000, r = 8, t = 2 29 | OUTPUT : Simple Interest is: 160.00 30 | */ 31 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_O_Squares.go: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of squares of first N natural numbers is given 3 | by the equation ((N) * (N + 1) * (2 * N + 1)) / 6 i.e. 4 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2.........N^2 sums upto 5 | ((N) * (N + 1) * (2 * N + 1)) / 6 6 | */ 7 | 8 | package main 9 | import "fmt" 10 | 11 | func main(){ 12 | var N int 13 | fmt.Printf("Enter the value of N: \n") 14 | fmt.Scanf("%d", &N) 15 | ans := ((N) * (N + 1) * (2 * N + 1)) / 6 16 | fmt.Printf("%d", ans) 17 | } 18 | 19 | /* 20 | Input: 21 | N = 5 22 | 23 | Output: 24 | ans = 55 25 | 26 | Verification: 27 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2 28 | = 1 + 4 + 9 + 16 + 25 29 | = 55 30 | */ 31 | -------------------------------------------------------------------------------- /Next_Perfect_Square/Next_Perfect_Square.c: -------------------------------------------------------------------------------- 1 | /* 2 | NEXT PERFECT SQUARE 3 | 4 | A perfect square is a number which can be expressed as a product of 5 | two equal numbers. The task is to find the first such number greater 6 | than the given number. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | float n; 14 | printf("Enter N: "); 15 | scanf("%f", &n); 16 | int x = floor(sqrt(n)) + 1; 17 | printf("The next perfect square is %d", (x * x)); 18 | } 19 | 20 | /* 21 | INPUT : n = 1091 22 | OUTPUT: The next perfect square is 1156 23 | VERIFICATION : 1156 = 34 * 34 24 | Hence it is a perfect square. 25 | */ 26 | -------------------------------------------------------------------------------- /Set_Bits_Till_N/Set_Bits_Till_N.py: -------------------------------------------------------------------------------- 1 | ''' 2 | TOTAL SET BITS FROM 1 to N 3 | 4 | Total set bits in a number n is defined as number of 1s in 5 | binary representation of the number. The task is to calculate 6 | total number of set bits in all numbers till N. 7 | ''' 8 | 9 | def setBits(n): 10 | bit = 0 11 | for i in range(0, n): 12 | bit = bit + count(i) 13 | return bit 14 | 15 | def count(n): 16 | bit = 0 17 | while (n): 18 | bit += n & 1 19 | n >>= 1 20 | return bit 21 | 22 | n = int(input()) 23 | print("The total set bits are", setBits(n)) 24 | 25 | ''' 26 | INPUT : 27 | n = 12 28 | OUTPUT : 29 | The total set bits are 20 30 | ''' 31 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_Of_Squares.c: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of squares of first N natural numbers is given 3 | by the equation ((N) * (N + 1) * (2 * N + 1)) / 6 i.e. 4 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2.........N^2 sums upto 5 | ((N) * (N + 1) * (2 * N + 1)) / 6 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int N; 13 | printf("Enter the value of N: \n"); 14 | scanf("%d", &N); 15 | int ans = ((N) * (N + 1) * (2 * N + 1)) / 6; 16 | printf("%d", ans); 17 | return 0; 18 | } 19 | 20 | /* 21 | Input: 22 | N = 5 23 | 24 | Output: 25 | ans = 55 26 | 27 | Verification: 28 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2 29 | = 1 + 4 + 9 + 16 + 25 30 | = 55 31 | */ 32 | -------------------------------------------------------------------------------- /Tribonacci_Numbers/Tribonacci_Numbers.py: -------------------------------------------------------------------------------- 1 | ''' 2 | TRIBONACCI NUMBERS 3 | 4 | It is a series of numbers where each term is the sum of preceeding 3 5 | numbers of the sequence. 6 | 7 | T(n) = T(n-1) + T(n-2) + T(n-3) 8 | ''' 9 | 10 | def tribonacci(n): 11 | x = n - 3 12 | if x < 0: 13 | return 0 14 | if not x: 15 | return 1 16 | return tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3) 17 | 18 | n = int(input("Enter N: ")) 19 | print("The tribonacci number at position", n, "is:", tribonacci(n)) 20 | 21 | ''' 22 | INPUT : n = 6 23 | OUTPUT: The tribonacci number at position 6 is: 4 24 | VERIFICATION: The tribonacci series : 0 0 1 1 2 4 25 | ''' 26 | -------------------------------------------------------------------------------- /Root_Mean_Square/Root_Mean_Square.py: -------------------------------------------------------------------------------- 1 | ''' 2 | ROOT MEAN SQUARE 3 | 4 | Root Mean Square or RMS is known as quadratic mean of a given array. 5 | It holds a lot of importance in mathematical calculations in various 6 | domains like Physics and geometry. 7 | ''' 8 | 9 | def RMS(a, n, index, ans): 10 | if (index == n): 11 | ans = ans // n 12 | print("RMS of array is :", ans) 13 | return 14 | ans = ans + a[index] * a[index] 15 | RMS(a, n, index + 1, ans) 16 | 17 | n = int(input()) 18 | a = [int(x) for x in input().split(' ')] 19 | RMS(a, n, 0, 0) 20 | 21 | ''' 22 | INPUT : n = 3 23 | a = [3, 15, 32] 24 | OUTPUT : 25 | RMS of array is : 419 26 | ''' 27 | -------------------------------------------------------------------------------- /Bitwise_Addition/bitwise_addition.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int bitwiseaddition(int a, int b) 4 | { 5 | while(b != 0) 6 | { 7 | int carry = a & b; 8 | a = a ^ b; 9 | b = carry << 1; 10 | } 11 | return a; 12 | } 13 | int main() 14 | { 15 | int no1,no2,result; 16 | printf("Enter two numbers: "); 17 | scanf("%d%d",&no1,&no2); 18 | result = bitwiseaddition(no1,no2); 19 | printf("\nusing bitwise addition,\n"); 20 | printf("\n%d + %d = %d",no1,no2,result); 21 | return 0; 22 | } 23 | 24 | 25 | /* 26 | INPUT: 27 | Enter two numbers: 12 30 28 | 29 | OUTPUT: 30 | using bitwise operations, 31 | 32 | 12+30 = 42 33 | */ 34 | -------------------------------------------------------------------------------- /Centered_Decagonal_Number/Centered_Decagonal_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | A centered decagonal number is a centered figurate number that represents 3 | a decagon with a dot in the center and all other dots surrounding the center 4 | dot in successive decagonal layers. 5 | 6 | The centered decagonal number for n is given by the formula 7 | 5n^2+5n+1 8 | ''' 9 | 10 | def centeredDecagonal (num): 11 | # Using formula 12 | return 5 * num * num + 5 * num + 1 13 | 14 | # Driver code 15 | num = int(input()) 16 | print(num, "centered decagonal number :", centeredDecagonal(num)) 17 | 18 | ''' 19 | Input: 20 | 6 21 | output: 22 | 6 centered decagonal number : 211 23 | ''' 24 | -------------------------------------------------------------------------------- /Perfect_Number/Perfect_Number.c: -------------------------------------------------------------------------------- 1 | // Perfect number is a positive integer that is equal to the sum of its proper divisors. 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int num, sum = 0, i; 8 | printf("Enter a Number: "); 9 | scanf("%d", &num); 10 | 11 | // Calculating the sum of divisors 12 | for (i = 1; i < num; i++) 13 | { 14 | if (num % i == 0) 15 | { 16 | sum = sum + i; 17 | } 18 | } 19 | if (sum == num) 20 | printf("It is a perfect number"); 21 | else 22 | printf("It is not a perfect number"); 23 | return 0; 24 | } 25 | 26 | /* 27 | Enter a Number: 28 28 | It is a perfect number */ 29 | -------------------------------------------------------------------------------- /Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.py: -------------------------------------------------------------------------------- 1 | def SieveOfEratosthenes(n): 2 | prime = [True] * (n + 1) 3 | 4 | i = 2 5 | while i**2 <= n: 6 | if prime[i] == True: 7 | for j in range(2 * i, n, i): 8 | prime[j] = False 9 | 10 | i += 1 11 | 12 | for i in range(2, n): 13 | if prime[i] == True: 14 | print(i, end = " ") 15 | 16 | print() 17 | 18 | 19 | n = 30 20 | print("Following are the prime numbers smaller than or equal to " + str(n)) 21 | SieveOfEratosthenes(n) 22 | 23 | 24 | ''' Output 25 | 26 | Following are the prime numbers smaller than or equal to 30 27 | 2 3 5 7 11 13 17 19 23 29 28 | 29 | ''' 30 | -------------------------------------------------------------------------------- /Tree_Inorder_Traversal/Tree_Inorder_Traversal.py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,key): 3 | self.left = None 4 | self.right = None 5 | self.val = key 6 | 7 | def InOrder(root): 8 | if root: 9 | InOrder(root.left) 10 | print(root.val, end = " ") 11 | InOrder(root.right) 12 | 13 | root = node(1) 14 | root.left = node(2) 15 | root.right = node(3) 16 | root.left.left = node(4) 17 | root.left.right = node(5) 18 | root.right.left = node(6) 19 | root.right.right = node(7) 20 | 21 | print("In Order traversal of tree is", end = " ") 22 | InOrder(root) 23 | 24 | ''' Output 25 | 26 | In Order traversal of tree is 4 2 5 1 6 3 7 27 | 28 | ''' 29 | -------------------------------------------------------------------------------- /Logarithmic_Exponent/Logarithmic_Exponent.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | long long exponent (int base, int power) { 4 | long long result = 1, value = base; 5 | while (power) { 6 | if (power & 1) 7 | result *= value; 8 | value *= value; 9 | power >>= 1; 10 | } 11 | return result; 12 | } 13 | 14 | int main () { 15 | int base, power; 16 | scanf("%d", &base); 17 | scanf("%d", &exponent); 18 | printf("Value of %d raised to the power %d is %lld", base, power, exponent(base, power)); 19 | return 0; 20 | } 21 | 22 | /* 23 | Input : 24 | base = 2, exponent = 32 25 | Output: 26 | Value of 2 raised to the power 32 is 4294967296 27 | */ 28 | -------------------------------------------------------------------------------- /Next_Perfect_Square/Next_Perfect_Square.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | NEXT PERFECT SQUARE 3 | 4 | A perfect square is a number which can be expressed as a product of 5 | two equal numbers. The task is to find the first such number greater 6 | than the given number. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | float n; 14 | cout << "Enter N: "; 15 | cin >> n; 16 | int x = floor(sqrt(n)) + 1; 17 | cout << "The next perfect square is " << (x * x) << endl; 18 | } 19 | 20 | /* 21 | INPUT : n = 1091 22 | OUTPUT: The next perfect square is 1156 23 | VERIFICATION : 1156 = 34 * 34 24 | Hence it is a perfect square. 25 | */ 26 | -------------------------------------------------------------------------------- /Number_Conversion/Python/Binary_To_Decimal.py: -------------------------------------------------------------------------------- 1 | ''' 2 | For binary number with n digits: 3 | dn-1 ... d3 d2 d1 d0 4 | The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n): 5 | decimal = d0×2^0 + d1×2^1 + d2×2^2 + ... 6 | ''' 7 | 8 | def binaryToDecimal(num): 9 | decimal = 0 10 | 11 | base = 1 12 | 13 | while(num): 14 | x = num % 10 15 | num = int(num / 10) 16 | 17 | decimal += x * base 18 | base = base * 2 19 | return decimal 20 | 21 | # Driver Code 22 | num = int(input()) 23 | print(binaryToDecimal(num)) 24 | 25 | ''' 26 | Input: 27 | 100 28 | Output: 29 | 4 30 | ''' 31 | -------------------------------------------------------------------------------- /Tree_Preorder_Traversal/Tree_Preorder_Traversal.py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,key): 3 | self.left = None 4 | self.right = None 5 | self.val = key 6 | 7 | def PreOrder(root): 8 | if root: 9 | print(root.val, end = " ") 10 | PreOrder(root.left) 11 | PreOrder(root.right) 12 | 13 | root = node(1) 14 | root.left = node(2) 15 | root.right = node(3) 16 | root.left.left = node(4) 17 | root.left.right = node(5) 18 | root.right.left = node(6) 19 | root.right.right = node(7) 20 | 21 | print("Pre Order traversal of tree is", end = " ") 22 | PreOrder(root) 23 | 24 | ''' Output 25 | 26 | Pre Order traversal of tree is 1 2 4 5 3 6 7 27 | 28 | ''' 29 | -------------------------------------------------------------------------------- /Decagonal_Numbers/Decagonal_Numbers.go: -------------------------------------------------------------------------------- 1 | /* 2 | A decagonal number is a figurate number that extends the concept of 3 | triangular and square numbers to the decagon (a ten-sided polygon). 4 | 5 | The n-th decagonal number is given by the formula: 6 | Dn = 4n^2 - 3n. 7 | */ 8 | 9 | package main 10 | 11 | import "fmt" 12 | 13 | func decagonalnumber (num int) int { 14 | // Using formula 15 | return 4 * num * num - 3 * num 16 | } 17 | 18 | func main() { 19 | var num int 20 | fmt.Scanf("%d", &num) 21 | 22 | fmt.Printf("%d decagonal number : %d", num, decagonalnumber(num)) 23 | } 24 | 25 | /* 26 | Input: 27 | 6 28 | output: 29 | 6 decagonal number : 126 30 | */ 31 | -------------------------------------------------------------------------------- /Nonagonal_Number/Nonagonal_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | NONAGONAL NUMBER 3 | 4 | These numbers define the number of dots(or, verices), when a few nonagons 5 | (9-sided polygons) sharing a common corner are placed together. 6 | 7 | The nth Nonagonal Number has the formula : n * (7 * n - 5) / 2 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int nonagon(int num) { 14 | return (num * ((7 * num) - 5)) / 2; 15 | } 16 | 17 | int main() { 18 | int num; 19 | cin >> num; 20 | cout << "The Nonagonal Number at position " << num << " is: " << nonagon(num); 21 | } 22 | 23 | /* 24 | Input : num = 5 25 | Output : The Nonagonal Number at position 5 is: 75 26 | */ -------------------------------------------------------------------------------- /Shell_Sort/Shell_Sort.py: -------------------------------------------------------------------------------- 1 | def Shell_Sort(list): 2 | size = len(list) 3 | gap = int(size / 2) 4 | 5 | while gap > 0: 6 | for i in range(gap, size): 7 | temp = list[i] 8 | 9 | j = i 10 | while j >= gap and list[j - gap] > temp: 11 | list[j] = list[j - gap] 12 | j -= gap 13 | 14 | list[j] = temp 15 | gap = int(gap / 2) 16 | 17 | def Print_list(list): 18 | for i in range(len(list)): 19 | print(list[i], end = " ") 20 | 21 | print() 22 | 23 | list = [12, 34, 54, 2, 3] 24 | 25 | Shell_Sort(list) 26 | Print_list(list) 27 | 28 | 29 | ''' Output 30 | 31 | 2 3 12 34 54 32 | 33 | ''' 34 | -------------------------------------------------------------------------------- /Bitwise_Addition/bitwise_addition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int bitwiseaddition(int a, int b) 5 | { 6 | while(b != 0) 7 | { 8 | int carry = a & b; 9 | a = a ^ b; 10 | b = carry << 1; 11 | } 12 | return a; 13 | } 14 | int main() 15 | { 16 | int no1,no2; 17 | cout << "Enter two numbers: "; 18 | cin >> no1 >> no2; 19 | cout << endl << "using bitwise operations," << endl; 20 | cout << endl << no1 << "+" << no2 <<" = " << bitwiseaddition(no1, no2); 21 | return 0; 22 | } 23 | 24 | 25 | /* 26 | INPUT: 27 | Enter two numbers: 12 30 28 | 29 | OUTPUT: 30 | using bitwise operations, 31 | 32 | 12+30 = 42 33 | */ 34 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_Of_Squares.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of squares of first N natural numbers is given 3 | by the equation ((N) * (N + 1) * (2 * N + 1)) / 6 i.e. 4 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2.........N^2 sums upto 5 | ((N) * (N + 1) * (2 * N + 1)) / 6 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int N; 14 | cout<<"Enter the value of N: "<>N; 16 | int ans = ((N) * (N + 1) * (2 * N + 1)) / 6; 17 | cout< 7 | 8 | int towerOfHanoi(int n, char barFrom, char barTo, char barAuxillary) 9 | { 10 | if(n == 1) 11 | { printf("\nMove disk 1 from %c to %c", barFrom, barTo); 12 | return 1; 13 | } 14 | towerOfHanoi(n - 1, barFrom, barAuxillary, barTo); 15 | printf("\nMove disk %d from %c to %c", n, barFrom, barTo); 16 | towerOfHanoi(n - 1, barAuxillary, barTo, barFrom); 17 | } 18 | 19 | int main() 20 | { 21 | int disks; 22 | printf("Enter the no. of disks: "); 23 | scanf("%d", &disks); 24 | towerOfHanoi(disks, 'A', 'C', 'B'); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Tree_Postorder_Traversal/Tree_Postorder_Traversal.py: -------------------------------------------------------------------------------- 1 | class node: 2 | def __init__(self,key): 3 | self.left = None 4 | self.right = None 5 | self.val = key 6 | 7 | def PostOrder(root): 8 | if root: 9 | PostOrder(root.left) 10 | PostOrder(root.right) 11 | print(root.val, end = " ") 12 | 13 | root = node(1) 14 | root.left = node(2) 15 | root.right = node(3) 16 | root.left.left = node(4) 17 | root.left.right = node(5) 18 | root.right.left = node(6) 19 | root.right.right = node(7) 20 | 21 | print("Post Order traversal of tree is", end = " ") 22 | PostOrder(root) 23 | 24 | ''' Output 25 | 26 | Post Order traversal of tree is 4 5 2 6 7 3 1 27 | 28 | ''' 29 | -------------------------------------------------------------------------------- /Centered_Square_Number/Centered_Square_Number.go: -------------------------------------------------------------------------------- 1 | /* 2 | Centered Square Number is a centered figurate number that gives the number 3 | of dots in a square with a dot in the center and all other dots surrounding 4 | the center dot in successive square layers. 5 | 6 | Nth Centered square number can be calculated by using formula n^2 + (n-1)^2. 7 | */ 8 | 9 | package main 10 | import "fmt" 11 | 12 | func centeredSquare(num int)int { 13 | // Using formula 14 | return num * num + ((num - 1) * (num - 1)) 15 | } 16 | 17 | func main() { 18 | var num int 19 | fmt.Scanf("%d", &num) 20 | fmt.Printf("%d", centeredSquare(num)) 21 | } 22 | 23 | /* 24 | Input: 25 | 6 26 | output: 27 | 61 28 | */ 29 | -------------------------------------------------------------------------------- /Number_Conversion/C/Octal_To_Decimal.c: -------------------------------------------------------------------------------- 1 | /* 2 | For octal number with n digits: 3 | dn-1 ... d3 d2 d1 d0 4 | The decimal number is equal to the sum of octal digits (dn) times their power of 8 (8n): 5 | decimal = d0×8^0 + d1×8^1 + d2×8^2 + ... 6 | */ 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | int num; 13 | scanf("%d", &num); 14 | int decimal; 15 | int base = 1; 16 | 17 | while (num) 18 | { 19 | int x = num % 10; 20 | num = num / 10; 21 | 22 | decimal += x * base; 23 | 24 | base = base * 8; 25 | } 26 | printf("%d", decimal); 27 | } 28 | 29 | /* 30 | Input: 31 | 1000 32 | Output: 33 | 512 34 | */ 35 | -------------------------------------------------------------------------------- /Insertion_Sort/Insertion_Sort.rb: -------------------------------------------------------------------------------- 1 | #Function for insertion sort 2 | def insertionSort (array) 3 | for i in 1 .. array.length - 1 4 | temp = array[i] #Store key value 5 | j = i - 1 6 | while j > 0 and array[j] > temp #Comparing with key and swapping accordingly 7 | array[j+1] = array[j] 8 | j = j - 1 9 | end 10 | array[j+1] = temp #Final swap 11 | end 12 | end 13 | 14 | #Hardcoded array 15 | array = [0,9,1,4,2] 16 | 17 | #Pre sort array 18 | print(array) 19 | 20 | puts("\nAfter Sorting:\n") 21 | 22 | insertionSort(array) 23 | 24 | #Sorted Array 25 | print(array) 26 | 27 | #Output 28 | #[0, 9, 1, 4, 2] 29 | #[0, 1, 2, 4, 9] 30 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.rb: -------------------------------------------------------------------------------- 1 | #Method for Linear Search 2 | def LinearSearch (array, searchedValue) 3 | array.each do |value| 4 | if value == searchedValue 5 | return "Value found." 6 | end 7 | end 8 | return "Value not found." 9 | end 10 | 11 | #Hardcoded Array 12 | array = [9,1,2,0,3] 13 | 14 | print(array) 15 | 16 | #Searching for 0 17 | puts("\nSearching for 0 in the array: ") 18 | print(LinearSearch(array,0)) 19 | 20 | #Searching for 4 21 | puts("\nSearching for 4 in the array: ") 22 | print(LinearSearch(array,4)) 23 | 24 | 25 | #Output 26 | #[9, 1, 2, 0, 3] 27 | #Searching for 0 in the array: Value found. 28 | #Searching for 4 in the array: Value not found. 29 | -------------------------------------------------------------------------------- /Logarithmic_Exponent/Logarithmic_Exponent.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long exponent (int base, int power) { 6 | long long result = 1, value = base; 7 | while (power) { 8 | if (power & 1) 9 | result *= value; 10 | value *= value; 11 | power /= 2; 12 | } 13 | return result; 14 | } 15 | 16 | int main () { 17 | int base, power; 18 | cin >> base; 19 | cin >> power; 20 | cout << "Value of " << base << " raised to the power " << power << " is " << exponent(base, power); 21 | return 0; 22 | } 23 | 24 | /* 25 | Input: 26 | base : 2, power : 32 27 | Output: 28 | Value of 2 raised to the power 32 is 4294967296 29 | */ 30 | -------------------------------------------------------------------------------- /Naive_String_Matching/Naive_String_Matching.py: -------------------------------------------------------------------------------- 1 | def search(text, pattern): 2 | lengthText = len(text) 3 | lengthPattern = len(pattern) 4 | 5 | for i in range(0, lengthText - lengthPattern): 6 | j = 0 7 | 8 | while j < lengthPattern: 9 | if text[i + j] != pattern[j]: 10 | break 11 | 12 | j += 1 13 | 14 | if j == lengthPattern: 15 | print("Pattern found at " + str(i + 1)) 16 | 17 | text = input() 18 | pattern = input() 19 | search(text, pattern) 20 | 21 | ''' 22 | Input: 23 | text = "namanchamanbomanamansanam" 24 | pattern = "aman" 25 | 26 | Output: 27 | Pattern found at 2 28 | Pattern found at 8 29 | Pattern found at 17 30 | ''' 31 | -------------------------------------------------------------------------------- /Number_Conversion/C/Binary_To_Decimal.c: -------------------------------------------------------------------------------- 1 | /* 2 | For binary number with n digits: 3 | dn-1 ... d3 d2 d1 d0 4 | 5 | The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n): 6 | decimal = d0×2^0 + d1×2^1 + d2×2^2 + ... 7 | */ 8 | 9 | #include 10 | 11 | int main() 12 | { 13 | int num; 14 | scanf("%d", &num); 15 | int decimal; 16 | int base = 1; 17 | 18 | while (num) 19 | { 20 | int x = num % 10; 21 | num = num / 10; 22 | 23 | decimal += x * base; 24 | 25 | base = base * 2; 26 | } 27 | printf("%d", decimal); 28 | } 29 | 30 | /* 31 | Input: 32 | 100 33 | Output: 34 | 4 35 | */ 36 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.go: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of cubes of first N natural numbers is given 3 | by the equation (N * (N + 1) / 2) ^ 2 i.e. 4 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3.........N^3 sums upto 5 | (N * (N + 1) / 2) ^ 2 6 | */ 7 | 8 | package main 9 | import ( 10 | "fmt" 11 | "math" 12 | ) 13 | 14 | func main(){ 15 | var N int 16 | fmt.Printf("Enter the value of N: \n") 17 | fmt.Scanf("%d", &N) 18 | var ans float64 19 | ans = math.Pow(float64(N * (N + 1) / 2), float64(2)) 20 | fmt.Println(ans) 21 | } 22 | 23 | /* 24 | Input: 25 | N = 5 26 | 27 | Output: 28 | ans = 225 29 | 30 | Verification: 31 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3 32 | = 1 + 8 + 27 + 64 + 125 33 | = 225 34 | */ 35 | -------------------------------------------------------------------------------- /Tribonacci_Words/Tribonacci_Words.py: -------------------------------------------------------------------------------- 1 | ''' 2 | TRIBONACCI WORDS 3 | 4 | It is a series of words where each word is formed by concatenation 5 | of last three words. 6 | where : 7 | T(1) = 1 8 | T(2) = 12 9 | T(3) = 1213 10 | 11 | T(n) = T(n-1) + T(n-2) + T(n-3) 12 | ''' 13 | 14 | def tribonacci(n): 15 | if n == 1: 16 | return "1" 17 | if n == 2: 18 | return "12" 19 | if n == 3: 20 | return "1213" 21 | return tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3) 22 | 23 | n = int(input("Enter N: ")) 24 | print("The tribonacci word at position", n, "is:", tribonacci(n)) 25 | 26 | ''' 27 | INPUT : n = 5 28 | OUTPUT: The tribonacci word at position 5 is: 1213121121312 29 | ''' 30 | -------------------------------------------------------------------------------- /Ugly_Number/Ugly_Number.py: -------------------------------------------------------------------------------- 1 | # Ugly numbers are numbers whose only prime factors are 2, 3 or 5. 2 | 3 | x = False 4 | num = int(input("Input an integer number: ")) 5 | while (num != 1) : 6 | if (num % 5 == 0) : # To check if number is divisible by 5 or not 7 | num /= 5 8 | elif (num % 3 == 0) : # To check if number is divisible by 3 or not 9 | num /= 3 10 | elif (num % 2 == 0) : # To check if number is divisible by 2 or not 11 | num /= 2 12 | else : 13 | print("It is not an ugly number") 14 | x = True 15 | break 16 | if x == False : 17 | print("It is an ugly number") 18 | 19 | ''' 20 | Input an integer number: 8 21 | It is an ugly number. 22 | ''' 23 | -------------------------------------------------------------------------------- /Centered_Square_Number/Centered_Square_Number.c: -------------------------------------------------------------------------------- 1 | /* 2 | Centered Square Number is a centered figurate number that gives the number 3 | of dots in a square with a dot in the center and all other dots surrounding 4 | the center dot in successive square layers. 5 | 6 | Nth Centered square number can be calculated by using formula n^2 + (n-1)^2. 7 | */ 8 | 9 | #include 10 | 11 | int centeredSquare(int num) 12 | { 13 | // Using formula 14 | return num * num + ((num - 1) * (num - 1)); 15 | } 16 | 17 | int main() 18 | { 19 | int num; 20 | scanf("%d", &num); 21 | printf("%d", centeredSquare(num)); 22 | return 0; 23 | } 24 | 25 | /* 26 | Input: 27 | 6 28 | output: 29 | 61 30 | */ 31 | -------------------------------------------------------------------------------- /Fibonacci_Number/Fibonacci_Number.php: -------------------------------------------------------------------------------- 1 | 30 | -------------------------------------------------------------------------------- /Knapsack_Unbounded/Knapsack_Unbounded.py: -------------------------------------------------------------------------------- 1 | # Unbounded Knapsack Problem 2 | 3 | def UnboundedKnapsack(Capacity,n,weight,value): 4 | maxProfit=[] 5 | for i in range(Capacity+1): 6 | maxProfit.append(0) 7 | for i in range(0,Capacity+1): 8 | for j in range(0,n): 9 | if weight[j] <= i: 10 | maxProfit[i] = max(maxProfit[i] , maxProfit[i-weight[j]]+value[j]) 11 | return maxProfit[Capacity] 12 | 13 | # No. of items 14 | n = 4 15 | # Weights of all items 16 | weight = [5,10,8,15] 17 | # Values of all items 18 | value = [40,30,50,25] 19 | # Capacity of Knapsack 20 | Capacity = 120 21 | 22 | print("The maximum value possible is ",UnboundedKnapsack(Capacity,n,weight,value)) 23 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.js: -------------------------------------------------------------------------------- 1 | /* 2 | Sum of cubes of first N natural numbers can be calculated using the following equation 3 | 4 | Sum = (N*(N+1)/2)^2 5 | i.e 1^3 + 2^3 + 3^3 + 4^3 + ..... + N^3 6 | 7 | */ 8 | 9 | var number = window.prompt("Enter the number:", ""); 10 | var n = parseInt(number); 11 | var res = sumOfCubes(n); 12 | window.alert("Sum of cubes of first " + n + " natural numbers is " + res); 13 | function sumOfCubes(n) 14 | { 15 | var sum = Math.pow((n * (n + 1) / 2), 2); 16 | return sum; 17 | } 18 | 19 | /* 20 | Input:- 21 | N = 3 22 | 23 | Output:- 24 | 25 | Sum of cubes of first 3 natural numbers is 36 26 | 27 | Verification: 28 | 1^3 + 2^3 + 3^3 29 | = 1 + 8 + 27 30 | = 36 31 | */ 32 | -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.py: -------------------------------------------------------------------------------- 1 | ''' The sum of digits of a number is the mathematical addition 2 | of the digits extracted from the number. 3 | Example: number = 12345, sum = 1 + 2 + 3 + 4 + 5 = 15 4 | number = 1005, sum = 1 + 0 + 0 + 5 = 6 ''' 5 | 6 | def sumOfDigits(N): 7 | sum = 0 8 | while N > 0: 9 | a = N % 10 10 | sum = sum + a 11 | N = int(N / 10) 12 | return sum 13 | 14 | print("Enter number:") 15 | N = int(input()) 16 | ans = sumOfDigits(N) 17 | print("The sum of digits of %d is %d" % (N, ans)) 18 | 19 | ''' 20 | Input: 21 | 12345 22 | 23 | Output: 24 | The sum of digits of 12345 is 15 25 | 26 | Input: 27 | 1000 28 | 29 | Output: 30 | The sum of digits of 1000 is 1 31 | ''' 32 | -------------------------------------------------------------------------------- /Next_Perfect_Square/Next_Perfect_Square.go: -------------------------------------------------------------------------------- 1 | /* 2 | NEXT PERFECT SQUARE 3 | 4 | A perfect square is a number which can be expressed as a product of 5 | two equal numbers. The task is to find the first such number greater 6 | than the given number. 7 | */ 8 | 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | "math" 14 | ) 15 | 16 | func main() { 17 | var n float64 18 | fmt.Print("Enter N: ") 19 | fmt.Scan(&n) 20 | x := math.Floor(math.Sqrt(n)) + 1 21 | fmt.Print("The next perfect square is ", (x * x)) 22 | } 23 | 24 | /* 25 | INPUT : n = 1091 26 | OUTPUT: The next perfect square is 1156 27 | VERIFICATION : 1156 = 34 * 34 28 | Hence it is a perfect square. 29 | */ 30 | -------------------------------------------------------------------------------- /Number_Conversion/C++/Binary_To_Decimal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | For binary number with n digits: 3 | dn-1 ... d3 d2 d1 d0 4 | The decimal number is equal to the sum of binary digits (dn) times their power of 2 (2n): 5 | decimal = d0×2^0 + d1×2^1 + d2×2^2 + ... 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int num; 14 | cin>>num; 15 | int decimal; 16 | int base = 1; 17 | 18 | while (num) 19 | { 20 | int x = num % 10; 21 | num = num / 10; 22 | 23 | decimal += x * base; 24 | 25 | base = base * 2; 26 | } 27 | cout< 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | int num; 14 | cin>>num; 15 | int decimal; 16 | int base = 1; 17 | 18 | while (num) 19 | { 20 | int x = num % 10; 21 | num = num / 10; 22 | 23 | decimal += x * base; 24 | 25 | base = base * 8; 26 | } 27 | cout< 0: 10 | a = N % 10 11 | rev = rev * 10 + a 12 | N = int(N / 10) 13 | return rev 14 | 15 | print("Enter number to reverse:") 16 | N = int(input()) 17 | ans = reverse(N) 18 | print("The reverse of %d is %d" % (N, ans)) 19 | 20 | ''' 21 | Input: 22 | 12345 23 | 24 | Output: 25 | The reverse of 12345 is 54321 26 | 27 | Input: 28 | 1000 29 | 30 | Output: 31 | The reverse of 1000 is 1 32 | ''' 33 | -------------------------------------------------------------------------------- /Bubble_Sort/Bubble_Sort.php: -------------------------------------------------------------------------------- 1 | $arr[$j + 1]) 13 | { 14 | $temp = $arr[$j]; 15 | $arr[$j] = $arr[$j + 1]; 16 | $arr[$j + 1] = $temp; 17 | } 18 | } 19 | } 20 | 21 | for ( $k=0 ; $k<$size;$k++) 22 | echo "$arr[$k] "; 23 | } 24 | // Bubble sort function calling 25 | Bubble_Sort($arr, 7); 26 | 27 | // Output 28 | // 1 2 3 4 4 6 8 29 | ?> 30 | -------------------------------------------------------------------------------- /Chocolate_Distribution/README.md: -------------------------------------------------------------------------------- 1 | Chocolate distribution problem states that - 2 | 3 | There exist M chocolate packets each packet can have variable number of chocolates. There are N students. 4 | Chocolate packets should be distributed such that: 5 | a: Each student gets 1 packet 6 | b: Difference between number of chocolates in packet with maximum chocolates and packet with minimum chocolates given to the students is minimum. 7 | 8 | Solution 9 | 10 | One efficient solution is based on the observation that, to minimize the difference, we must choose consecutive elements from sorted packets. We first sort the array a[0..n-1], then find the subarray of size m with the minimum difference between last and first elements. 11 | -------------------------------------------------------------------------------- /Nonagonal_Number/Nonagonal_Number.go: -------------------------------------------------------------------------------- 1 | /* 2 | NONAGONAL NUMBER 3 | 4 | These numbers define the number of dots(or, verices), when a few nonagons 5 | (9-sided polygons) sharing a common corner are placed together. 6 | 7 | The nth Nonagonal Number has the formula : n * (7 * n - 5) / 2 8 | */ 9 | 10 | package main 11 | 12 | import ( 13 | "fmt" 14 | ) 15 | 16 | 17 | func nonagon(num int) int{ 18 | return (num * ((7 * num) - 5)) / 2 19 | } 20 | 21 | func main() { 22 | var num int 23 | fmt.Scan(&num) 24 | fmt.Print("The Nonagonal Number at position " , num , 25 | " is: " , nonagon(num)) 26 | } 27 | 28 | /* 29 | Input : num = 5 30 | Output : The Nonagonal Number at position 5 is: 75 31 | */ -------------------------------------------------------------------------------- /Anagram/Anagram.js: -------------------------------------------------------------------------------- 1 | function check_anagram (s1, s2) { 2 | let l1 = s1.length; 3 | let l2 = s2.length; 4 | 5 | // Check if length of both the strings are same 6 | if(l1 !== l2){ 7 | console.log('Invalid Input'); 8 | return 9 | } 10 | 11 | // sort the strings by splitting it to an array then apply the sort method and join the array 12 | let str1 = s1.split('').sort().join(''); 13 | let str2 = s2.split('').sort().join(''); 14 | 15 | // Check if both the strings are equal 16 | if(str1 === str2){ 17 | console.log("It is an Anagrams"); 18 | } 19 | else{ 20 | console.log("It is not an Anagrams"); 21 | } 22 | } 23 | 24 | check_anagram("debit card","bad credit") 25 | -------------------------------------------------------------------------------- /Centered_Square_Number/Centered_Square_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Centered Square Number is a centered figurate number that gives the number 3 | of dots in a square with a dot in the center and all other dots surrounding 4 | the center dot in successive square layers. 5 | 6 | Nth Centered square number can be calculated by using formula n^2 + (n-1)^2. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int centeredSquare(int num) 13 | { 14 | // Using formula 15 | return num * num + ((num - 1) * (num - 1)); 16 | } 17 | 18 | int main() 19 | { 20 | int num; 21 | cin>>num; 22 | cout< 7 | 8 | using namespace std; 9 | 10 | int towerOfHanoi(int n, char barFrom, char barTo, char barAuxillary) 11 | { 12 | if(n == 1) 13 | { cout<<"\nMove disk 1 from "<>disks; 26 | towerOfHanoi(disks, 'A', 'C', 'B'); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Decagonal_Numbers/Decagonal_Numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | A decagonal number is a figurate number that extends the concept of 3 | triangular and square numbers to the decagon (a ten-sided polygon). 4 | 5 | The n-th decagonal number is given by the formula: 6 | Dn = 4n^2 - 3n. 7 | */ 8 | 9 | #include 10 | 11 | // To calculate decagonal number 12 | int decagonalnumber (int num) 13 | { 14 | // Using formula 15 | return 4 * num * num - 3 * num; 16 | } 17 | 18 | int main() 19 | { 20 | int num; 21 | scanf("%d", &num); 22 | printf("%d decagonal number : %d", num, decagonalnumber(num)); 23 | return 0; 24 | } 25 | 26 | /* 27 | Input: 28 | 6 29 | output: 30 | 6 decagonal number : 126 31 | */ 32 | -------------------------------------------------------------------------------- /Set_Bits_Till_N/Set_Bits_Till_N.js: -------------------------------------------------------------------------------- 1 | /* 2 | TOTAL SET BITS FROM 1 to N 3 | 4 | Total set bits in a number n is defined as number of 1s in 5 | binary representation of the number. The task is to calculate 6 | total number of set bits in all numbers till N. 7 | */ 8 | function count(n) { 9 | let bit = 0; 10 | while(n > 0) { 11 | bit += n & 1; 12 | n >>= 1; 13 | } 14 | return bit; 15 | } 16 | function setBits(n) { 17 | let bit = 0; 18 | for (let i = 0; i < n; i++) { 19 | bit = bit + count(i); 20 | } 21 | return bit; 22 | } 23 | const n = 12; 24 | const ans = setBits(n); 25 | console.log(ans); 26 | 27 | 28 | /* 29 | INPUT : 30 | n = 12 31 | OUTPUT : 32 | The total set bits are 20 33 | */ 34 | -------------------------------------------------------------------------------- /Lucas_Numbers/Lucas_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | LUCAS NUMBERS 3 | 4 | These numbers are similar to Fibonacci Numbers. The only difference 5 | is that the first two terms are 2 and 1. 6 | ''' 7 | 8 | def lucas(n): 9 | a = [] 10 | a.append(2) 11 | a.append(1) 12 | for i in range(2, n): 13 | a.append(a[i - 1] + a[i - 2]) 14 | return a[n - 1] 15 | 16 | def lucas_recursive(n): 17 | if (n == 1): 18 | return 2 19 | if (n == 2): 20 | return 1 21 | return lucas_recursive(n - 1) + lucas_recursive(n - 2) 22 | 23 | number = int(input()) 24 | print("The Lucas number at position", number, "is", lucas(number)) 25 | 26 | ''' 27 | INPUT : 28 | n = 8 29 | OUTPUT : 30 | The Lucas number at position 8 is 29 31 | ''' 32 | -------------------------------------------------------------------------------- /Perfect_Number/Perfect_Number.cpp: -------------------------------------------------------------------------------- 1 | // Perfect number is a positive integer that is equal to the sum of its proper divisors. 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int num, rem, sum = 0, i; 9 | cout << "Enter a Number: "; 10 | cin >> num; 11 | 12 | // Calculating the sum of divisors 13 | for (i = 1; i < num; i++) 14 | { 15 | rem = num % i; 16 | if (rem == 0) 17 | { 18 | sum = sum + i; 19 | } 20 | } 21 | if (sum == num) 22 | cout << "It is a perfect number"; 23 | else 24 | cout << "It is not a perfect number"; 25 | return 0; 26 | } 27 | 28 | /* 29 | Enter a Number: 28 30 | It is a perfect number */ 31 | -------------------------------------------------------------------------------- /Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.php: -------------------------------------------------------------------------------- 1 | 23 | 24 | /*Input: 25 | Enter an integer 26 | 10 27 | Output 28 | 2 3 5 7 29 | */ 30 | -------------------------------------------------------------------------------- /Dynamic_Programming_Rod_Cutting/Dynamic_Programming_Rod_Cutting.py: -------------------------------------------------------------------------------- 1 | 2 | INT_MIN = -32767 3 | 4 | # Function for money calculation 5 | def money(price, n): 6 | val = [0 for x in range(n + 1)] 7 | val[0] = 0 8 | 9 | for i in range(1, n + 1): 10 | max_val = INT_MIN 11 | for j in range(i): 12 | max_val = max(max_val, price[j] + val[i - j - 1]) 13 | val[i] = max_val 14 | 15 | return val[n] 16 | 17 | # Testing the function 18 | size = int(input()) 19 | arr = [] 20 | for i in range(0, size): 21 | arr.append(int(input())) 22 | print("Maximum Money : " + str(money(arr, size))) 23 | 24 | ''' 25 | INPUT: 26 | size = 8 27 | arr = {3, 5, 8, 9, 10, 17, 17, 20} 28 | OUTPUT: 29 | Maximum value is 24 30 | ''' 31 | -------------------------------------------------------------------------------- /Insertion_Sort/Insertion_Sort.py: -------------------------------------------------------------------------------- 1 | # function for insertion sort 2 | def Insertion_Sort(list): 3 | for i in range(1, len(list)): 4 | temp = list[i] 5 | j = i - 1 6 | while j >= 0 and list[j] > temp: 7 | list[j + 1] = list[j] 8 | j -= 1 9 | 10 | list[j + 1] = temp 11 | 12 | # function to print list 13 | def Print_list(list): 14 | for i in range(0, len(list)): 15 | print(list[i],end=" ") 16 | print() 17 | 18 | num = int(input()) 19 | list = [] 20 | for i in range(0, num): 21 | list.append(int(input())) 22 | Insertion_Sort(list) 23 | Print_list(list) 24 | 25 | ''' 26 | Input : 27 | num = 6 28 | array = [1, 6, 3, 3, 5, 2] 29 | 30 | Output : 31 | [1, 2, 3, 3, 5, 6] 32 | ''' 33 | -------------------------------------------------------------------------------- /Selection_Sort/Selection_Sort.py: -------------------------------------------------------------------------------- 1 | # Function for selection sort 2 | def Selection_Sort(array): 3 | for i in range(0, len(array) - 1): 4 | min_index = i 5 | 6 | for j in range(i + 1, len(array)): 7 | if array[j] < array[min_index]: 8 | min_index = j 9 | 10 | array[i], array[min_index] = array[min_index], array[i] 11 | 12 | # Function to print list 13 | def Print_list(array): 14 | for i in range(0, len(array)): 15 | print(array[i], end = " ") 16 | 17 | print() 18 | 19 | num = int(input()) 20 | array = [] 21 | for i in range(0, num): 22 | array.append(int(input())) 23 | Selection_Sort(array) 24 | Print_list(array) 25 | 26 | ''' Output 27 | 28 | 1 2 3 4 4 6 8 29 | 30 | ''' 31 | -------------------------------------------------------------------------------- /Sieve_Of_Eratosthenes/Sieve_Of_Eratosthenes.coffee: -------------------------------------------------------------------------------- 1 | SieveOfEratosthenes = (n) -> 2 | prime = Array(n + 1).fill(true) 3 | 4 | i = 2 5 | while i*i <= n 6 | if prime[i] is true 7 | for j in [2 * i...n] by i 8 | prime[j] = false 9 | 10 | i += 1 11 | 12 | string = "" 13 | for i in [2...n] 14 | if prime[i] is true 15 | string += i + " " 16 | 17 | console.log string 18 | 19 | 0 # Return 0 20 | 21 | 22 | n = 30 23 | 24 | console.log "Following are the prime numbers smaller than or equal to " + n 25 | SieveOfEratosthenes n 26 | 27 | 28 | ### Output 29 | 30 | Following are the prime numbers smaller than or equal to 30 31 | 2 3 5 7 11 13 17 19 23 29 32 | 33 | ### 34 | -------------------------------------------------------------------------------- /Decagonal_Numbers/Decagonal_Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A decagonal number is a figurate number that extends the concept of 3 | triangular and square numbers to the decagon (a ten-sided polygon). 4 | 5 | The n-th decagonal number is given by the formula: 6 | Dn = 4n^2 - 3n. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | // To calculate decagonal number 13 | int decagonalnumber (int num) 14 | { 15 | // Using formula 16 | return 4 * num * num - 3 * num; 17 | } 18 | 19 | int main() 20 | { 21 | int num; 22 | cin>>num; 23 | cout<= 0): 21 | print((hexa[j]), end = "") 22 | j = j - 1 23 | 24 | # Driver Code 25 | num = int(input()) 26 | decToHexa(num) 27 | 28 | '''Input: 29 | 1234 30 | Output: 31 | 4D2 32 | ''' 33 | -------------------------------------------------------------------------------- /Geometric_Progression/Sum_Of_Infinite_GP.py: -------------------------------------------------------------------------------- 1 | ''' 2 | SUM OF INFINITE GEOMETRIC PROGRESSION 3 | 4 | Any sequence of elements where the quotient between any two 5 | consecutive elements is equal is termed to be in G.P. 6 | The nth term of an G.P. is defined as (a * r^(n-1)) 7 | where a is the first element of the G.P. 8 | r is the common ratio of the G.P. 9 | n is the number of term. 10 | 11 | The Sum of infinite series of a G.P. is defined as : 12 | a / (1 - r), where -1 < r < 1 13 | ''' 14 | 15 | a = float(input("Enter the First Term of G.P.")) 16 | r = float(input("Enter the common ratio")) 17 | print("The sum of infinite G.P. is ", (a * 1.0 / (1 - r))) 18 | 19 | ''' 20 | INPUT : a = 5 21 | r = 0.5 22 | OUTPUT : The sum of infinite G.P. is 10.0 23 | ''' 24 | -------------------------------------------------------------------------------- /Prime_Number/Prime_Number.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def Prime(num, i): 4 | if (num == 1): 5 | return False 6 | 7 | # Base cases 8 | if (i <= 1): 9 | return True 10 | 11 | # Checking if i is a divisor or not 12 | if (num % i == 0): 13 | return False 14 | 15 | # Check for next divisor 16 | return Prime(num, i - 2) 17 | 18 | # Driver Program 19 | num = int(input()) 20 | if (num == 2): 21 | print("It is a prime number") 22 | if (num % 2 == 0): 23 | print("It is not a prime number") 24 | if (Prime(num, math.sqrt(num))): 25 | print("It is a prime number") 26 | else: 27 | print("It is not a prime number") 28 | 29 | # Input: 29 30 | # Output: It is a prime number 31 | -------------------------------------------------------------------------------- /Fibonacci_Words/Fibonacci_Words.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | FIBONACCI WORDS 3 | 4 | It is a series of words where each word is formed by concatenation 5 | of last two words. 6 | where : 7 | T(1) = 0 8 | T(2) = 01 9 | 10 | T(n) = T(n-1) + T(n-2) 11 | */ 12 | 13 | #include 14 | using namespace std; 15 | 16 | string fibonacci(int n) { 17 | if (n == 1) { 18 | return "0"; 19 | } 20 | if (n == 2) { 21 | return "01"; 22 | } 23 | return (fibonacci(n - 1) + fibonacci(n - 2)); 24 | } 25 | 26 | int main() { 27 | int n; 28 | cout<<"Enter N: "; 29 | cin>>n; 30 | cout<<"The fibonacci word at position "< 2 | 3 | int search(int a[],int size, int elt) 4 | { 5 | int i,flag = -1; 6 | for (i=0;i 2 | Fixes # 3 | 4 | #### Checklist: 5 | - [ ] 4 space indentation. 6 | - [ ] Coding conventions are followed. 7 | - [ ] Input is taken dynamically. 8 | - [ ] Sample Input / Output is added at the end of file. 9 | - [ ] Logic Documentation (Comments). 10 | - [ ] File names are correct. 11 | 12 | Make sure these boxes are checked before your pull request (PR) is ready to be reviewed and merged. Thanks! 13 | 14 | #### Changes proposed in this pull request: 15 | - 16 | - 17 | 18 | #### Languages Used: 19 | - 20 | - 21 | 22 | #### Files Added: 23 | - 24 | - 25 | 26 | > We're happy to help you get this ready -- don't be afraid to ask for help. 27 | 28 | Thanks! 29 | -------------------------------------------------------------------------------- /Power_Of_Two/Power_Of_Two.py: -------------------------------------------------------------------------------- 1 | ''' 2 | To check whether a number is a power of two or not, 3 | we do bitwise "and" of number and number - 1, if the 4 | result is zero the number is a power of two. 5 | Example: number = 4, then 4 & 3 i.e 100 && 011 is 0 6 | 16 & 15 i.e 10000 & 01111 = 0 7 | This is true for all powers of two. 8 | ''' 9 | 10 | def isPowerOfTwo(x): 11 | return x and (not(x & (x - 1))) 12 | 13 | print("Enter a number to check:") 14 | num = int(input()) 15 | if isPowerOfTwo(num): 16 | print("The number is a power of two") 17 | else: 18 | print("The number is not a power of two") 19 | 20 | ''' 21 | Input: 22 | 64 23 | 24 | Output: 25 | The number is a power of two 26 | 27 | Input: 28 | 5 29 | 30 | Output: 31 | The number is not a power of two 32 | ''' 33 | -------------------------------------------------------------------------------- /Sleep_Sort/Sleep_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void routine(void *a) 6 | { 7 | int n = *(int *) a; 8 | Sleep(n); 9 | printf("%d ", n); 10 | } 11 | 12 | void sleepSort(int arr[], int n) 13 | { 14 | int i; 15 | HANDLE threads[n]; 16 | for (i = 0; i < n; i++) 17 | threads[i] = (HANDLE)_beginthread(&routine, 0, &arr[i]); 18 | WaitForMultipleObjects(n, threads, TRUE, INFINITE); 19 | return; 20 | } 21 | 22 | int main() 23 | { 24 | cin >> n; 25 | int arr[n]; 26 | for (int i = 0; i < n; i++) 27 | cin >> arr[i]; 28 | sleepSort (arr, n); 29 | 30 | return 0; 31 | } 32 | 33 | /* 34 | INPUT: 35 | n = 4 36 | arr[] = {12, 42, 23, 3} 37 | OUTPUT: 38 | 3 12 23 42 39 | */ 40 | -------------------------------------------------------------------------------- /Digit_Count/Digit_Count.java: -------------------------------------------------------------------------------- 1 | /* 2 | DIGIT COUNT OF A NUMBER 3 | 4 | The task is to count the number of digits present in the given 5 | number. 6 | */ 7 | 8 | import java.util.Scanner; 9 | 10 | class Solution { 11 | 12 | public static int getDigits(int n) { 13 | int ans = 0; 14 | while (n > 0) { 15 | ans ++; 16 | n /= 10; 17 | } 18 | return ans; 19 | } 20 | 21 | public static void main(String args[]) { 22 | System.out.print("Enter N: "); 23 | int n; 24 | Scanner s = new Scanner(System.in); 25 | n = s.nextInt(); 26 | System.out.print("Number of digits is : " + getDigits(n)); 27 | } 28 | } 29 | 30 | /* 31 | INPUT : n = 6534 32 | OUTPUT: Number of digits is : 4 33 | */ 34 | -------------------------------------------------------------------------------- /Dynamic_Programming_Rod_Cutting/Dynamic_Programming_Rod_Cutting.php: -------------------------------------------------------------------------------- 1 | $b) 6 | return $a; 7 | else 8 | return $b; 9 | } 10 | 11 | // Max Money 12 | function money($cost, $n) 13 | { 14 | $val = new SplFixedArray($n + 1); 15 | $val[0] = 0; 16 | 17 | for($i = 1; $i <= $n; $i++) 18 | { 19 | $max_value = 0; 20 | for($j = 0; $j < $i; $j++) 21 | $max_value = maximum($max_value, $cost[$j] + $val[$i-$j-1]); 22 | $val[$i] = $max_value; 23 | } 24 | return $val[$n]; 25 | } 26 | 27 | $cost = array(3, 5, 8, 9, 10, 17, 17, 20); 28 | $size = sizeof($cost); 29 | echo money($cost, $size); 30 | 31 | 32 | // Output 33 | // 24 34 | 35 | ?> 36 | -------------------------------------------------------------------------------- /Fibonacci_Words/Fibonacci_Words.go: -------------------------------------------------------------------------------- 1 | /* 2 | FIBONACCI WORDS 3 | 4 | It is a series of words where each word is formed by concatenation 5 | of last two words. 6 | where : 7 | T(1) = 0 8 | T(2) = 01 9 | 10 | T(n) = T(n-1) + T(n-2) 11 | */ 12 | 13 | package main 14 | 15 | import "fmt" 16 | 17 | func fibonacci(n int) string{ 18 | if n == 1 { 19 | return "0" 20 | } 21 | if n == 2 { 22 | return "01" 23 | } 24 | return (fibonacci(n - 1) + fibonacci(n - 2)) 25 | } 26 | 27 | func main() { 28 | var n int 29 | fmt.Print("Enter N: ") 30 | fmt.Scan(&n) 31 | fmt.Print("The fibonacci word at position ", n, " is: ", fibonacci(n)) 32 | 33 | } 34 | 35 | /* 36 | INPUT : n = 5 37 | OUTPUT: The fibonacci word at position 5 is: 01001010 38 | */ 39 | -------------------------------------------------------------------------------- /Inheritance(C++)/Single_Level.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Rectangle 6 | { 7 | protected: 8 | float length, breadth; 9 | 10 | public: 11 | Rectangle(): length(0.0), breadth(0.0) 12 | { 13 | length = 5; 14 | breadth = 6; 15 | } 16 | }; 17 | 18 | /* Area class is derived from base class Rectangle. */ 19 | class Area : public Rectangle 20 | { 21 | public: 22 | float calc() 23 | { 24 | return length * breadth; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | Area a; 31 | cout << "Area = " << a.calc() << " square meters" << endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | /* OUTPUT 38 | 39 | Area = 30 square meter 40 | 41 | */ 42 | -------------------------------------------------------------------------------- /Sleep_Sort/Sleep_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | std::mutex lock; 8 | 9 | void sortThread(const int x) { 10 | std::chrono::milliseconds duration{ x * 10 }; 11 | 12 | std::this_thread::sleep_for(duration); 13 | 14 | lock.lock(); 15 | std::cout << x << " "; 16 | lock.unlock(); 17 | } 18 | 19 | int main() { 20 | std::vector x = { 2, 4, 3, 1, 6, 8, 4 }; 21 | std::vector threads; 22 | 23 | for (const auto &i : x) { 24 | threads.push_back(std::thread(sortThread, i)); 25 | } 26 | 27 | for (auto it = threads.begin(); it != threads.end(); ++it) { 28 | it->join(); 29 | } 30 | 31 | std::cout << "\n"; 32 | } 33 | 34 | // Output 35 | // 1 2 3 4 4 6 8 36 | -------------------------------------------------------------------------------- /Taylor_series_Repo/Taylor_Series_Horner_Rule.cpp: -------------------------------------------------------------------------------- 1 | // Taylor series using Horner Rule in c++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // creating exponential function 7 | double exponential (int x, int num) 8 | { 9 | static double s; // declare static variable 10 | if (num == 0) // Base case 11 | { 12 | return s; 13 | } 14 | s = 1 + x * (s / num); // formula of horner's rule 15 | return exponential (x, num - 1); // recursive case 16 | } 17 | 18 | // main method 19 | int main () 20 | { 21 | int x, num; 22 | cin >> x >> num; 23 | double result = exponential (x, num); 24 | cout << result << endl; // x^num 25 | } 26 | 27 | /* Input:- 2 10 28 | Output:- 29 | 7.388713 30 | */ 31 | 32 | -------------------------------------------------------------------------------- /Tree_Inorder_Traversal/Tree_Inorder_Traversal.rb: -------------------------------------------------------------------------------- 1 | class Node 2 | def initialize(key) 3 | @left 4 | @right 5 | @val = key 6 | end 7 | attr_accessor :left 8 | attr_accessor :right 9 | attr_accessor :val 10 | end 11 | 12 | def InOrder(root) 13 | if root 14 | InOrder(root.left) 15 | print(root.val.to_s + " ") 16 | InOrder(root.right) 17 | end 18 | end 19 | 20 | print("InOrder traversal of tree is ") 21 | 22 | root = Node.new(1) 23 | root.left = Node.new(2) 24 | root.right = Node.new(3) 25 | root.left.left = Node.new(4) 26 | root.left.right = Node.new(5) 27 | root.right.left = Node.new(6) 28 | root.right.right = Node.new(7) 29 | 30 | InOrder(root) 31 | 32 | # => Output 33 | 34 | # InOrder traversal of tree is 4 2 5 1 6 3 7 35 | -------------------------------------------------------------------------------- /1-D_Array/Ruby/Max_And_Next_Max_Number.rb: -------------------------------------------------------------------------------- 1 | #Ruby program to find the maximum and the next maximum number in an array 2 | 3 | puts "Enter number of elements in array:" 4 | num =gets.to_i 5 | arr = Array.new(num) 6 | puts "Enter your array: " 7 | 8 | counter=0 9 | while counter 1 20 | puts ("Next max: "+ arr[num-2].to_s) 21 | end 22 | 23 | # Output: 24 | # Enter number of elements in array: 25 | # 5 26 | # Enter your array: 27 | # 3 28 | # 4 29 | # 7 30 | # 6 31 | # 9 32 | # Max: 9 33 | # Next max: 7 34 | 35 | -------------------------------------------------------------------------------- /Binary_Search/Binary_Search.jl: -------------------------------------------------------------------------------- 1 | function Binary_Search(x,key) #declared function 2 | mid = Int(floor((1 + length(x))/2)) #calculating the mid of array 3 | try 4 | if x[mid] == key 5 | println("Key found") # if key is found 6 | elseif x[mid]< key #comparision to see if right side of key is to iterated 7 | Binary_Search(x[mid+1:end],key) #recursive call 8 | else x[mid]>key #comparision to see if left side of key is to iterated 9 | Binary_Search(x[1:mid-1],key) #recursive call 10 | end 11 | catch 12 | println("Key: $key not found") # else key is not found 13 | end 14 | end 15 | 16 | x = [1,4,6,8,9,19,56] 17 | key = 10 18 | Binary_Search(x,key) 19 | -------------------------------------------------------------------------------- /Delannoy_Number/Delannoy_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | DELANNOY NUMBER 3 | 4 | This number denotes the number of different paths available from 5 | bottom-right corner of a grid i.e. (0,0) to top-left corner (m,n). 6 | The constraint is that we can only use a step east, north or, 7 | north-east(diagonal). 8 | ''' 9 | 10 | def check(x, y, row, col): 11 | if (x == row - 1 and y == col - 1): 12 | return 1 13 | if (x == row or y == col): 14 | return 0 15 | return check(x + 1, y, row, col) + \ 16 | check(x, y + 1, row, col) + \ 17 | check(x + 1, y + 1, row, col) 18 | 19 | row = int(input()) 20 | col = int(input()) 21 | print("The number of paths is", check(0, 0, row, col)) 22 | 23 | ''' 24 | INPUT : 25 | row = 3 26 | col = 5 27 | OUTPUT : 28 | The number of paths is 41 29 | ''' 30 | -------------------------------------------------------------------------------- /Tree_Preorder_Traversal/Tree_Preorder_Traversal.rb: -------------------------------------------------------------------------------- 1 | class Node 2 | def initialize(key) 3 | @left 4 | @right 5 | @val = key 6 | end 7 | attr_accessor :left 8 | attr_accessor :right 9 | attr_accessor :val 10 | end 11 | 12 | def PreOrder(root) 13 | if root 14 | print(root.val.to_s + " ") 15 | PreOrder(root.left) 16 | PreOrder(root.right) 17 | end 18 | end 19 | 20 | print("PreOrder traversal of tree is ") 21 | 22 | root = Node.new(1) 23 | root.left = Node.new(2) 24 | root.right = Node.new(3) 25 | root.left.left = Node.new(4) 26 | root.left.right = Node.new(5) 27 | root.right.left = Node.new(6) 28 | root.right.right = Node.new(7) 29 | 30 | PreOrder(root) 31 | 32 | # => Output 33 | 34 | # PreOrder traversal of tree is 1 2 4 5 3 6 7 35 | -------------------------------------------------------------------------------- /Binary_Search/Binary_Search.coffee: -------------------------------------------------------------------------------- 1 | Binary_Search = (array, desired) -> 2 | left = 0 3 | right = array.length - 1 4 | 5 | while left <= right 6 | middle = left + (right - left) / 2 7 | 8 | return middle if array[middle] is desired 9 | 10 | if desired < array[middle] 11 | right = middle - 1 12 | else 13 | left = middle + 1 14 | 15 | -1 16 | 17 | 18 | array = [1, 2, 3, 4, 5, 6, 7] 19 | 20 | index = Binary_Search array, 4 21 | 22 | if index isnt -1 23 | console.log "Found" 24 | else 25 | console.log "Not Found" 26 | 27 | index = Binary_Search array, 9 28 | 29 | if index isnt -1 30 | console.log "Found" 31 | else 32 | console.log "Not Found" 33 | 34 | 35 | ### Output 36 | 37 | Found 38 | Not Found 39 | 40 | ### 41 | -------------------------------------------------------------------------------- /Dearrangements/Dearrangements.py: -------------------------------------------------------------------------------- 1 | ''' 2 | DEARRANGEMENTS 3 | 4 | It is a permutation of N elements such that no element appears in 5 | its original position. 6 | We have to find the number of such permutations possible. 7 | 8 | If count(n) = no of dearrangements for n numbers, 9 | we use the generic formula 10 | count(n) = (n - 1) * [count(n - 1) + count(n - 2)] 11 | ''' 12 | 13 | def count(number): 14 | if (number <= 2): 15 | return (number + 1) % 2 16 | last = count(number - 1) 17 | secondLast = count(number - 2) 18 | return (number - 1) * (last + secondLast) 19 | 20 | number = int(input()) 21 | dearrangements = count(number) 22 | print("The number of dearrangements is", dearrangements) 23 | 24 | ''' 25 | INPUT : n = 6 26 | OUTPUT: The number of dearrangements is 265 27 | ''' 28 | -------------------------------------------------------------------------------- /Ugly_Number/Nth_Ugly_Number.rb: -------------------------------------------------------------------------------- 1 | # Function that checks whether the Number is ugly or not 2 | def Is_Ugly(x) 3 | m = 1 4 | while x != 1 5 | if x % 2 == 0 6 | x = x/2 7 | elsif x % 3 == 0 8 | x = x/3 9 | elsif x % 5 == 0 10 | x = x/5 11 | else 12 | m = 0 13 | break 14 | end 15 | end 16 | return m 17 | end 18 | 19 | print("Enter the Nth number") 20 | # User Input of Nth number 21 | n = gets.chomp.to_i 22 | i = 1 23 | 24 | # Finds the Nth ugly Number 25 | while n != 1 26 | if Is_Ugly(i+1) == 1 27 | n = n-1 28 | end 29 | i = i+1 30 | end 31 | 32 | # Prints the ugly Number 33 | print(i) 34 | 35 | =begin 36 | input: 37 | Enter the Nth number: 10 38 | output: 39 | 12 40 | =end 41 | -------------------------------------------------------------------------------- /Centered_Decagonal_Number/Centered_Decagonal_Number.go: -------------------------------------------------------------------------------- 1 | /* 2 | A centered decagonal number is a centered figurate number that represents 3 | a decagon with a dot in the center and all other dots surrounding the center 4 | dot in successive decagonal layers. 5 | 6 | The centered decagonal number for n is given by the formula 7 | 5n^2+5n+1 8 | */ 9 | 10 | package main 11 | 12 | import "fmt" 13 | 14 | func centeredDecagonal (num int) int { 15 | // Using formula 16 | return 5 * num * num + 5 * num + 1 17 | } 18 | 19 | func main() { 20 | var num int 21 | fmt.Scanf("%d", &num) 22 | 23 | fmt.Printf("%d centered decagonal number : %d", num, centeredDecagonal(num)) 24 | } 25 | 26 | /* 27 | Input: 28 | 6 29 | output: 30 | 6 centered decagonal number : 211 31 | */ 32 | -------------------------------------------------------------------------------- /Power_Of_Two/Power_Of_Two.js: -------------------------------------------------------------------------------- 1 | /* 2 | To check whether a number is a power of two or not, 3 | we do bitwise "and" of number and number - 1, if the 4 | result is zero the number is a power of two. 5 | Example: number = 4, then 4 & 3 i.e 100 && 011 is 0 6 | 16 & 15 i.e 10000 & 01111 = 0 7 | This is true for all powers of two. 8 | */ 9 | 10 | function isPowerofTwo(n){ 11 | return n && ( ! ( n & (n - 1) )); 12 | } 13 | 14 | var num = parseInt(prompt("Enter a number to check:")); 15 | 16 | if(isPowerofTwo(num)){ 17 | console.log("The number is a power of two"); 18 | } 19 | else{ 20 | console.log("The number is not a power of two"); 21 | } 22 | 23 | 24 | /* 25 | Input: 26 | 64 27 | Output: 28 | The number is a power of two 29 | Input: 30 | 5 31 | Output: 32 | The number is not a power of two 33 | */ 34 | -------------------------------------------------------------------------------- /Tree_Postorder_Traversal/Tree_Postorder_Traversal.rb: -------------------------------------------------------------------------------- 1 | class Node 2 | def initialize(key) 3 | @left 4 | @right 5 | @val = key 6 | end 7 | attr_accessor :left 8 | attr_accessor :right 9 | attr_accessor :val 10 | end 11 | 12 | def PostOrder(root) 13 | if root 14 | PostOrder(root.left) 15 | PostOrder(root.right) 16 | print(root.val.to_s + " ") 17 | end 18 | end 19 | 20 | print("PostOrder traversal of tree is ") 21 | 22 | root = Node.new(1) 23 | root.left = Node.new(2) 24 | root.right = Node.new(3) 25 | root.left.left = Node.new(4) 26 | root.left.right = Node.new(5) 27 | root.right.left = Node.new(6) 28 | root.right.right = Node.new(7) 29 | 30 | PostOrder(root) 31 | 32 | # => Output 33 | 34 | # PostOrder traversal of tree is 4 5 2 6 3 7 1 35 | -------------------------------------------------------------------------------- /Divisible_Pairs_In_Array/Divisible_Pairs_In_Array.py: -------------------------------------------------------------------------------- 1 | ''' 2 | DIVISIBLE PAIRS IN AN ARRAY 3 | 4 | The problem is to find all pairs of numbers in an array, such that 5 | one of those numbers divides the other. 6 | ''' 7 | 8 | print("Enter the size of array : ") 9 | num = int(input()) 10 | a = [] 11 | print("Enter array elements") 12 | for i in range(0, num): 13 | a.append(int(input())) 14 | print("The following are the divisible pairs :") 15 | for i in range(0, num): 16 | for j in range(i + 1, num): 17 | if (a[i] % a[j] == 0 or a[j] % a[i] == 0): 18 | print(a[i], a[j]) 19 | 20 | ''' 21 | Input : 22 | num = 4 23 | Array = [1, 4, 5, 20] 24 | 25 | Output : 26 | 27 | The following are the divisible pairs : 28 | 1 4 29 | 1 5 30 | 1 20 31 | 4 20 32 | 5 20 33 | ''' 34 | -------------------------------------------------------------------------------- /Leaders_Of_Array/Leaders_Of_Array.py: -------------------------------------------------------------------------------- 1 | ''' 2 | LEADERS OF AN ARRAY 3 | 4 | The task is to find all leaders in an array, where 5 | a leader is an array element which is greater than all the elements 6 | on its right side 7 | ''' 8 | 9 | print("Enter the size of array : ") 10 | num = int(input()) 11 | a = [] 12 | print("Enter array elements") 13 | for i in range(0, num): 14 | a.append(int(input())) 15 | maximum = a[num - 1] 16 | print("The following are the leaders of array : ") 17 | print(a[num - 1], " ", end = '') 18 | for i in range(num - 2, -1, -1): 19 | if (a[i] > maximum): 20 | print(a[i], " ", end = '') 21 | 22 | ''' 23 | Input : num = 5 24 | Array = [13, 4, 12, 1, 5] 25 | Output : 26 | The following are the leaders of array : 27 | 5 12 13 28 | ''' 29 | -------------------------------------------------------------------------------- /Natural_Numbers_Sum/Natural_Numbers_Sum.java: -------------------------------------------------------------------------------- 1 | /* 2 | Sum of First N Natural Numbers 3 | 4 | The task is to find sum of first n natural numbers, where n is 5 | the number input to us. We use the well known formula, 6 | sum = n * (n + 1)/2 to compute the sum 7 | 8 | */ 9 | 10 | import java.util.Scanner; 11 | 12 | class Sum_Of_N_Natural_Numbers { 13 | 14 | public static void main(String args[]) { 15 | int num; 16 | System.out.print("Enter N: "); 17 | Scanner s = new Scanner(System.in); 18 | num = s.nextInt(); 19 | int sum = num * (num + 1) / 2; 20 | System.out.print("The sum of first " + num + " natural numbers is : " + sum); 21 | } 22 | } 23 | 24 | /* 25 | Input : num = 6 26 | Output : The sum of first 6 natural numbers is : 21 27 | */ 28 | -------------------------------------------------------------------------------- /Palindrome/palindrome_string.py: -------------------------------------------------------------------------------- 1 | # Python program to find whether a string is a palindrome or not 2 | 3 | # Function to reverse a String 4 | def reverse(str): 5 | 6 | # Initialize variable 7 | rev = "" 8 | 9 | for i in range (len(str)-1, -1, -1): 10 | rev = rev + str[i] 11 | return rev 12 | 13 | 14 | # --- main --- 15 | string = raw_input(("Enter a string: ")) #User Input 16 | 17 | a = reverse(string) #Function call 18 | 19 | if(a == string): #Comparing the reversed String with original String 20 | print("String entered is palindrome!") 21 | else: 22 | print("String entered is not a palindrome!") 23 | 24 | ''' 25 | TEST CASES 26 | 27 | Enter a string: affcffa 28 | String entered is palindrome! 29 | 30 | Enter a string: abbcd 31 | String entered is not a palindrome! 32 | ''' 33 | -------------------------------------------------------------------------------- /Sum_Of_Cubes/Sum_Of_Cubes.java: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of cubes of first N natural numbers is given 3 | by the equation (N * (N + 1) / 2) ^ 2 i.e. 4 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3.........N^3 sums upto 5 | (N * (N + 1) / 2) ^ 2 6 | */ 7 | 8 | import java.util.Scanner; 9 | import java.lang.Math; 10 | 11 | class Sum_Of_Cubes 12 | { 13 | public static void main(String args[]) 14 | { 15 | Scanner sc = new Scanner(System.in); 16 | System.out.println("Enter the value of N: "); 17 | int N = sc.nextInt(); 18 | double ans = Math.pow(N * (N + 1) / 2, 2); 19 | System.out.println(ans); 20 | } 21 | } 22 | 23 | /* 24 | Input: 25 | N = 5 26 | 27 | Output: 28 | ans = 225 29 | 30 | Verification: 31 | 1^3 + 2^3 + 3^3 + 4^3 + 5^3 32 | = 1 + 8 + 27 + 64 + 125 33 | = 225 34 | */ 35 | -------------------------------------------------------------------------------- /Sum_Of_Squares/Sum_Of_Squares.java: -------------------------------------------------------------------------------- 1 | /* 2 | The sum of squares of first N natural numbers is given 3 | by the equation ((N) * (N + 1) * (2 * N + 1)) / 6 i.e. 4 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2.........N^2 sums upto 5 | ((N) * (N + 1) * (2 * N + 1)) / 6 6 | */ 7 | 8 | import java.util.Scanner; 9 | 10 | class Sum_Of_Squares 11 | { 12 | public static void main(String args[]) 13 | { 14 | Scanner sc = new Scanner(System.in); 15 | System.out.println("Enter the value of N: "); 16 | int N = sc.nextInt(); 17 | int ans = ((N) * (N + 1) * (2 * N + 1)) / 6; 18 | System.out.println(ans); 19 | } 20 | } 21 | 22 | /* 23 | Input: 24 | N = 5 25 | 26 | Output: 27 | ans = 55 28 | 29 | Verification: 30 | 1^2 + 2^2 + 3^2 + 4^2 + 5^2 31 | = 1 + 4 + 9 + 16 + 25 32 | = 55 33 | */ 34 | -------------------------------------------------------------------------------- /Compound_Interest/Compound_Interest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Compound Interest = P(1 + R/100)^T 3 | 4 | P is principle amount 5 | R is the rate and 6 | T is the time span 7 | */ 8 | 9 | import java.util.Scanner; 10 | import java.lang.Math; 11 | 12 | public class Compound_Interest { 13 | 14 | public static void main(String args[]) { 15 | Scanner s = new Scanner(System.in); 16 | double p, r, t; 17 | System.out.print("Enter Principal: "); 18 | p = s.nextDouble(); 19 | System.out.print("Enter Rate: "); 20 | r = s.nextDouble(); 21 | System.out.print("Enter Time: "); 22 | t = s.nextDouble(); 23 | System.out.print(p * (Math.pow((1 + r / 100), t))); 24 | } 25 | } 26 | 27 | /* 28 | INPUT : p = 1000, r = 8, t = 2 29 | OUTPUT : 1166.40 30 | */ 31 | -------------------------------------------------------------------------------- /Kadane_Algorithm/Kadane_Algorithm.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | // to calculate the sum of this maximum subArray 4 | function maxSubArraySum(a, size) 5 | { 6 | // overall maximum variable to keep track of subarray elements for largest sum 7 | var max_so_far = Number.NEGATIVE_INFINITY; 8 | 9 | var max_ending_here = 0; 10 | 11 | for (var i = 0; i < size; i++) 12 | { 13 | max_ending_here = max_ending_here + a[i]; 14 | if (max_so_far < max_ending_here) 15 | max_so_far = max_ending_here; 16 | 17 | if (max_ending_here < 0) 18 | max_ending_here = 0; 19 | } 20 | 21 | return max_so_far; 22 | } 23 | 24 | 25 | // input array 26 | var a = [-2, -3, 4, -1, -2, 1, 5, -3]; 27 | 28 | console.log(maxSubArraySum(a , a.length)); 29 | 30 | // OUTPUT : 31 | // 7 -------------------------------------------------------------------------------- /Number_Conversion/Java/Decimal_To_Octal.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Scanner; 3 | 4 | class Decimal_To_Octal 5 | { 6 | static void decToOctal(int num) 7 | { 8 | int[] octal = new int[20]; 9 | 10 | int i = 0; 11 | while (num != 0) 12 | { 13 | octal[i] = num % 8; 14 | num = num / 8; 15 | i++; 16 | } 17 | 18 | for (int j = i - 1; j >= 0; j--) 19 | System.out.print(octal[j]); 20 | } 21 | 22 | public static void main (String[] args) 23 | { 24 | int num; 25 | Scanner sc = new Scanner(System.in); 26 | num = sc.nextInt(); 27 | decToOctal(num); 28 | } 29 | } 30 | 31 | /* 32 | Input: 33 | 20 34 | Output: 35 | 24 36 | */ 37 | -------------------------------------------------------------------------------- /Set_Bits_Till_N/Set_Bits_Till_N.c: -------------------------------------------------------------------------------- 1 | /* 2 | TOTAL SET BITS FROM 1 to N 3 | 4 | Total set bits in a number n is defined as number of 1s in 5 | binary representation of the number. The task is to calculate 6 | total number of set bits in all numbers till N. 7 | */ 8 | 9 | #include 10 | 11 | int setBits(int n) { 12 | int bit = 0; 13 | for (int i = 0; i < n; i++) { 14 | bit = bit + count(i); 15 | } 16 | return bit; 17 | } 18 | 19 | int count(int n) { 20 | int bit = 0; 21 | while(n > 0) { 22 | bit += n & 1; 23 | n >>= 1; 24 | } 25 | return bit; 26 | } 27 | 28 | int main() { 29 | int n; 30 | scanf("%d", &n); 31 | printf("The total set bits are %d", setBits(n)); 32 | } 33 | 34 | /* 35 | INPUT : 36 | n = 12 37 | OUTPUT : 38 | The total set bits are 20 39 | */ 40 | -------------------------------------------------------------------------------- /Tribonacci_Numbers/Tribonacci_Numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | TRIBONACCI NUMBERS 3 | 4 | It is a series of numbers where each term is the sum of preceeding 3 5 | numbers of the sequence. 6 | 7 | T(n) = T(n-1) + T(n-2) + T(n-3) 8 | */ 9 | 10 | #include 11 | 12 | int tribonacci(int n) { 13 | int x = n - 3; 14 | if (x < 0) { 15 | return 0; 16 | } 17 | if (x == 0) { 18 | return 1; 19 | } 20 | return (tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)); 21 | } 22 | 23 | int main() { 24 | int n; 25 | printf("Enter N: "); 26 | scanf("%d", &n); 27 | printf("The tribonacci number at position %d is: %d", n, tribonacci(n)); 28 | } 29 | 30 | /* 31 | INPUT : n = 6 32 | OUTPUT: The tribonacci number at position 6 is: 4 33 | VERIFICATION: The tribonacci series : 0 0 1 1 2 4 34 | */ 35 | -------------------------------------------------------------------------------- /Modular_Exponentiation/Iterative_Modular_Exponentiation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //calculation of modular exponentiation using iterative approach 4 | long long power(long long base, long long exp,long long p) { 5 | long long holder = 1; 6 | base=base%p; 7 | while (exp > 0) { 8 | if (exp & 1) 9 | holder = (holder * base)%p; 10 | base = (base * base)%p; 11 | exp >>= 1; 12 | } 13 | return holder; 14 | } 15 | 16 | int main() 17 | { 18 | long long base,exponent ,p; 19 | 20 | scanf("%lli %lli %lli",&base,&exponent,&p); 21 | //taking input for base and exponent ; 22 | 23 | printf("The result of given expression is id %lli",power(base,exponent,p)); 24 | 25 | return 0; 26 | } 27 | 28 | /* 29 | output: 30 | base : 2 31 | exponent:4 32 | output :The result of given expression is1 33 | */ 34 | -------------------------------------------------------------------------------- /Prefix_Function/Prefix_Function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void search(string s, string t) { 7 | string p = t + "$" + s; 8 | int n = (int)p.size(); 9 | vector pi(n + 1, 0); 10 | for (int i = 1; i < n; ++i) { 11 | int j = pi[i - 1]; 12 | while (j > 0 && p[i] != p[j]) 13 | j = pi[j - 1]; 14 | if (p[i] == p[j]) 15 | ++j; 16 | pi[i] = j; 17 | } 18 | for (int i = (int)t.size(); i < n; ++i) 19 | if (pi[i] == (int)t.size()) 20 | cout << "Pattern found at " << i - 2 * (int)t.size() + 1 << endl; 21 | } 22 | 23 | int main() { 24 | string text = "namanchamanbomanamansanam"; 25 | string pattern = "aman"; 26 | search(text, pattern); 27 | return 0; 28 | } 29 | 30 | /* Output 31 | 32 | Pattern found at 2 33 | Pattern found at 8 34 | Pattern found at 17 35 | 36 | */ 37 | -------------------------------------------------------------------------------- /Selection_Sort/Selection_Sort.rb: -------------------------------------------------------------------------------- 1 | #Function for Selection_Sort 2 | def Selection_Sort (array) 3 | for i in (0 .. array.length - 2) 4 | min_index = i 5 | for j in (i + 1 .. array.length - 1) 6 | if (array[j] < array[min_index]) 7 | min_index = j 8 | end 9 | end 10 | if i != min_index #Swap if required 11 | temp = array[min_index] 12 | array[min_index] = array[i] 13 | array[i] = temp 14 | end 15 | end 16 | end 17 | 18 | array = [0,9,2,1,3,5] #Hard-coded Array 19 | 20 | puts("Array before sorting: ") 21 | 22 | print(array) 23 | 24 | puts("\nArray after sorting: ") 25 | 26 | Selection_Sort(array) 27 | 28 | print(array) 29 | 30 | #OutPut 31 | 32 | #Array before sorting: 33 | #[0, 9, 2, 1, 3, 5] 34 | #Array after sorting: 35 | #[0, 1, 2, 3, 5, 9] 36 | -------------------------------------------------------------------------------- /Shell_Sort/Shell_Sort.c: -------------------------------------------------------------------------------- 1 | // C implementation of Shell Sort 2 | #include 3 | 4 | void Shell_Sort(int array[], int size) 5 | { 6 | int gap, i, temp, j; 7 | 8 | for (gap = size / 2; gap > 0; gap /= 2) 9 | { 10 | for (i = gap; i < size; i++) 11 | { 12 | temp = array[i]; 13 | 14 | for (j = i; j >= gap && array[j-gap] > temp; j -= gap) 15 | array[j] = array[j - gap]; 16 | 17 | array[j] = temp; 18 | } 19 | } 20 | } 21 | 22 | void Print_Array(int array[], int size) 23 | { 24 | int i; 25 | for (i = 0; i < size; i++) 26 | printf("%d ", array[i]); 27 | 28 | printf("\n"); 29 | } 30 | 31 | int main() 32 | { 33 | int array[] = {12, 34, 54, 2, 3}; 34 | int size = 5; 35 | 36 | Shell_Sort(array, size); 37 | Print_Array(array, size); 38 | 39 | return 0; 40 | } 41 | 42 | /* Output 43 | 44 | 2 3 12 34 54 45 | 46 | */ 47 | -------------------------------------------------------------------------------- /Tribonacci_Numbers/Tribonacci_Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | TRIBONACCI NUMBERS 3 | 4 | It is a series of numbers where each term is the sum of preceeding 3 5 | numbers of the sequence. 6 | 7 | T(n) = T(n-1) + T(n-2) + T(n-3) 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | int tribonacci(int n) { 14 | int x = n - 3; 15 | if (x < 0) { 16 | return 0; 17 | } 18 | if (x == 0) { 19 | return 1; 20 | } 21 | return (tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)); 22 | } 23 | 24 | int main() { 25 | int n; 26 | cout<<"Enter N: "; 27 | cin>>n; 28 | cout<<"The tribonacci number at position "< 11 | 12 | // To calculate centered decagonal number 13 | int centeredDecagonal (int num) 14 | { 15 | // Using formula 16 | return 5 * num * num + 5 * num + 1; 17 | } 18 | 19 | int main() 20 | { 21 | int num; 22 | scanf("%d", &num); 23 | printf("%d centered decagonal number : %d", num, centeredDecagonal(num)); 24 | return 0; 25 | } 26 | 27 | /* 28 | Input: 29 | 6 30 | output: 31 | 6 centered decagonal number : 211 32 | */ 33 | -------------------------------------------------------------------------------- /Pascal_Triangle/Pascal_Triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int factorial(int num) 4 | { 5 | int fact; 6 | for(fact = 1; num > 1; num--) 7 | fact *= num; 8 | return fact; 9 | } 10 | 11 | int main() 12 | { 13 | int num, i, j; 14 | printf("Enter the numbers of rows "); 15 | scanf("%d",&num); 16 | 17 | for(i = 0; i < num; i++) 18 | { 19 | for(j = 0; j < num-i; j++) 20 | printf(" "); 21 | 22 | for(j = 0; j <= i; j++) 23 | printf(" %3d",factorial(i)/(factorial(j)*factorial(i-j)) ); 24 | 25 | printf("\n"); 26 | } 27 | return 0; 28 | } 29 | 30 | //INPUT:Enter the numbers of rows 6 31 | //OUTPUT: 32 | // 1 33 | // 1 1 34 | // 1 2 1 35 | // 1 3 3 1 36 | // 1 4 6 4 1 37 | // 1 5 10 10 5 1 38 | -------------------------------------------------------------------------------- /Set_Bits_Till_N/Set_Bits_Till_N.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | TOTAL SET BITS FROM 1 to N 3 | 4 | Total set bits in a number n is defined as number of 1s in 5 | binary representation of the number. The task is to calculate 6 | total number of set bits in all numbers till N. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | int count(int n) { 13 | int bit = 0; 14 | while(n > 0) { 15 | bit += n & 1; 16 | n >>= 1; 17 | } 18 | return bit; 19 | } 20 | 21 | int setBits(int n) { 22 | int bit = 0; 23 | for (int i = 0; i < n; i++) { 24 | bit = bit + count(i); 25 | } 26 | return bit; 27 | } 28 | 29 | int main() { 30 | int n; 31 | cin >> n; 32 | cout << "The total set bits are " << setBits(n); 33 | } 34 | 35 | /* 36 | INPUT : 37 | n = 12 38 | OUTPUT : 39 | The total set bits are 20 40 | */ 41 | -------------------------------------------------------------------------------- /Automorphic_Number/Automorphic_Number.java: -------------------------------------------------------------------------------- 1 | //A number is called Automorphic number if and only if its square ends in the same digits as the number itself. 2 | 3 | class Automorphic_Number 4 | { 5 | public static void main(String[] args) 6 | { 7 | int num = 5; 8 | int square = num * num; 9 | 10 | // Start Comparing digits 11 | while (num > 0) 12 | { 13 | //if any digit of N doesn't 14 | // match with its square's digits from last 15 | if (num % 10 != square % 10) 16 | { 17 | System.out.println("Not Automorphic"); 18 | return; 19 | } 20 | 21 | num /= 10; 22 | square /= 10; 23 | } 24 | System.out.println("Automorphic"); 25 | } 26 | } 27 | 28 | //OUTPUT:Automorphic 29 | -------------------------------------------------------------------------------- /Cyclic_Permutation/Cyclic_Permutation.py: -------------------------------------------------------------------------------- 1 | ''' 2 | CYCLIC PERMUTATIONS 3 | 4 | Cyclic permutations of a number are defined as a number that 5 | can be obtained by rotating the concerned number any number 6 | of times between it's digits. 7 | ''' 8 | 9 | def getDigits(n): 10 | ans = 0 11 | while (n): 12 | ans = ans + 1 13 | n = n // 10 14 | return ans 15 | 16 | def cyclic(n): 17 | length = getDigits(n) 18 | res = n 19 | while (1) : 20 | print(res) 21 | remainder = int(res % 10) 22 | quotient = int(res / 10) 23 | res = remainder * pow(10, length - 1) + quotient 24 | if (res == n): 25 | break 26 | 27 | n = int(input("Enter N: ")) 28 | cyclic(n) 29 | 30 | ''' 31 | INPUT : n = 6534 32 | OUTPUT: 6534 33 | 4653 34 | 3465 35 | 5346 36 | ''' 37 | -------------------------------------------------------------------------------- /Fermat_Little_Theorem/Fermat_Little_Theorem.coffee: -------------------------------------------------------------------------------- 1 | # Finding greatest common divisor using Euclidean Algorithm 2 | gcdFunction = (a, b) -> 3 | return b if a is 0 4 | 5 | gcdFunction b % a, a 6 | 7 | power = (x, y, m) -> 8 | return 1 if y is 0 9 | 10 | p = power(x, Math.floor(y / 2), m) % m 11 | p = (p * p) % m 12 | 13 | if y % 2 is 0 14 | p 15 | else 16 | (x * p) % m 17 | 18 | modInverse = (a, m) -> 19 | gcd = gcdFunction a, m 20 | 21 | if gcd isnt 1 22 | -1 23 | else 24 | power a, m - 2, m 25 | 26 | 27 | a = 3 28 | m = 11 29 | 30 | ans = modInverse a, m 31 | 32 | if ans is -1 33 | console.log "Inverse doesn't exist" 34 | else 35 | console.log "Modular multiplicative inverse is " + ans 36 | 37 | 38 | ### Output 39 | 40 | Modular multiplicative inverse is 4 41 | 42 | ### 43 | -------------------------------------------------------------------------------- /Lucas_Theorem/Lucas_Theorem.py: -------------------------------------------------------------------------------- 1 | def nCrModpDP(n, r, p): 2 | C = [0] * (r + 1) 3 | C[0] = 1 4 | for i in range(1, n + 1): 5 | for j in range(min(i, r), 0, -1): 6 | C[j] = (C[j] + C[j - 1])%p 7 | 8 | return C[r] 9 | 10 | def nCrModpLucas(n, r, p): 11 | n, r = int(n), int(r) 12 | if r == 0: 13 | return 1 14 | 15 | ni = n % p 16 | ri = r % p 17 | return (nCrModpLucas(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p 18 | 19 | def main(): 20 | n = int(input()) 21 | r = int(input()) 22 | p = int(input()) 23 | print("Value of nCr % p is ", nCrModpLucas(n, r, p)) 24 | 25 | if __name__ == '__main__': 26 | main() 27 | 28 | ''' 29 | INPUT - OUTPUT 30 | sample input 31 | n = 1000 32 | r = 900 33 | p = 13 34 | output 35 | value of nCr % p is 8 36 | ''' 37 | -------------------------------------------------------------------------------- /Palindrome/palindrome_no.py: -------------------------------------------------------------------------------- 1 | # Python program to find whether a number is a palindrome or not 2 | 3 | # Function to reverse a number 4 | def reverse(num): 5 | # Initialize variable 6 | rev = 0 7 | 8 | while ( num > 0 ) : 9 | dig = num % 10 10 | rev = rev * 10 + dig 11 | num = num // 10 12 | # Returning reversed number 13 | return rev 14 | 15 | # --- main --- 16 | num = int(input("Enter a number:")) 17 | a = reverse(num) 18 | 19 | # Comparing the reversed number with original number 20 | if(a == num): 21 | print("Number entered is palindrome!") 22 | else: 23 | print("Number entered is not a palindrome!") 24 | 25 | ''' 26 | TEST CASES 27 | INPUT 28 | Enter a number:3445443 29 | Number entered is palindrome! 30 | 31 | OUTPUT 32 | Enter a number:234 33 | Number entered is not a palindrome! 34 | ''' 35 | -------------------------------------------------------------------------------- /Centered_Decagonal_Number/Centered_Decagonal_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A centered decagonal number is a centered figurate number that represents 3 | a decagon with a dot in the center and all other dots surrounding the center 4 | dot in successive decagonal layers. 5 | 6 | The centered decagonal number for n is given by the formula 7 | 5n^2+5n+1 8 | */ 9 | 10 | #include 11 | using namespace std; 12 | 13 | // To calculate centered decagonal number 14 | int centeredDecagonal (int num) 15 | { 16 | // Using formula 17 | return 5 * num * num + 5 * num + 1; 18 | } 19 | 20 | int main() 21 | { 22 | int num; 23 | cin>>num; 24 | cout< 10 | 11 | void RMS(int a[], int n, int index, int ans) { 12 | if (index == n) { 13 | ans = ans / n; 14 | printf("RMS of array is : %d", ans); 15 | return; 16 | } 17 | ans = ans + a[index] * a[index]; 18 | RMS(a, n, index + 1, ans); 19 | } 20 | 21 | int main() { 22 | int n; 23 | scanf("%d", &n); 24 | int a[n]; 25 | for (int i = 0; i < n; i++) { 26 | scanf("%d", &a[i]); 27 | } 28 | RMS(a, n, 0, 0); 29 | } 30 | 31 | /* 32 | INPUT : n = 3 33 | a = [3, 15, 32] 34 | OUTPUT : 35 | RMS of array is : 419 36 | */ 37 | -------------------------------------------------------------------------------- /Set_Bits_Till_N/Set_Bits_Till_N.go: -------------------------------------------------------------------------------- 1 | /* 2 | TOTAL SET BITS FROM 1 to N 3 | 4 | Total set bits in a number n is defined as number of 1s in 5 | binary representation of the number. The task is to calculate 6 | total number of set bits in all numbers till N. 7 | */ 8 | 9 | package main 10 | 11 | import ( 12 | "fmt" 13 | ) 14 | 15 | func setBits(n int) int { 16 | var bit = 0 17 | for i := 0; i < n; i++ { 18 | bit = bit + count(i) 19 | } 20 | return bit 21 | } 22 | 23 | func count(n int) int { 24 | var bit = 0 25 | for (n > 0) { 26 | bit += n & 1 27 | n >>= 1 28 | } 29 | return bit 30 | } 31 | 32 | func main() { 33 | var n int 34 | fmt.Scan(&n) 35 | fmt.Print("The total set bits are ", setBits(n)) 36 | } 37 | 38 | /* 39 | INPUT : 40 | n = 12 41 | OUTPUT : 42 | The total set bits are 20 43 | */ 44 | -------------------------------------------------------------------------------- /Simple_Interest/Simple_Interest.java: -------------------------------------------------------------------------------- 1 | /* 2 | SIMPLE INTEREST 3 | 4 | Simple Interest in Accounting is calculated as : 5 | 6 | S.I. = (p * r * t) / 100 7 | where p = principal amount 8 | r = rate of interest 9 | t = time 10 | */ 11 | 12 | import java.util.Scanner; 13 | 14 | class Simple_Interest { 15 | 16 | public static void main(String args[]) { 17 | Scanner s = new Scanner(System.in); 18 | float p, r, t; 19 | System.out.print("Enter Principal: "); 20 | p = s.nextInt(); 21 | System.out.print("Enter Rate: "); 22 | r = s.nextInt(); 23 | System.out.print("Enter Time: "); 24 | t = s.nextInt(); 25 | System.out.print("Simple Interest is: " + ((p * r * t) / 100)); 26 | } 27 | } 28 | 29 | /* 30 | INPUT : p = 1000, r = 8, t = 2 31 | OUTPUT : Simple Interest is: 160.00 32 | */ 33 | -------------------------------------------------------------------------------- /Jolly_Jumper_Sequence/Jolly_Jumper_Sequence.py: -------------------------------------------------------------------------------- 1 | ''' 2 | JOLLY JUMPER SEQUENCE 3 | 4 | If the set of differences of consecutive elements of the sequence 5 | varies from 1 to n - 1, the sequence formed is said to be Jolly Jumper 6 | Sequence. 7 | ''' 8 | 9 | def Jolly(a, n): 10 | check = [] 11 | for i in range(0, n): 12 | check.append(0) 13 | for i in range(1, n): 14 | x = abs(a[i] - a[i - 1]) 15 | if (x == 0 or x > n - 1 or check[x] == 1): 16 | return 0 17 | check[x] = 1 18 | return 1 19 | 20 | n = int(input()) 21 | a = [int(x) for x in input().split()] 22 | if (Jolly(a, n) == 1): 23 | print("It is a jolly jumper sequence") 24 | else: 25 | print("It is not a jolly jumper sequence") 26 | 27 | ''' 28 | INPUT : 29 | n = 6 30 | a = [14 9 13 12 10 7] 31 | OUTPUT : 32 | It is a jolly jumper sequence 33 | ''' 34 | -------------------------------------------------------------------------------- /Nonagonal_Number/Nonagonal_Number.java: -------------------------------------------------------------------------------- 1 | /* 2 | NONAGONAL NUMBER 3 | 4 | These numbers define the number of dots(or, verices), when a few nonagons 5 | (9-sided polygons) sharing a common corner are placed together. 6 | 7 | The nth Nonagonal Number has the formula : n * (7 * n - 5) / 2 8 | */ 9 | 10 | import java.util.Scanner; 11 | 12 | public class Nonagonal { 13 | 14 | public static int nonagon(int num) { 15 | return (num * ((7 * num) - 5)) / 2; 16 | } 17 | 18 | public static void main(String args[]) { 19 | int num; 20 | Scanner s = new Scanner(System.in); 21 | num = s.nextInt(); 22 | System.out.print("The Nonagonal Number at position " + num + 23 | " is: " + nonagon(num)); 24 | } 25 | } 26 | 27 | /* 28 | Input : num = 5 29 | Output : The Nonagonal Number at position 5 is: 75 30 | */ -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.c: -------------------------------------------------------------------------------- 1 | /* The sum of digits of a number is the mathematical addition 2 | of the digits extracted from the number. 3 | Example: number = 12345, sum = 1 + 2 + 3 + 4 + 5 = 15 4 | number = 1005, sum = 1 + 0 + 0 + 5 = 6 */ 5 | 6 | #include 7 | 8 | int sumOfDigits(int N) 9 | { 10 | int a, sum = 0; 11 | while (N > 0) 12 | { 13 | a = N % 10; 14 | sum += a; 15 | N /= 10; 16 | } 17 | return sum; 18 | } 19 | 20 | int main() 21 | { 22 | int N; 23 | printf("Enter number:\n"); 24 | scanf("%d", &N); 25 | int ans = sumOfDigits(N); 26 | printf("The sum of digits of %d is %d", N, ans); 27 | return 0; 28 | } 29 | 30 | /* 31 | Input: 32 | 12345 33 | 34 | Output: 35 | The sum of digits of 12345 is 15 36 | 37 | Input: 38 | 1000 39 | 40 | Output: 41 | The sum of digits of 1000 is 1 42 | */ 43 | -------------------------------------------------------------------------------- /Coprime_Numbers/Coprime_Numbers.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Two integers a and b are said to be coprime if the only positive 3 | integer that divides both of them is 1 i.e their greatest common 4 | divisor or GCD is 1. 5 | Example: 5 and 7, as gcd(5, 7) = 1 6 | 9 and 11, as gcd(9, 11) = 1 7 | ''' 8 | 9 | def gcd(x, y): 10 | if x == 0: 11 | return y 12 | return gcd(y % x, x) 13 | 14 | def isCoprime(a, b): 15 | if gcd(a, b) == 1: 16 | return 1 17 | return 0 18 | 19 | print("Enter two numbers") 20 | a = int(input()) 21 | b = int(input()) 22 | ans = isCoprime(a, b) 23 | if ans == 1: 24 | print("The numbers are coprime") 25 | else: 26 | print("The numbers are not coprime") 27 | 28 | ''' 29 | Input: 30 | 9 31 | 11 32 | 33 | Output: 34 | The numbers are coprime 35 | 36 | Input: 37 | 4 38 | 12 39 | 40 | Output: 41 | The numbers are not coprime 42 | ''' 43 | -------------------------------------------------------------------------------- /Quick_Sort/Quick_Sort.coffee: -------------------------------------------------------------------------------- 1 | # Conquer 2 | Parition = (array, left, right) -> 3 | pivot = array[left] 4 | index = right 5 | 6 | for j in [right...left - 1] by -1 7 | if array[j] > pivot 8 | [array[index], array[j]] = [array[j], array[index]] 9 | index -= 1 10 | 11 | [array[index], array[left]] = [array[left], array[index]] 12 | index 13 | 14 | # Divide array into halves 15 | Quick = (array, left, right) -> 16 | if left < right 17 | pivot = Parition array, left, right 18 | 19 | Quick array, left, pivot - 1 20 | Quick array, pivot + 1, right 21 | 22 | Quick_Sort = (array) -> 23 | Quick array, 0, array.length - 1 24 | 25 | 26 | array = [2, 4, 3, 1, 6, 8, 4] 27 | 28 | Quick_Sort array 29 | 30 | console.log array 31 | 32 | 33 | ### Output 34 | 35 | [ 1, 2, 3, 4, 4, 6, 8 ] 36 | 37 | ### 38 | -------------------------------------------------------------------------------- /Selection_Sort/Selection_Sort.kt: -------------------------------------------------------------------------------- 1 | //Function for Selection Sort 2 | fun Selection_Sort(list: MutableList):List { 3 | for(i in 0 until (list.size) ) { 4 | var min_index: Int = i 5 | for(j in (i+1) until (list.size)) { 6 | if(list[j]) { 19 | val list = mutableListOf(4, 67, 89, 2, 45, 102) 20 | println("Unsorted List: $list") 21 | val sortedList = Selection_Sort(list) 22 | println("Sorted List: $sortedList") 23 | } 24 | 25 | //Input: Unsorted: [4, 67, 89, 2, 45, 102] 26 | //Output: Sorted: [2, 4, 25, 67, 89, 102] -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.go: -------------------------------------------------------------------------------- 1 | /* The sum of digits of a number is the mathematical addition 2 | of the digits extracted from the number. 3 | Example: number = 12345, sum = 1 + 2 + 3 + 4 + 5 = 15 4 | number = 1005, sum = 1 + 0 + 0 + 5 = 6 */ 5 | 6 | package main 7 | import "fmt" 8 | 9 | func sumOfDigits(N int) int { 10 | var a int 11 | var sum int 12 | for (N > 0) { 13 | a = N % 10 14 | sum += a 15 | N /= 10 16 | } 17 | return sum 18 | } 19 | 20 | func main() { 21 | var N int 22 | fmt.Printf("Enter number:\n") 23 | fmt.Scanf("%d", &N) 24 | ans := sumOfDigits(N) 25 | fmt.Printf("The sum of digits of %d is %d", N, ans) 26 | } 27 | 28 | /* 29 | Input: 30 | 12345 31 | 32 | Output: 33 | The sum of digits of 12345 is 15 34 | 35 | Input: 36 | 1000 37 | 38 | Output: 39 | The sum of digits of 1000 is 1 40 | */ 41 | 42 | -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.js: -------------------------------------------------------------------------------- 1 | /* The sum of digits of a number is the mathematical addition 2 | of the digits extracted from the number. 3 | Example: number = 12345, sum = 1 + 2 + 3 + 4 + 5 = 15 4 | number = 1005, sum = 1 + 0 + 0 + 5 = 6 */ 5 | 6 | const sumOfDigits = n => { 7 | let sum = 0; // initialise sum variable to 0 8 | while (n > 0) { 9 | const b = n % 10; 10 | sum += b; 11 | n = Math.floor(n / 10); // integer divison in JS 12 | } 13 | return sum; 14 | }; 15 | 16 | // Call the functions 17 | let n = 12345; 18 | console.log(`The sum of digits of ${n} is ${sumOfDigits(n)}`); 19 | 20 | n = 1000; 21 | console.log(`The sum of digits of ${n} is ${sumOfDigits(n)}`); 22 | 23 | /* 24 | Input: 25 | 12345 26 | Output: 27 | The sum of digits of 12345 is 15 28 | Input: 29 | 1000 30 | Output: 31 | The sum of digits of 1000 is 1 32 | */ 33 | -------------------------------------------------------------------------------- /Babylonian_Method/Babylonian_Method.py: -------------------------------------------------------------------------------- 1 | # 1. Start with an arbitrary positive start value num (the closer to the 2 | # root, the better). 3 | # 2. Initializing guess=1; 4 | # 3. Gives the accuracy of decimal places 5 | # 4. Do following until desired approximation is achieved. 6 | # a) Get the next approximation for root using average of num and guess 7 | # b) Set guess = number/guess */ 8 | 9 | #Function for square_root 10 | def square_root(number): 11 | num = number 12 | guess = 1 13 | accuracy = 0.000001 14 | while(num - guess > accuracy): 15 | num = (num + guess) / 2 16 | guess = number / num 17 | 18 | return num 19 | 20 | #To check if function is working 21 | n = int(input()) 22 | print("Square root of", n, "is", round(square_root(n), 6)) 23 | 24 | #Input: n = 2 25 | #Output :- Square root of 3 is 1.732051 26 | -------------------------------------------------------------------------------- /Binary_Search/Binary_Search.php: -------------------------------------------------------------------------------- 1 | $right) 9 | return -1; 10 | 11 | $mid = ($right + $left) / 2; 12 | 13 | if ($list[$mid] == $element_to_be_found) 14 | return $mid + 1; 15 | else if ($list[$mid] > $element_to_be_found) 16 | return binary_search($element_to_be_found,$list,$left,$mid-1); 17 | else 18 | return binary_search($element_to_be_found,$list,$mid+1,$right); 19 | } 20 | 21 | $ans = binary_search($element_to_be_found, $list, 0, count($list) - 1); 22 | 23 | if($ans == -1) 24 | echo "Element not found"; 25 | else 26 | echo "Element found at position $ans"; 27 | 28 | /* 29 | 30 | Output : Element found at position 3 31 | 32 | */ 33 | 34 | ?> 35 | -------------------------------------------------------------------------------- /Euclidean_Algorithm/Euclidean_Algorithm.c: -------------------------------------------------------------------------------- 1 | // Program to demonstrate Basic Euclidean Algorithm 2 | #include 3 | 4 | // Function to return gcd of x and y 5 | int gcd_algorithm(int x,int y) 6 | { 7 | // if the remainder is 0, return second number 8 | if(x == 0) 9 | { 10 | return y; 11 | } 12 | return gcd_algorithm((y % x),x); // recursive call to the function 13 | } 14 | 15 | // Driver program 16 | int main() 17 | { 18 | int num1, num2, gcd; 19 | printf("\nEnter two numbers to find gcd using Euclidean algorithm: "); 20 | scanf("%d %d", &num1, &num2); 21 | gcd = gcd_algorithm(num1, num2); 22 | printf("\nThe GCD of %d and %d is %d\n", num1, num2, gcd); 23 | return 0; 24 | } 25 | 26 | /* 27 | Output- 28 | Enter two numbers to fing gcd using Euclidean algorithm: 29 | 12 16 30 | The GCD of 12 and 16 is 4 31 | */ 32 | -------------------------------------------------------------------------------- /Fermat_Little_Theorem/Fermat_Little_Theorem.rb: -------------------------------------------------------------------------------- 1 | def gcdFunction(a, b) 2 | if a == 0 3 | return b 4 | end 5 | return gcdFunction(b % a, a) 6 | end 7 | 8 | def power(x, y, m) 9 | if y == 0 10 | return 1 11 | end 12 | 13 | p = power(x, (y/2).to_i, m) % m 14 | p = (p * p) % m 15 | 16 | if y % 2 == 0 17 | return p 18 | else 19 | return (x * p) % m 20 | end 21 | end 22 | 23 | def modInverse(a, m) 24 | gcd = gcdFunction(a, m) 25 | 26 | if gcd != 1 27 | return -1 28 | else 29 | return power(a, m - 2, m) 30 | end 31 | end 32 | 33 | a = 3 34 | m = 11 35 | 36 | ans = modInverse(a, m) 37 | 38 | if ans == -1 39 | puts "Inverse doesn't exist" 40 | else 41 | puts "Modular multiplicative inverse is " + ans.to_s 42 | end 43 | 44 | # OUTPUT 45 | 46 | # Modular multiplicative inverse is 4 47 | -------------------------------------------------------------------------------- /Fibonacci_Number/Fibonacci_Number.py: -------------------------------------------------------------------------------- 1 | # Function to print nth fibonacci number 2 | def FibonacciNumber (num): 3 | if (num <= 1): 4 | return num 5 | else: 6 | return FibonacciNumber(num - 1) + FibonacciNumber(num - 2) 7 | 8 | # Function to print fibonacci series 9 | def FibonacciSeries(num): 10 | a = 0 11 | b = 1 12 | if (num < 1): 13 | return 14 | 15 | for i in range(0, num): 16 | print(b , end = " ") 17 | c = a + b; 18 | a = b; 19 | b = c; 20 | 21 | # Driver Program 22 | num = int(input()) 23 | print("The fibonacci number at position ", num, "is ", FibonacciNumber(num)) 24 | print("Fibonacci series:") 25 | FibonacciSeries(num) 26 | 27 | ''' 28 | Input : 29 | 6 30 | Output : 31 | The fibonacci number at position 6 is 8 32 | Fibonacci series: 33 | 1 1 2 3 5 8 34 | ''' 35 | -------------------------------------------------------------------------------- /Reverse_Of_Number/Reverse_Of_Number.c: -------------------------------------------------------------------------------- 1 | /* Reverse of a number is the number obtained when we read 2 | the original number backwards. 3 | Example: number = 12345, reverse = 54321 4 | number = 1005, reverse = 5001 */ 5 | 6 | #include 7 | 8 | // function to reverse a number 9 | int reverse(int N) 10 | { 11 | int a, rev = 0; 12 | while (N > 0) 13 | { 14 | a = N % 10; 15 | rev = rev * 10 + a; 16 | N /= 10; 17 | } 18 | return rev; 19 | } 20 | 21 | int main() 22 | { 23 | int N; 24 | printf("Enter number to reverse:\n"); 25 | scanf("%d", &N); 26 | int ans = reverse(N); 27 | printf("The reverse of %d is %d", N, ans); 28 | return 0; 29 | } 30 | 31 | /* 32 | Input: 33 | 12345 34 | 35 | Output: 36 | The reverse of 12345 is 54321 37 | 38 | Input: 39 | 1000 40 | 41 | Output: 42 | The reverse of 1000 is 1 43 | */ 44 | -------------------------------------------------------------------------------- /Reverse_Of_Number/Reverse_Of_Number.go: -------------------------------------------------------------------------------- 1 | /* Reverse of a number is the number obtained when we read 2 | the original number backwards. 3 | Example: number = 12345, reverse = 54321 4 | number = 1005, reverse = 5001 */ 5 | 6 | package main 7 | import "fmt" 8 | 9 | // function to reverse a number 10 | func reverse(N int) int { 11 | var a int 12 | var rev int 13 | for (N > 0) { 14 | a = N % 10 15 | rev = rev * 10 + a 16 | N /= 10 17 | } 18 | return rev 19 | } 20 | 21 | func main() { 22 | var N int 23 | fmt.Printf("Enter number to reverse:\n") 24 | fmt.Scanf("%d", &N) 25 | ans := reverse(N) 26 | fmt.Printf("The reverse of %d is %d", N, ans) 27 | } 28 | 29 | /* 30 | Input: 31 | 12345 32 | 33 | Output: 34 | The reverse of 12345 is 54321 35 | 36 | Input: 37 | 1000 38 | 39 | Output: 40 | The reverse of 1000 is 1 41 | */ 42 | -------------------------------------------------------------------------------- /Rotation_Of_Numbers/Rotation_Of_Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to rotations of a number 4 | void rotation(int num) 5 | { 6 | int temp = num; 7 | int digits = 0; 8 | 9 | // Finding the number of digits 10 | while (temp > 0) 11 | { 12 | digits++; 13 | temp /= 10; 14 | } 15 | 16 | int power = pow(10, digits - 1); 17 | 18 | // Rotations of the number 19 | for (int i = 0; i < digits - 1; i++) 20 | { 21 | int lastDigit = num % 10; 22 | int x = num / 10; 23 | num = lastDigit * power + x; 24 | printf("%d ", num); 25 | } 26 | } 27 | 28 | // Driver Code 29 | int main() 30 | { 31 | int num; 32 | scanf("%d", &num); 33 | rotation(num); 34 | return 0; 35 | } 36 | 37 | /* 38 | Input: 39 | 1234 40 | Output: 41 | 4123 3412 2341 42 | */ 43 | -------------------------------------------------------------------------------- /Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.ts: -------------------------------------------------------------------------------- 1 | export {} 2 | 3 | /* 4 | Extended Euclidean Algorithm :- Find greatest common divisor of the two numbers 5 | a*x + b*y = gcd(a,b) 6 | and solves the above linear equation 7 | */ 8 | 9 | // find greatest common divisor 10 | function gcdExtended(a:number , b:number) : number[] 11 | { 12 | //base case 13 | if (a==0) 14 | { 15 | return [b,0,1]; 16 | } 17 | //recursive call 18 | let ans: number[] = gcdExtended(b%a , a); 19 | return [ans[0], (ans[2] - (Math.floor(b/a) * ans[1])) , ans[1]]; 20 | } 21 | 22 | let a : number = 12; 23 | let b : number = 16; 24 | 25 | let ans : number[] = gcdExtended(a,b); 26 | console.log(ans[0] , ans[1] , ans[2]); 27 | 28 | /* 29 | INPUT 30 | a = 12 31 | b = 16 32 | 33 | OUTPUT 34 | 4 -1 1 35 | */ 36 | -------------------------------------------------------------------------------- /Lucas_Theorem/Lucus_Theorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int nCrModpDP(int n, int r, int p) 5 | { 6 | int C[r+1]; 7 | memset(C, 0, sizeof(C)); 8 | 9 | C[0] = 1; 10 | 11 | for (int i = 1; i <= n; i++) 12 | { 13 | for (int j = min(i, r); j > 0; j--) 14 | C[j] = (C[j] + C[j-1])%p; 15 | } 16 | return C[r]; 17 | } 18 | int nCrModpLucas(int n, int r, int p) 19 | { 20 | if (r==0) 21 | return 1; 22 | int ni = n%p, ri = r%p; 23 | return (nCrModpLucas(n/p, r/p, p) * 24 | nCrModpDP(ni, ri, p)) % p; 25 | } 26 | 27 | int main() 28 | { 29 | int n = 1000, r = 900, p = 13; 30 | cout << "Value of nCr % p is " << nCrModpLucas(n, r, p); 31 | return 0; 32 | } 33 | /* INPUT - OUTPUT 34 | sample input=13 35 | output=value of nCr %p is 8 */ -------------------------------------------------------------------------------- /Next_Perfect_Square/Next_Perfect_Square.java: -------------------------------------------------------------------------------- 1 | /* 2 | NEXT PERFECT SQUARE 3 | 4 | A perfect square is a number which can be expressed as a product of 5 | two equal numbers. The task is to find the first such number greater 6 | than the given number. 7 | */ 8 | 9 | import java.util.Scanner; 10 | import java.lang.Math; 11 | 12 | class Next_Perfect_Square { 13 | 14 | public static void main(String args[]) { 15 | System.out.print("Enter N: "); 16 | int n; 17 | Scanner s = new Scanner(System.in); 18 | n = s.nextInt(); 19 | int x = (int)Math.floor(Math.sqrt(n)) + 1; 20 | System.out.print("The next perfect square is " + (x * x)); 21 | } 22 | } 23 | 24 | /* 25 | INPUT : n = 1091 26 | OUTPUT: The next perfect square is 1156 27 | VERIFICATION : 1156 = 34 * 34 28 | Hence it is a perfect square. 29 | */ 30 | -------------------------------------------------------------------------------- /Prefix_Sum/Prefix_Sum.js: -------------------------------------------------------------------------------- 1 | /* 2 | PREFIX SUM 3 | 4 | The task is to find the prefix sum of every element of a given array. 5 | Prefix sum equals to the sum of all element from start to the current element of the array. 6 | 7 | */ 8 | 9 | function prefixSum(array) { 10 | const prefix_array = []; 11 | let sum = 0; 12 | /* 13 | We maintain a prefix array where prefix_array[i] = prefix_array[i - 1] + array[i]. 14 | As, prefix sum of element i = prefix sum of element i - 1 + element i. 15 | */ 16 | for (const i in array) { 17 | sum = sum + array[i]; 18 | prefix_array.push(sum); 19 | } 20 | return prefix_array; 21 | } 22 | 23 | const array = [3, 2, 4, 6, 7]; 24 | const prefix_array = prefixSum(array); 25 | console.log(prefix_array); 26 | /* 27 | Input: array = { 3, 2, 4, 6, 7} 28 | Output: The prefix sum array is: 29 | 3 5 9 15 22 30 | */ 31 | -------------------------------------------------------------------------------- /Pythagorean_Quadruples/Pythagorean_Quadruples.py: -------------------------------------------------------------------------------- 1 | ''' 2 | PYTHAGOREAN QUADRUPLES 3 | 4 | Four numbers - a, b , c and d are said to be pythagorean quadruples if following 5 | formula holds : 6 | 7 | a^2 + b^2 + c^2 = d^2 8 | ''' 9 | 10 | a = int(input(("Enter a: "))) 11 | b = int(input(("Enter b: "))) 12 | c = int(input(("Enter c: "))) 13 | d = int(input(("Enter d: "))) 14 | sq_a = a * a 15 | sq_b = b * b 16 | sq_c = c * c 17 | sq_d = d * d 18 | if sq_a + sq_b + sq_d == sq_c or sq_a + sq_c + sq_d == sq_b \ 19 | or sq_b + sq_c + sq_d == sq_a or sq_a + sq_b + sq_c == sq_d: 20 | print("It is a pythagorean quadruple") 21 | else: 22 | print("It is not a pythagorean quadruple") 23 | 24 | ''' 25 | INPUT : a = 3 b = 6 c = 2 d = 7 26 | OUTPUT: It is a pythagorean quadruple 27 | VERIFICATION : 3^2 + 6^2 + 2^2 = 9 + 36 + 4 = 49 28 | 7^2 = 49 29 | ''' 30 | -------------------------------------------------------------------------------- /Root_Mean_Square/Root_Mean_Square.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ROOT MEAN SQUARE 3 | 4 | Root Mean Square or RMS is known as quadratic mean of a given array. 5 | It holds a lot of importance in mathematical calculations in various 6 | domains like Physics and geometry. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | void RMS(int a[], int n, int index, int ans) { 13 | if (index == n) { 14 | ans = ans / n; 15 | cout << "RMS of array is : " << ans; 16 | return; 17 | } 18 | ans = ans + a[index] * a[index]; 19 | RMS(a, n, index + 1, ans); 20 | } 21 | 22 | int main() { 23 | int n; 24 | cin >> n; 25 | int a[n]; 26 | for (int i = 0; i < n; i++) { 27 | cin >> a[i]; 28 | } 29 | RMS(a, n, 0, 0); 30 | } 31 | 32 | /* 33 | INPUT : n = 3 34 | a = [3, 15, 32] 35 | OUTPUT : 36 | RMS of array is : 419 37 | */ 38 | -------------------------------------------------------------------------------- /Centered_Square_Number/Centered_Square_Number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Centered Square Number is a centered figurate number that gives the number 3 | of dots in a square with a dot in the center and all other dots surrounding 4 | the center dot in successive square layers. 5 | 6 | Nth Centered square number can be calculated by using formula n^2 + (n-1)^2. 7 | */ 8 | 9 | import java.util.*; 10 | 11 | class Centered_Square 12 | { 13 | static int centeredSquare (int num) 14 | { 15 | // Using formula 16 | return num * num + ((num - 1) * (num - 1)); 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | int num; 22 | Scanner sc = new Scanner(System.in); 23 | num = sc.nextInt(); 24 | System.out.println(centeredSquare(num)); 25 | } 26 | } 27 | 28 | /* 29 | Input: 30 | 6 31 | output: 32 | 61 33 | */ 34 | -------------------------------------------------------------------------------- /Interpolation_Search/Interpolation_Search.coffee: -------------------------------------------------------------------------------- 1 | interpolation = (array, search_item) -> 2 | high = array.length - 1 3 | low = 0 4 | 5 | while low <= high and search_item >= array[low] and search_item <= array[high] 6 | rise = high - low 7 | run = array[high] - array[low] 8 | x = search_item - array[low] 9 | pos = low + (rise / run) * x 10 | 11 | return pos if array[pos] is search_item 12 | 13 | if search_item < array[pos] 14 | high = pos - 1 15 | else 16 | low = pos + 1 17 | 18 | -1 19 | 20 | array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 21 | search_item = 8 22 | index = interpolation array, search_item 23 | 24 | if index is -1 25 | console.log "Element not found" 26 | else 27 | console.log "Found at position " + index 28 | 29 | ### Ouput 30 | 31 | Found at position 7 32 | 33 | ### 34 | -------------------------------------------------------------------------------- /Automorphic_Number/Automorphic_Number.c: -------------------------------------------------------------------------------- 1 | //A number is called Automorphic number if and only if its square ends in the same digits as the number itself. 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int num; 8 | printf("Enter the number: "); 9 | scanf("%d", &num); 10 | 11 | // calculating the square of the number 12 | int square = num * num; 13 | 14 | // Start comparing digits 15 | while (num > 0) 16 | { 17 | // Return , if any digit of num doesn't 18 | // match with its square's digits from last 19 | if (num % 10 != square % 10) 20 | { 21 | printf("Not Automorphic"); 22 | return 0; 23 | } 24 | 25 | num /= 10; 26 | square /= 10; 27 | } 28 | 29 | printf("Automorphic"); 30 | return 0; 31 | } 32 | 33 | /*Enter the number: 25 34 | Automorphic*/ 35 | -------------------------------------------------------------------------------- /Lucas_Numbers/Lucas_Number.c: -------------------------------------------------------------------------------- 1 | /* 2 | LUCAS NUMBERS 3 | 4 | These numbers are similar to Fibonacci Numbers. The only difference 5 | is that the first two terms are 2 and 1. 6 | */ 7 | 8 | #include 9 | 10 | int lucas(int n) { 11 | int a[n]; 12 | a[0] = 2; 13 | a[1] = 1; 14 | for (int i = 2; i < n; i++) { 15 | a[i] = a[i - 1] + a[i - 2]; 16 | } 17 | return a[n - 1]; 18 | } 19 | 20 | int lucas_recursive(int n) { 21 | if (n == 1) { 22 | return 2; 23 | } 24 | if (n == 2) { 25 | return 1; 26 | } 27 | return lucas_recursive(n - 1) + lucas_recursive(n - 2); 28 | } 29 | 30 | int main() { 31 | int number; 32 | scanf("%d", &number); 33 | printf("The Lucas number at position %d is %d", number, lucas(number)); 34 | } 35 | 36 | /* 37 | INPUT : 38 | n = 8 39 | OUTPUT : 40 | The Lucas number at position 8 is 29 41 | */ 42 | -------------------------------------------------------------------------------- /Tribonacci_Words/Tribonacci_Words.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | TRIBONACCI WORDS 3 | 4 | It is a series of words where each word is formed by concatenation 5 | of last three words. 6 | where : 7 | T(1) = 1 8 | T(2) = 12 9 | T(3) = 1213 10 | 11 | T(n) = T(n-1) + T(n-2) + T(n-3) 12 | */ 13 | 14 | #include 15 | using namespace std; 16 | 17 | string tribonacci(int n) { 18 | if (n == 1) { 19 | return "1"; 20 | } 21 | if (n == 2) { 22 | return "12"; 23 | } 24 | if (n == 3) { 25 | return "1213"; 26 | } 27 | return (tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3)); 28 | } 29 | 30 | int main() { 31 | int n; 32 | cout<<"Enter N: "; 33 | cin>>n; 34 | cout<<"The tribonacci word at position "<= 0 and pattern[j] == text[s + j]): 23 | j -= 1 24 | 25 | if j < 0: 26 | print("Pattern match at shift = {0}".format(s)) 27 | if (s + len_pattern < len_text): 28 | s += len_pattern - badchar[ord(text[s + len_pattern])] 29 | else: 30 | s += 1 31 | else: 32 | s += max(1, j - badchar[ord(text[s + j])]) 33 | 34 | text = "ABAAABCD" 35 | pattern = "ABC" 36 | 37 | search(text, pattern) -------------------------------------------------------------------------------- /Pascal_Triangle/Pascal_Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int num) 6 | { 7 | int fact; 8 | for(fact = 1; num > 1; num--) 9 | fact *= num; 10 | return fact; 11 | } 12 | 13 | int main() 14 | { 15 | int num, i, j; 16 | cout<<"Enter the numbers of rows "; 17 | cin>>num; 18 | 19 | for(i = 0; i < num; i++) 20 | { 21 | for(j = 0; j < num-i; j++) 22 | cout<<" "; 23 | 24 | for(j = 0; j <= i; j++) 25 | cout<<" "< accuracy) 16 | { 17 | num = (num + guess)/2; 18 | guess = number / num; 19 | } 20 | return num; 21 | } 22 | 23 | //To check if function is working 24 | var n = 3; 25 | var result = square_root(n).toFixed(6); 26 | console.log(result); 27 | 28 | //Output- 1.732051 29 | -------------------------------------------------------------------------------- /Geometric_Progression/Sum_Of_Infinite_GP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | SUM OF INFINITE GEOMETRIC PROGRESSION 3 | 4 | Any sequence of elements where the quotient between any two 5 | consecutive elements is equal is termed to be in G.P. 6 | The nth term of an G.P. is defined as (a * r^(n-1)) 7 | where a is the first element of the G.P. 8 | r is the common ratio of the G.P. 9 | n is the number of term. 10 | 11 | The Sum of infinite series of a G.P. is defined as : 12 | a / (1 - r), where -1 < r < 1 13 | */ 14 | 15 | #include 16 | using namespace std; 17 | 18 | int main() { 19 | cout<<"Enter the First Term of G.P."; 20 | float a; 21 | cin>>a; 22 | cout<<"Enter the common ratio"; 23 | float r; 24 | cin>>r; 25 | cout<<"The sum of infinite G.P. is "<<(a * 1.0 / (1 - r)); 26 | } 27 | 28 | /* 29 | INPUT : a = 5 30 | r = 0.5 31 | OUTPUT : The sum of infinite G.P. is 10.0 32 | */ 33 | -------------------------------------------------------------------------------- /Interpolation_Search/Interpolation_Search.py: -------------------------------------------------------------------------------- 1 | def interpolation(list, search_item): 2 | high = len(list) - 1 3 | low = 0 4 | 5 | while low <= high and search_item >= list[low] and search_item <= list[high]: 6 | rise = high - low 7 | run = list[high] - list[low] 8 | x = search_item - list[low] 9 | pos = low + int(rise / run) * x; 10 | 11 | if list[pos] == search_item: 12 | return pos 13 | elif search_item < list[pos]: 14 | high = pos - 1 15 | else: 16 | low = pos + 1 17 | 18 | return -1 19 | 20 | list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 21 | search_item = 8 22 | index = interpolation(list, search_item) 23 | 24 | if index == -1: 25 | print("Element not found") 26 | else: 27 | print("Found at position " + str(index)) 28 | 29 | 30 | ''' Output 31 | 32 | Found at position 7 33 | 34 | ''' 35 | -------------------------------------------------------------------------------- /Rotation_Of_Numbers/Rotation_Of_Numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Function to rotations of a number 5 | void rotation(int num) 6 | { 7 | int temp = num; 8 | int digits = 0; 9 | 10 | // Finding the number of digits 11 | while (temp > 0) 12 | { 13 | digits++; 14 | temp /= 10; 15 | } 16 | 17 | int power = pow(10, digits - 1); 18 | 19 | // Rotations of the number 20 | for (int i = 0; i < digits - 1; i++) 21 | { 22 | int lastDigit = num % 10; 23 | int x = num / 10; 24 | num = lastDigit * power + x; 25 | cout<>num; 34 | rotation(num); 35 | return 0; 36 | } 37 | 38 | /* 39 | Input: 40 | 1234 41 | Output: 42 | 4123 3412 2341 43 | */ 44 | -------------------------------------------------------------------------------- /Subarray_Sum/Subarray_Sum.py: -------------------------------------------------------------------------------- 1 | ''' 2 | SUBARRAY SUM 3 | 4 | The task is to find sum of all possible subarrays of an array. 5 | This can be done by finding all subarray by a brute force solution 6 | and then finding combined sum of them. 7 | An efficient solution gets us to the theory that every element 8 | contributes (i+1)*(n-1) times in the required sum. We use this 9 | formula for the computation of sum. 10 | ''' 11 | 12 | print("Enter the size of array : ") 13 | num = int(input()) 14 | a = [] 15 | print("Enter array elements") 16 | for i in range(0, num): 17 | a.append(int(input())) 18 | ans = 0 19 | for i in range(0, num): 20 | ans = ans + (a[i] * (i + 1) * (num - i)) 21 | print("The sum of all subarrays is :", ans) 22 | 23 | ''' 24 | Input : num = 4 25 | Array = [-1, -4, -5, 20] 26 | Output : 27 | The sum of all subarrays is : 22 28 | ''' 29 | -------------------------------------------------------------------------------- /Sum_Of_Digits/Sum_Of_Digits.cpp: -------------------------------------------------------------------------------- 1 | /* The sum of digits of a number is the mathematical addition 2 | of the digits extracted from the number. 3 | Example: number = 12345, sum = 1 + 2 + 3 + 4 + 5 = 15 4 | number = 1005, sum = 1 + 0 + 0 + 5 = 6 */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | int sumOfDigits(int N) 10 | { 11 | int a, sum = 0; 12 | while (N > 0) 13 | { 14 | a = N % 10; 15 | sum += a; 16 | N /= 10; 17 | } 18 | return sum; 19 | } 20 | 21 | int main() 22 | { 23 | int N; 24 | cout<<"Enter number:"<>N; 26 | int ans = sumOfDigits(N); 27 | cout<<"The sum of digits of "< list[j + 1]: 8 | list[j], list[j + 1] = list[j + 1], list[j] 9 | flag = 1 10 | if flag == 0: # if this is true , then it means that no swapping has been done in either the first pass or any other pass , break out of loop 11 | break 12 | 13 | # function to print list 14 | def Print_list(list): 15 | for i in range(0, len(list)): 16 | print(list[i], end = " ") 17 | print() 18 | 19 | list = [2, 4, 3, 1, 6, 8, 4] 20 | Bubble_Sort(list) 21 | Print_list(list) 22 | 23 | # Output 24 | # 1 2 3 4 4 6 8 25 | -------------------------------------------------------------------------------- /Leap_Year/Leap_Year.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Leap year is an year with 366 days. Leap Year should be divisible by 4. 3 | Also it should be divisible by 400 and not divisible by 100. 4 | ''' 5 | 6 | def checkLeapYear(year): 7 | if year % 400 == 0: 8 | return 1 9 | 10 | if year % 100 == 0: 11 | return 0 12 | 13 | if year % 4 == 0: 14 | return 1 15 | 16 | return 0 17 | 18 | year1 = int(input("Enter the first year:\n")) 19 | year2 = int(input("Enter the second year:\n")) 20 | print("The leap years between %d and %d are:" % (year1, year2)) 21 | for year in range(year1, year2): 22 | if checkLeapYear(year) == 1: 23 | print(year) 24 | 25 | ''' 26 | Input: 27 | 2000 28 | 2050 29 | 30 | Output: 31 | The leap years between 2000 and 2050 are: 32 | 2000 33 | 2004 34 | 2008 35 | 2012 36 | 2016 37 | 2020 38 | 2024 39 | 2028 40 | 2032 41 | 2036 42 | 2040 43 | 2044 44 | 2048 45 | ''' 46 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Function for linear search 6 | int linearSearch(int array[], int size, int desired) 7 | { 8 | for(int i = 0; i < size; i++) 9 | { 10 | // return position if element is found 11 | if(array[i] == desired) 12 | return i; 13 | } 14 | 15 | return -1; 16 | } 17 | 18 | // Driver Function 19 | int main() 20 | { 21 | int num; 22 | cin >> num; 23 | int arr[num]; 24 | for (int i = 0; i < num; i++) { 25 | cin >> arr[i]; 26 | } 27 | int desired; 28 | cin >> desired; 29 | if(linearSearch(array, num, desired) != -1) 30 | cout << "Found" << endl; 31 | else 32 | cout << "Number not found" << endl; 33 | return 0; 34 | } 35 | 36 | /* 37 | Input : 38 | num = 5 39 | arr = [1,4,5,6,3] 40 | desired = 3 41 | 42 | Output : 43 | Number not found 44 | */ 45 | -------------------------------------------------------------------------------- /Linear_Search/Linear_Search.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // LinearSearch searches the complete slice and 9 | // returns the index of the desired element if found in the slice. 10 | func linearSearch(a []int, d int) int { 11 | for i := range a { 12 | if a[i] == d { 13 | return i 14 | } 15 | } 16 | return -1 17 | } 18 | 19 | func main() { 20 | var num int 21 | fmt.Scan(&num) 22 | a := []int{} 23 | for i := 0; i < num; i++ { 24 | var x int 25 | fmt.Scan(&x) 26 | a = append(a, x) 27 | } 28 | var desired int 29 | fmt.Scan(&desired) 30 | var index = linearSearch(a, desired) 31 | if index != -1 { 32 | fmt.Println("found 7 at ", strconv.Itoa(index)) 33 | } else { 34 | fmt.Println("Number not found") 35 | } 36 | } 37 | 38 | /* 39 | Input : 40 | num = 5 41 | arr = [1,4,5,6,3] 42 | desired = 3 43 | 44 | Output : 45 | Number not found 46 | */ 47 | -------------------------------------------------------------------------------- /Modular_Exponentiation/Recursive_Modular_Exponentiation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //recursive mechanism 4 | long long power(long int base,long long exponent,long long p) 5 | { 6 | long long holder; 7 | if(base==0) 8 | return 0; 9 | if ( exponent == 0) 10 | return 1; 11 | 12 | if(exponent%2==0) 13 | { 14 | holder=power(base,exponent/2,p); 15 | holder=(holder*holder)%p; 16 | } 17 | else 18 | { 19 | holder=base%p; 20 | holder=(holder*power(base,exponent-1,p)%p)%p; 21 | } 22 | return (holder+p)%p; 23 | } 24 | 25 | int main() 26 | { 27 | long long base,exponent,p; 28 | scanf("%lli %lli %lli",&base,&exponent,&p); 29 | printf("The value of given expression is %lli",power(base,exponent,p)); 30 | return 0; 31 | } 32 | 33 | /* 34 | output: 35 | base : 2 36 | exponent:4 37 | output :The result of given expression is 1 38 | */ 39 | -------------------------------------------------------------------------------- /Tree_Inorder_Traversal/Tree_Inorder_Traversal.php: -------------------------------------------------------------------------------- 1 | left = null; 10 | $this->right = null; 11 | $this->value = $key; 12 | } 13 | } 14 | 15 | function inOrder($root){ 16 | if (!empty($root)){ 17 | inOrder($root->left); 18 | echo $root->value . " "; 19 | inOrder($root->right); 20 | } 21 | } 22 | 23 | $root = new Node(1); 24 | $root->left = new Node(2); 25 | $root->right = new Node(3); 26 | $root->left->left = new Node(4); 27 | $root->left->right = new Node(5); 28 | $root->right->left = new Node(6); 29 | $root->right->right = new Node(7); 30 | 31 | echo "inOrder traversal of tree is "; 32 | inOrder($root); 33 | echo "\n"; 34 | 35 | /** Output 36 | * 37 | * Pre Order traversal of tree is 4 2 5 1 6 3 7 38 | * 39 | */ 40 | -------------------------------------------------------------------------------- /Fermat_Little_Theorem/Fermat_Little_Theorem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int power(int a, unsigned int n, int p) 4 | { 5 | int res=1; 6 | a=a%p; 7 | while(n>0) 8 | { 9 | if(n & 1) 10 | res=(res*a)%p; 11 | n=n>>1; 12 | a=(a*a)%p; 13 | } 14 | return res; 15 | } 16 | bool isPrime(unsigned int n, int k) 17 | { 18 | if (n <= 1 || n == 4) return false; 19 | if (n <= 3) return true; 20 | 21 | while (k>0) 22 | { 23 | int a = 2 + rand()%(n-4); 24 | if (power(a, n-1, n) != 1) 25 | return false; 26 | k--; 27 | } 28 | return true; 29 | 30 | } 31 | int main() 32 | { 33 | int k = 3; 34 | isPrime(100, k)? cout << " true\n": cout << " false\n"; 35 | isPrime(111, k)? cout << " true\n": cout << " false\n"; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Fermat_Little_Theorem/Fermat_Little_Theorem.py: -------------------------------------------------------------------------------- 1 | # Finding greatest common divisor using Euclidean Algorithm 2 | def gcdFunction(a, b): 3 | if a == 0: 4 | return b 5 | 6 | return gcdFunction(b % a, a) 7 | 8 | def power(x, y, m): 9 | if y == 0: 10 | return 1 11 | 12 | p = power(x, int(y / 2), m) % m 13 | p = (p * p) % m 14 | 15 | if y % 2 == 0: 16 | return p 17 | else: 18 | return (x * p) % m 19 | 20 | def modInverse(a, m): 21 | gcd = gcdFunction(a, m) 22 | 23 | if gcd != 1: 24 | return -1 25 | else: 26 | return power(a, m - 2, m) 27 | 28 | 29 | a = 3 30 | m = 11 31 | 32 | ans = modInverse(a, m) 33 | 34 | if ans == -1: 35 | print("Inverse doesn't exist") 36 | else: 37 | print("Modular multiplicative inverse is " + str(ans)) 38 | 39 | 40 | ''' Output 41 | 42 | Modular multiplicative inverse is 4 43 | 44 | ''' 45 | -------------------------------------------------------------------------------- /Power_Of_Two/Power_Of_Two.c: -------------------------------------------------------------------------------- 1 | /* 2 | To check whether a number is a power of two or not, 3 | we do bitwise "and" of number and number - 1, if the 4 | result is zero the number is a power of two. 5 | Example: number = 4, then 4 & 3 i.e 100 && 011 is 0 6 | 16 & 15 i.e 10000 & 01111 = 0 7 | This is true for all powers of two. 8 | */ 9 | 10 | #include 11 | 12 | int isPowerOfTwo(int x) 13 | { 14 | return x && (!(x & (x - 1))); 15 | } 16 | 17 | int main() 18 | { 19 | int num; 20 | printf("Enter a number to check:\n"); 21 | scanf("%d", &num); 22 | if (isPowerOfTwo(num)) 23 | printf("The number is a power of two"); 24 | else 25 | printf("The number is not a power of two"); 26 | return 0; 27 | } 28 | 29 | /* 30 | Input: 31 | 64 32 | 33 | Output: 34 | The number is a power of two 35 | 36 | Input: 37 | 5 38 | 39 | Output: 40 | The number is not a power of two 41 | */ 42 | -------------------------------------------------------------------------------- /Reverse_Of_Number/Reverse_Of_Number.cpp: -------------------------------------------------------------------------------- 1 | /* Reverse of a number is the number obtained when we read 2 | the original number backwards. 3 | Example: number = 12345, reverse = 54321 4 | number = 1005, reverse = 5001 */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | // Function to reverse a number 10 | int reverse(int N) 11 | { 12 | int a, rev = 0; 13 | while (N > 0) 14 | { 15 | a = N % 10; 16 | rev = rev * 10 + a; 17 | N /= 10; 18 | } 19 | return rev; 20 | } 21 | 22 | int main() 23 | { 24 | int N; 25 | cout<<"Enter number to reverse:"<>N; 27 | int ans = reverse(N); 28 | cout<<"The reverse of "<left = null; 10 | $this->right = null; 11 | $this->value = $key; 12 | } 13 | } 14 | 15 | function preOrder($root){ 16 | if (!empty($root)){ 17 | echo $root->value . " "; 18 | preOrder($root->left); 19 | preOrder($root->right); 20 | } 21 | } 22 | 23 | $root = new Node(1); 24 | $root->left = new Node(2); 25 | $root->right = new Node(3); 26 | $root->left->left = new Node(4); 27 | $root->left->right = new Node(5); 28 | $root->right->left = new Node(6); 29 | $root->right->right = new Node(7); 30 | 31 | echo "preOrder traversal of tree is "; 32 | preOrder($root); 33 | echo "\n"; 34 | 35 | /** Output 36 | * 37 | * Pre Order traversal of tree is 1 2 4 5 3 6 7 38 | * 39 | */ 40 | -------------------------------------------------------------------------------- /Geometric_Progression/Sum_Of_Infinite_GP.c: -------------------------------------------------------------------------------- 1 | /* 2 | SUM OF INFINITE GEOMETRIC PROGRESSION 3 | 4 | Any sequence of elements where the quotient between any two 5 | consecutive elements is equal is termed to be in G.P. 6 | The nth term of an G.P. is defined as (a * r^(n-1)) 7 | where a is the first element of the G.P. 8 | r is the common ratio of the G.P. 9 | n is the number of term. 10 | 11 | The Sum of infinite series of a G.P. is defined as : 12 | a / (1 - r), where -1 < r < 1 13 | */ 14 | 15 | #include 16 | #include 17 | 18 | int main() { 19 | printf("Enter the First Term of G.P."); 20 | float a; 21 | scanf("%f", &a); 22 | printf("Enter the common ratio"); 23 | float r; 24 | scanf("%f", &r); 25 | printf("The sum of infinite G.P. is %.1f", (a * 1.0/ (1 - r))); 26 | } 27 | 28 | /* 29 | INPUT : a = 5 30 | r = 0.5 31 | OUTPUT : The sum of infinite G.P. is 10.0 32 | */ 33 | -------------------------------------------------------------------------------- /Merge_Sort/Merge_Sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func merge(left, right []int) []int { 6 | result := make([]int, len(right)+len(left)) 7 | i, j := 0, 0 8 | for i < len(right) && j < len(left) { 9 | if right[i] <= left[j] { 10 | result[i+j] = right[i] 11 | i += 1 12 | } else { 13 | result[i+j] = left[j] 14 | j += 1 15 | } 16 | } 17 | for i < len(right) { 18 | result[i+j] = right[i] 19 | i += 1 20 | } 21 | for j < len(left) { 22 | result[i+j] = left[j] 23 | j += 1 24 | } 25 | return result 26 | } 27 | 28 | func MergeSort(array []int) []int { 29 | if len(array) <= 1 { 30 | return array 31 | } 32 | mid := len(array) / 2 33 | right := MergeSort(array[:mid]) 34 | left := MergeSort(array[mid:]) 35 | return merge(right, left) 36 | } 37 | 38 | func main() { 39 | array := []int{2, 4, 3, 1, 6, 8, 4} 40 | result := MergeSort(array) 41 | fmt.Println(result) 42 | } 43 | -------------------------------------------------------------------------------- /Number_Conversion/C/Decimal_To_Hexadecimal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num; 6 | scanf("%d", &num); 7 | 8 | // To store hexadecimal number 9 | char hexa[100]; 10 | 11 | int i = 0, j; 12 | while (num != 0) 13 | { 14 | int temp = 0; 15 | temp = num % 16; 16 | 17 | // Checking if temp < 10; if it then it is a letter 18 | if (temp < 10) 19 | { 20 | hexa[i] = temp + 48; 21 | i++; 22 | } 23 | else 24 | { 25 | hexa[i] = temp + 55; 26 | i++; 27 | } 28 | 29 | num = num / 16; 30 | } 31 | 32 | for (j = i - 1; j >= 0; j--) 33 | printf("%c", hexa[j]); 34 | 35 | return 0; 36 | } 37 | 38 | /* 39 | Input: 40 | 1234 41 | Output: 42 | 4D2 43 | */ 44 | -------------------------------------------------------------------------------- /Perfect_Number/Perfect_Number.java: -------------------------------------------------------------------------------- 1 | // Perfect number is a positive integer that is equal to the sum of its proper divisors. 2 | 3 | import java.util.Scanner; 4 | public class Perfect 5 | { 6 | public static void main(String[] args) 7 | { 8 | int sum = 0; 9 | Scanner input = new Scanner(System.in); 10 | int num = input.nextInt(); 11 | 12 | // Calculating the sum of divisors 13 | for (int i = 1; i < num; i++) 14 | { 15 | if (num % i == 0) 16 | { 17 | sum = sum + i; 18 | } 19 | } 20 | if (sum == num) 21 | { 22 | System.out.println("It is a perfect number"); 23 | } 24 | else 25 | { 26 | System.out.println("It is not a perfect number"); 27 | } 28 | } 29 | } 30 | 31 | // INPUT: 28 32 | // OUTPUT: It is a perfect number 33 | -------------------------------------------------------------------------------- /Spiral_Array/README.md: -------------------------------------------------------------------------------- 1 | # Spiral Array 2 | 3 | Spiral Array Algorithm traverses an array in a spiral order. 4 | 5 | ## Example 6 | 7 | ![Spiral Array](http://1.bp.blogspot.com/-CD9C_7oeI3I/VgwL3AO-IeI/AAAAAAAACBc/EG-WAf-y_7E/s1600/spiral-circular-matrix.jpg) 8 | 9 | ## Algorithm 10 | 11 | ``` 12 | Step-1 : Traverse the topmost row of array in left to right. 13 | Step-2 : Increment the top variable. 14 | Step-3 : Traverse the rightmost column of array from top to bottom. 15 | Step-4 : Decrement the right variable. 16 | Step-5 : Traverse the bottommost row of array from right to left. 17 | Step-6 : Decrement the bottom variable. 18 | Step-7 : Traverse the leftmost row of array from bottom. 19 | Step-8 : Increment the left variable. 20 | ``` 21 | 22 | ## Sample I/O 23 | 24 | ### INPUT 25 | 26 | ``` 27 | 3 3 28 | 1 2 3 29 | 4 5 6 30 | 7 8 9 31 | ``` 32 | 33 | ### OUTPUT 34 | 35 | ``` 36 | 1 2 3 6 9 8 7 4 5 37 | ``` 38 | --------------------------------------------------------------------------------