9 Exp.No. 1 F inding prime and composite numbers Date: Program 1 : Find 7 is prime or composite number CODE: n = 7; flag = 0; for i = 1:n if modulo(n, i) == 0 then flag = flag + 1; end end if flag == 2 then disp(string(n) + ' is a prime number'); el se disp(string(n) + ' is a composite number'); end OUTPUT: 10 Program 2 : Find 95 is prime or composite number CODE: n = 95 ; flag = 0; for i = 1:n if modulo(n, i) == 0 then flag = flag + 1; end end if flag == 2 then disp(s tring(n) + ' is a prime number'); else disp(string(n) + ' is a composite number'); end OUTPUT: 11 Program 3: Find 1573 is prime or composite number CODE: n = 1573 ; flag = 0; for i = 1:n if modulo(n, i) == 0 then flag = f lag + 1; end end if flag == 2 then disp(string(n) + ' is a prime number'); else disp(string(n) + ' is a composite number'); end OUTPUT: 12 Exp.No. 2 Find gcd by using Euclid algorithm Date: Program 1: Find gcd (48,18) CODE: / / Define the two numbers num1 = 48; num2 = 18; // Implement the Euclidean algorithm while num2 ~= 0 temp = num2; num2 = modulo(num1, num2); num1 = temp; end // The GCD is stored in num1 gcd = num1; // Display the result disp(gcd); OUTPUT: 13 Program: 2 Find gcd (60,35) CODE: // Define the two numbers num1 = 60 ; num2 = 35 ; // Implement the Euclidean algorithm while num2 ~= 0 temp = num2; num2 = modulo(num1, num2); num1 = temp; end // The GCD is stored in num1 gcd = n um1; // Display the result disp(gcd); OUTPUT: 14 Program 3: Find gcd (2500,250) CODE: // Define the two numbers num1 = 2500 ; num2 = 250 ; // Implement the Euclidean algorithm while num2 ~= 0 temp = num2; num2 = modulo(num1, nu m2); num1 = temp; end // The GCD is stored in num1 gcd = num1; // Display the result disp(gcd); OUTPUT: 15 Exp.No. 3 F inding LCM Date: Program 1: i) Find the LCM for 12 and 15 CODE: // Define the two n umbers num1 = 12; num2 = 15; // Implement the Euclidean algorithm to find GCD while num2 ~= 0 temp = num2; num2 = modulo(num1, num2); num1 = temp; end // The GCD is stored in num1 gcd = num1; // Calculate the LCM using the formula LCM = (nu m1 * num2) / GCD(num1, num2) lcm = (12 * 15) / gcd; // Display the result disp(lcm); OUTPUT: 16 Program 2: Find the LCM for 10 and 35 CODE: // Define the two numbers num1 = 10; num2 = 35; // Implement the Euclidean algorithm to find GCD while num2 ~= 0 temp = num2; num2 = modulo(num1, num2); num1 = temp; end // The GCD is stored in num1 gcd = num1; // Calculate the LCM using the formula LCM = (num1 * num2) / GCD(num1, num2) lcm = (10 * 35) / gcd; // Display the result disp(lcm) ; OUTPUT: 17 Exp.No. 4 So lving congruences Date: Program 1: Solve: ) 7 (mod 3 5 x OUTPUT: 18 Exp.No. 5 Implement CRT algorithm Date: Program 1: Solve: ) 5 (mod 3 ) 3 (mod 2 x x OUTPUT: 19 Exp.No. 6 Solving 2x2 linear system Date: Program 1: Solve: ) 13 (mod 2 7 6 ) 13 (mod 10 6 5 y x y x OUTPUT: 20 Exp.No. 7 Implement RSA Date: Program 1: In a RSA cryptosystem, a participant uses two prime numbers p= 61 and q =53 t o generate his public and private keys. If the private key is 17, then how will the text message ASCII 'H' be encrypted using the public key? Programming Code: // Helper function to calculate Greatest Common Divisor (GCD) using Euclidean algorithm functi on result = gcd(a, b) while b <> 0 temp = b; b = modulo(a, b); a = temp; end result = a; endfunction // Helper function for modular exponentiation (c = m^e mod n) function result = modPow(base, exp, m) result = 1; base = modulo(base, m); while exp > 0 if modulo(exp, 2) == 1 then result = modulo(result * base, m); end base = modulo(base * base, m); exp = int(exp / 2); end endfunction // Helper function to find m odular multiplicative inverse (d*e mod phi = 1) using extended Euclidean algorithm function result = modInverse(e, phi) m0 = phi; t0 = 0; t1 = 1; q = 0; r = 0; while e > 1 q = int(e / phi); r = modulo(e, phi); e = ph i; phi = r; t = t0 - q * t1; t0 = t1; t1 = t; end 21 if t1 < 0 then t1 = t1 + m0; end result = t1; endfunction // RSA Key Generation Example (using small primes for demonstration) p = 61; // First prime q = 53; // Second prime n = p * q; // Modulus phi = (p - 1) * (q - 1); // Euler's totient e = 17; // Public exponent, common choice // Ensure gcd(e, phi) == 1, which it is for 17 and 3120 d = modInverse(e, phi); // Private exponent printf("Public Key ( e, n): (%d, %d) \ n", e, n); printf("Private Key (d, n): (%d, %d) \ n", d, n); // Encryption and Decryption message = 72; // Example message (ASCII 'H') // Encryption: c = m^e mod n ciphertext = modPow(message, e, n); printf("Encrypted Ciphertext: %d \ n", cip hertext); // Decryption: m = c^d mod n decrypted_message = modPow(ciphertext, d, n); printf("Decrypted Message: %d \ n", decrypted_message); OUTPUT: 22 Exp.No. 8 Implement Knapsack Algorithm Date: Program 1: P rogram Code: function [max_profit, selected_items] = knapSack_DP(capacity, weights, values) n = length(values); // Create a 2D matrix (n+1 x capacity+1) to store maximum values // Initialize all elements to 0 V = zeros(n+1, capacity+1); // Build the V table in a bottom - up manner for i = 1:n for w = 1:capacity if weights(i) <= w then // Max of (not including the current item, including the current item) V(i+1, w+1) = max(V(i, w+1), values(i) + V(i, w+1 - weights(i)) ); else // If the item's weight is more than current capacity, it cannot be included V(i+1, w+1) = V(i, w+1); end end end 23 // The maximum profit is in the bottom - right corner of the table max_profit = V(n+1, capacity+1); // Trace back the items included in the knapsack selected_items = zeros(1, n); w = capacity; for i = n: - 1:1 if V(i+1, w+1) ~= V(i, w+1) then / / The current item was included selected_items(i) = 1; w = w - weights(i); // Reduce the remaining capacity end end endfunction // Input data W = 5; // Knapsack capacity val = [100, 20, 60, 40]; // Values of the items wt = [3, 2, 4, 1]; // Weights of the items // Execute the knapsack algorithm function [totalProfit, items] = knapSack_DP(W, wt, val); // Display the results disp("Maximum Profit: " + string(totalProfit)); disp("Selected Items (1 for included, 0 for not):") ; disp(items); OUTPUT 24 Program 2 Suppose you have a knapsack with a maximum weight capacity of W= 50. You have n = 3 items, each with a specific weight and value, as detailed below: Item Value Weight 1 60 10 2 100 20 3 120 30 The goal is to determine which items to put into knapsack to maximize the total value without exceeding the weight capacity. Program Code: function max_val = knapsack01(W, weights, values, n) // Initialize DP table with zeros // Rows: items (0 to n), Columns: capacity (0 to W) dp = zeros(n + 1, W + 1); for i = 2:n + 1 for w = 1:W + 1 current_wt = weights(i - 1); current_val = values(i - 1); if current_wt <= (w - 1) then / / Max of (including the item) or (excluding the item) dp(i, w) = max(current_val + dp(i - 1, w - current_wt), dp(i - 1, w)); else // Item too heavy, carry forward previous best dp(i, w) = dp(i - 1, w); end end end max_val = dp(n + 1, W + 1); endfunction // --- Driver Code --- values = [60, 100, 120]; weights = [10, 20, 30]; W = 50; n = length(values); result = knapsack01(W, weights, values, n); printf("Maximum value in Knapsac k = %d \ n", result); OUTPUT 25 Exp.No. 9 F i n d i n g Fermat factorization Date: Program 1: Finding the Fermat factorization of (i) 5959 and (ii) 23247 Program Code: function [p, q] = fermat_factor(N) // Check if N is an odd positive integer if modulo(N, 2) == 0 | N <= 0 then disp("Fermat factorization only works for odd positive integers.") p = %nan q = %nan return end a = ceil(sqrt(N)); b2 = a*a - N; // Iterate until b2 becomes a perfect square while modulo(sqrt(b2), 1) ~= 0 a = a + 1; b2 = a*a - N; // Equivalently: b2 = b2 + 2*a + 1 end b = sqrt(b2); p = a - b; q = a + b; disp("The factors of " + string(N) + " are " + string(p) + " and " + string(q) + ".") endfunc tion // Example usage: N = 5959; // Factors are 59 and 101, which are close [p, q] = fermat_factor(N); N2 = 23247; // Factors are 128 and 189 [p2, q2] = fermat_factor(N2); OUTPUT 26 Exp.No. 10 I m p l e m e n t Pollard Rho method Date: Program 1: Find the factors of 187 using Pollard Rho method Program Code: function d = pollardRho(n) if modulo(n, 2) == 0 then, d = 2; return; end x = 2; y = 2; d = 1; c = 1; funcprot(0) // Polynomial function f(x) = (x^2 + c) mod n function res = f(x, c, n) res = modulo(x*x + c, n); endfunction while d == 1 x = f(x, c, n); // Tortoise (1 step) y = f(f(y, c, n), c, n); // Hare (2 steps) // Use brackets [ ] to pass values as a single vector to gcd() d = gcd([abs(x - y), n]); // If the algorithm fails (returns n), restart with a different constant c if d == n then c = c + 1; x = 2; y = 2; d = 1; end end endfunction // Main execution n = 187; factor = pollardRho(n); m = n / factor; printf("Factors of %d are: %d and %d \ n", n, factor, m); OUTPUT