Lecture

Finding Primes - Problem Solving

Discover two methods for finding prime numbers within a given integer range.


Method 1
def solution(n): primes = [] for num in range(2, n + 1): # Iterate over all numbers from 2 to n for i in range(2, int(num ** 0.5) + 1): # Check if each number is a prime if num % i == 0: # If not a prime break # Exit the loop else: # If it is a prime primes.append(num) # Add to primes list return primes

This function checks each number by dividing it by numbers from 2 to its square root to determine if it is a prime.


Example Usage

Input/Output Example
result = solution(10) print(result) # Output: [2, 3, 5, 7]

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help