Computer Science
Write a method in Python to find and display the prime numbers from 2 to N. The value of N should be passed as an argument to the method.
Python
Python Functions
1 Like
Answer
def find_primes(N):
primes = []
for num in range(2, N + 1):
factors = 0
for i in range(1, num + 1):
if num % i == 0:
factors += 1
if factors == 2:
primes.append(num)
return primes
N = int(input("Enter the value of N: "))
prime_numbers = find_primes(N)
print("Prime numbers from 2 to", N, "are:", prime_numbers)
Output
Enter the value of N: 10
Prime numbers from 2 to 10 are: [2, 3, 5, 7]
Enter the value of N: 30
Prime numbers from 2 to 30 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Answered By
3 Likes
Related Questions
Consider the following Python function Fn(), that follows:
def Fn(n): print (n, end=" ") if n < 3: return n else: return Fn (n//2) - Fn (n//3)
What will be the result produced by the following function calls?
- Fn (12)
- Fn (10)
- Fn (7)
Figure out the problem with the following code that may occur when it is run.
def recur(p): if p == 0: print ("##") else: recur(p) p = p - 1
Write a method EvenSum(NUMBERS) to add those values in the tuple of NUMBERS which are even.
Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase) of the places whose names are longer than 5 characters. For example, Consider the following dictionary:
PLACES = {1: "Delhi", 2: "London", 3: "Paris", 4: "New York", 5:"Doha"}
The output should be:
LONDON
NEW YORK