Dstl Solution
Dstl Solution
LIST OF EXPERIMENTS
x=3
output:
The value of the expression for x = 3 is: 9
b) Write a Python program to find the roots of a quadra c func on
import cmath # Import cmath for complex number calcula ons
Args:
Returns:
"""
discriminant = (b**2) - 4 * (a * c)
if discriminant >= 0:
else:
print("-" * 20)
print("-" * 20)
print(f"Root 1: {root1}")
print(f"Root 2: {root2}")
print("-" * 20)
output:
Root 1: (3+0j)
Root 2: (2+0j)
ax^2 + bx + c = 0
Quadra c Formula:
Steps to Solve:
Nature of Roots:
Example:
Solve x^2 + 5x + 6 = 0.
1. a = 1, b = 5, c = 6.
2. Δ = 5^2 - 4(1)(6) = 25 - 24 = 1.
3. Since Δ > 0, two dis nct real roots exist.
x = (-5 ± 1) / 2
x = -2 or x = -3
Roots: x = -2, x = -3
def factorial(n):
if n < 0:
elif n == 0 or n == 1:
return 1
else:
result = 1
result *= i
return result
try:
result = factorial(number)
except ValueError:
output:
Enter a non-nega ve integer: 5
The factorial of 5 is: 120
def sum_of_natural_numbers(n):
if n < 0:
else:
try:
result = sum_of_natural_numbers(number)
except ValueError:
OUTPUT:
def cube_sum_of_natural_numbers(n):
if n < 0:
else:
result = cube_sum_of_natural_numbers(number)
print(f"The sum of the cubes of the first {number} natural numbers is: {result}")
except ValueError:
OUTPUT:
Enter a non-nega ve integer (n): 3
The sum of the cubes of the first 3 natural numbers is: 36
3. Implementa on of various set opera ons (union, intersec on, difference, symmetric difference,
Power set, cardinality).
def cardinality(s):
return len(s)
# Example sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(f"Set 1: {set1}")
print(f"Set 2: {set2}\n")
3. Difference: Returns elements that are in the first set but not in the second.
def difference(set1, set2):
return set1 - set2
4. Symmetric Difference: Returns elements that are in either of the sets but not in both.
def symmetric_difference(set1, set2):
return set1 ^ set2
5. Power Set: Generates the power set, which is the set of all subsets of a given set.
def power_set(s):
power_set_list = list(chain.from_iterable(combina ons(s, r) for r in range(len(s) + 1)))
return [set(subset) for subset in power_set_list]
Example Output:
Set 1: {1, 2, 3, 4}
Set 2: {3, 4, 5, 6}
Union: {1, 2, 3, 4, 5, 6}
Intersec on: {3, 4}
Difference (Set1 - Set2): {1, 2}
Symmetric Difference: {1, 2, 5, 6}
Power Set of Set 1: [set(), {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {1, 2, 3}, {1,
2, 4}, {1, 3, 4}, {2, 3, 4}, {1, 2, 3, 4}]
Cardinality of Set 1: 4
Cardinality of Set 2: 4
return False
return True
# Example usage
# Define the set of elements
elements = {1, 2, 3}
rela on_reflexive = {(1, 1), (2, 2), (3, 3), (1, 2), (2, 3)}
rela on_non_reflexive = {(1, 1), (2, 2), (1, 2), (2, 3)}
# Check reflexivity
Output:
Is the rela on {(1, 1), (2, 2), (3, 3), (1, 2), (2, 3)} reflexive? True
Is the rela on {(1, 1), (2, 2), (1, 2), (2, 3)} reflexive? False
return False
return True
# Example usage
OUTPUT:
Is the rela on {(1, 2), (2, 1), (3, 3)} symmetric? True
Is the rela on {(1, 2), (2, 3), (3, 1)} symmetric? False
return False
return True
# Example usage
print(f"Is the rela on {rela on_transi ve} transi ve? {is_transi ve(rela on_transi ve)}")
print(f"Is the rela on {rela on_non_transi ve} transi ve? {is_transi ve(rela on_non_transi ve)}")
OUTPUT:
Is the rela on {(1, 2), (2, 3), (1, 3)} transi ve? True
Is the rela on {(1, 2), (2, 3), (3, 1)} transi ve? False
5. Write program to generate recursive sequence of a closed formula and also calculate its value at
par cular non nega ve integer recursively for the following:
a) Polynomial 2n
def polynomial_2n(n):
if n < 0:
if n == 0:
else:
# Example usage
if __name__ == "__main__":
n = 5 # You can change this value to test with other non-nega ve integers
result = polynomial_2n(n)
OUTPUT:
def fibonacci(n):
"""Calculate the nth Fibonacci number recursively."""
if n < 0:
if n == 0:
elif n == 1:
else:
def factorial(n):
if n < 0:
if n == 0 or n == 1:
else:
# Example usage
if __name__ == "__main__":
fib_n = 6 # You can change this value to test with other non-nega ve integers
fib_value = fibonacci(fib_n)
fact_n = 5 # You can change this value to test with other non-nega ve integers
fact_value = factorial(fact_n)
a. Perform + m (addi on modulo) and xm (mul plica on modulo)for a par cular set.
total = 0
return total
product = 1
return product
# Example usage
if __name__ == "__main__":
numbers = [5, 10, 15] # You can change this list to test with other sets of numbers
m = 7 # Modulus
OUTPUT:
for i in range(len(numbers)):
for j in range(len(numbers)):
print(f"Closure under addi on modulo {m} fails for: ({numbers[i]} + {numbers[j]}) % {m} =
{result}")
return False
print(f"Closure under addi on modulo {m} holds for the set: {numbers}")
return True
for i in range(len(numbers)):
for j in range(len(numbers)):
print(f"Closure under mul plica on modulo {m} fails for: ({numbers[i]} * {numbers[j]}) %
{m} = {result}")
return False
print(f"Closure under mul plica on modulo {m} holds for the set: {numbers}")
return True
# Example usage
if __name__ == "__main__":
numbers = [0, 1, 2, 3, 4] # You can change this list to test with other sets of numbers
m = 5 # Modulus
check_addi on_modulo_closure(numbers, m)
OUTPUT:
Closure under mul plica on modulo 5 holds for the set: [0, 1, 2, 3, 4]
def birthday_problem(n):
days_in_year = 365
if n > days_in_year:
return 1.0
probability_no_shared_birthdays = 1.0
for i in range(n):
probability_no_shared_birthdays *= (days_in_year - i) / days_in_year
probability_shared_birthday = 1 - probability_no_shared_birthdays
return probability_shared_birthday
# Example usage
if __name__ == "__main__":
# Number of people
n = 23
probability = birthday_problem(n)
print(f"The probability of at least two people sharing a birthday in a group of {n} people is:
{probability:.4f}")
for i in range(1, 101, 10): # Tes ng for groups of 1, 11, 21, ..., 91
probability = birthday_problem(i)
OUTPUT:
The probability of at least two people sharing a birthday in a group of 23 people is: 0.5073
for a in elements:
return False
return True
return False
return True
return False
return True
if __name__ == "__main__":
elements = {1, 2, 3} # You can change this set to test with other elements
rela on = {(1, 1), (2, 2), (3, 3), (1, 2), (2, 1)} # You can change this rela on to test
else:
OUTPUT:
for a in group:
for b in group:
return False
return True
for a in group:
for b in group:
for c in group:
return True
for e in group:
return e
return None
for a in group:
if not any(opera on(a, b) == iden ty and opera on(b, a) == iden ty for b in group):
return False
return True
for a in group:
for b in group:
return False
return True
return False
return False
iden ty = has_iden ty(group, opera on)
if iden ty is None:
return False
return False
return False
return True
# Example usage
if __name__ == "__main__":
group = {0, 1, 2, 3, 4} # You can change this set to test with other elements
else: