Programming Exercises - 003: User-Defined Functions and Recursion

Programming Exercises - 003: User-Defined Functions and Recursion

This set of exercises focuses on creating user-defined functions and using recursion to solve problems related to Physics, Mathematics, and Progressions. Each problem is accompanied by hints to guide students through the process of writing the programs.

1. Calculate the Gravitational Force

Question: Write a Python program named gravitational_force.py that calculates the gravitational force between two masses. The gravitational force is given by the formula F = G * (m1 * m2) / r^2, where G is the gravitational constant, m1 and m2 are the masses, and r is the distance between the centers of the two masses.

Hint: Define a function gravitational_force(m1, m2, r) that takes the masses and distance as input and returns the gravitational force.

2. Sum of an Arithmetic Progression (AP)

Question: Write a Python program named sum_of_ap.py to calculate the sum of the first n terms of an arithmetic progression (AP) using recursion. The sum of the first n terms is given by Sn = n/2 * (2a + (n-1)d), where a is the first term and d is the common difference.

Hint: Define a recursive function sum_of_ap(a, d, n) that calculates the sum.

3. Compute Kinetic Energy

Question: Create a Python program named kinetic_energy.py that calculates the kinetic energy of an object. The kinetic energy is given by KE = 0.5 * m * v^2, where m is the mass of the object and v is its velocity.

Hint: Write a function kinetic_energy(m, v) that takes mass and velocity as input and returns the kinetic energy.

4. Find the nth Term of a Geometric Progression (GP)

Question: Write a Python program named nth_term_gp.py that finds the nth term of a geometric progression (GP). The nth term of a GP is given by an = a * r^(n-1), where a is the first term and r is the common ratio.

Hint: Define a recursive function nth_term_gp(a, r, n) to compute the nth term.

5. Compute Potential Energy

Question: Write a Python program named potential_energy.py that calculates the potential energy of an object. The potential energy is given by PE = m * g * h, where m is the mass, g is the acceleration due to gravity, and h is the height.

Hint: Create a function potential_energy(m, g, h) that returns the potential energy.

6. Compute the Sum of a Geometric Series

Question: Write a Python program named sum_of_gp.py that calculates the sum of the first n terms of a geometric series. The sum is given by Sn = a * (1 - r^n) / (1 - r) for r != 1.

Hint: Use recursion in the function sum_of_gp(a, r, n) to compute the sum.

7. Calculate the Work Done by a Force

Question: Write a Python program named work_done.py that calculates the work done by a force acting over a distance. The work done is given by W = F * d, where F is the force and d is the distance.

Hint: Write a function work_done(F, d) to calculate the work.

8. Calculate the nth Fibonacci Number

Question: Write a Python program named fibonacci.py that calculates the nth Fibonacci number using recursion. The Fibonacci sequence is defined as F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n > 1.

Hint: Define a recursive function fibonacci(n) to find the nth Fibonacci number.

9. Calculate the Escape Velocity

Question: Create a Python program named escape_velocity.py that calculates the escape velocity of an object from a planet. The escape velocity is given by v = sqrt(2 * G * M / R), where G is the gravitational constant, M is the mass of the planet, and R is the radius of the planet.

Hint: Write a function escape_velocity(G, M, R) to compute the velocity.

10. Compute the nth Harmonic Number

Question: Write a Python program named harmonic_number.py that calculates the nth harmonic number using recursion. The nth harmonic number is given by Hn = 1 + 1/2 + 1/3 + ... + 1/n.

Hint: Use recursion in the function harmonic_number(n) to find the nth harmonic number.

11. Calculate the Electric Potential

Question: Create a Python program named electric_potential.py that calculates the electric potential at a point due to a charge. The electric potential is given by V = k * Q / r, where k is Coulomb's constant, Q is the charge, and r is the distance from the charge.

Hint: Define a function electric_potential(k, Q, r) to compute the potential.

12. Calculate the nth Triangular Number

Question: Write a Python program named triangular_number.py that calculates the nth triangular number using recursion. The nth triangular number is given by Tn = n * (n + 1) / 2.

Hint: Define a recursive function triangular_number(n) to calculate the nth triangular number.

13. Calculate the Electric Field

Question: Write a Python program named electric_field.py that calculates the electric field at a point due to a charge. The electric field is given by E = k * Q / r^2, where k is Coulomb's constant, Q is the charge, and r is the distance from the charge.

Hint: Write a function electric_field(k, Q, r) to calculate the electric field.

14. Compute the nth Catalan Number

Question: Write a Python program named catalan_number.py that calculates the nth Catalan number using recursion. The nth Catalan number is given by Cn = Σ Ci * Cn-i-1 for i = 0 to n-1.

Hint: Define a recursive function catalan_number(n) to compute the nth Catalan number.

15. Calculate the Work Done by a Variable Force

Question: Write a Python program named work_done_variable_force.py that calculates the work done by a variable force over a distance using the trapezoidal rule for integration. Assume the force is given as a function of distance F(x).

Hint: Define a function work_done_variable(F, a, b, n) where F is the force function, a and b are the limits, and n is the number of intervals.

16. Calculate the nth Binomial Coefficient

Question: Write a Python program named binomial_coefficient.py that calculates the binomial coefficient C(n, k) using recursion. The binomial coefficient is given by C(n, k) = n! / (k! * (n - k)!).

Hint: Use recursion in the function binomial_coefficient(n, k) to compute the coefficient.

17. Compute the Sum of the First n Cubes

Question: Write a Python program named sum_of_cubes.py that calculates the sum of the first n cubes using recursion. The sum is given by Sn = 1^3 + 2^3 + ... + n^3.

Hint: Define a recursive function sum_of_cubes(n) to calculate the sum.

18. Calculate the Time Period of a Simple Pendulum

Question: Write a Python program named pendulum_time_period.py that calculates the time period of a simple pendulum. The time period is given by T = 2π * sqrt(L / g), where L is the length of the pendulum and g is the acceleration due to gravity.

Hint: Create a function pendulum_time_period(L, g) to compute the time period.

19. Calculate the nth Pell Number

Question: Write a Python program named pell_number.py that calculates the nth Pell number using recursion. The Pell sequence is defined by P(0) = 0, P(1) = 1, and P(n) = 2 * P(n-1) + P(n-2) for n > 1.

Hint: Define a recursive function pell_number(n) to find the nth Pell number.

20. Calculate the Mean and Variance of a Set of Numbers

Question: Write a Python program named mean_variance.py that calculates the mean and variance of a set of numbers provided by the user. The mean is the average of the numbers, and the variance is the average of the squared differences from the mean.

Hint: Define two functions mean(numbers) and variance(numbers) to compute the mean and variance, respectively. Use the mean function inside the variance function.