Here are 20 Python problems of varying difficulty levels that involve mathematical concepts:
### Easy Problems
1. **Sum of Natural Numbers**: Write a function to calculate the sum of the first n natural
numbers.
```python
def sum_natural_numbers(n):
return n * (n + 1) // 2
```
2. **Factorial Calculation**: Write a function to compute the factorial of a number.
```python
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
```
3. **Prime Check**: Write a function to check if a number is prime.
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
```
4. **GCD Calculation**: Write a function to calculate the greatest common divisor (GCD) of two
numbers.
```python
def gcd(a, b):
while b:
a, b = b, a % b
return a
```
5. **LCM Calculation**: Write a function to calculate the least common multiple (LCM) of two
numbers.
```python
def lcm(a, b):
return a * b // gcd(a, b)
```
6. **Sum of Digits**: Write a function to calculate the sum of the digits of a number.
```python
def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
```
7. **Fibonacci Sequence**: Write a function to generate the first n numbers in the Fibonacci
sequence.
```python
def fibonacci(n):
fib_sequence = [0, 1]
for i in range(2, n):
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:n]
```
8. **Perfect Number Check**: Write a function to check if a number is a perfect number.
```python
def is_perfect_number(n):
return n == sum(i for i in range(1, n) if n % i == 0)
```
9. **Armstrong Number Check**: Write a function to check if a number is an Armstrong number.
```python
def is_armstrong_number(n):
digits = [int(d) for d in str(n)]
return n == sum(d ** len(digits) for d in digits)
```
10. **Palindrome Check**: Write a function to check if a number is a palindrome.
```python
def is_palindrome_number(n):
return str(n) == str(n)[::-1]
```
### Intermediate Problems
11. **Prime Factors**: Write a function to find all prime factors of a number.
```python
def prime_factors(n):
factors = []
i=2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
[Link](i)
if n > 1:
[Link](n)
return factors
```
12. **Nth Prime Number**: Write a function to find the nth prime number.
```python
def nth_prime(n):
count = 0
num = 2
while True:
if is_prime(num):
count += 1
if count == n:
return num
num += 1
```
13. **Sum of Primes**: Write a function to calculate the sum of all prime numbers up to n.
```python
def sum_of_primes(n):
return sum(i for i in range(2, n + 1) if is_prime(i))
```
14. **Sieve of Eratosthenes**: Implement the Sieve of Eratosthenes algorithm to find all primes less
than n.
```python
def sieve_of_eratosthenes(n):
primes = [True] * (n + 1)
p=2
while p * p <= n:
if primes[p]:
for i in range(p * p, n + 1, p):
primes[i] = False
p += 1
return [p for p in range(2, n) if primes[p]]
```
15. **Greatest Common Divisor**: Implement Euclidean algorithm to compute the greatest
common divisor of two numbers.
```python
def gcd_euclidean(a, b):
while b:
a, b = b, a % b
return a
```
16. **LCM using GCD**: Write a function to calculate the least common multiple of two numbers
using the GCD.
```python
def lcm_using_gcd(a, b):
return a * b // gcd_euclidean(a, b)
```
17. **Quadratic Equation Solver**: Write a function to solve a quadratic equation ax^2 + bx + c = 0.
```python
import cmath
def solve_quadratic(a, b, c):
d = b**2 - 4 * a * c
sol1 = (-b - [Link](d)) / (2 * a)
sol2 = (-b + [Link](d)) / (2 * a)
return sol1, sol2
```
18. **Base Conversion**: Write a function to convert a number from one base to another.
```python
def convert_base(num, from_base, to_base):
if isinstance(num, str):
num = int(num, from_base)
else:
num = int(str(num), from_base)
return format(num, f'x' if to_base == 16 else f'o' if to_base == 8 else '')
```
19. **Permutation Calculation**: Write a function to compute nPr (number of permutations).
```python
def permutation(n, r):
return factorial(n) // factorial(n - r)
```
20. **Combination Calculation**: Write a function to compute nCr (number of combinations).
```python
def combination(n, r):
return factorial(n) // (factorial(r) * factorial(n - r))
```
These problems cover a range of mathematical concepts and provide good practice for using Python
in mathematical problem-solving.