The task is to create a Python program that checks if a given number is prime. Additionally, the program should be extended to generate all prime numbers up to a specified integer ( N ). A prime number is defined as a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.
def is_prime(n):
return n > 1 and all(n % i for i in range(2, int(n**0.5) + 1))
def primes_up_to(N):
return [x for x in range(2, N + 1) if is_prime(x)]
# Example usage
number = 29
print(f"{number} is prime: {is_prime(number)}")
print("Primes up to 30:", primes_up_to(30))
The provided code consists of two main functions: is_prime
and generate_primes_up_to_n
.
- is_prime(num): Returns
True
ifn
is greater than 1 and not divisible by any number from 2 to the square root ofn
. - generate_primes_up_to_n(n): Returns a list of all prime numbers from 2 to
N
by utilizing theis_prime
function.
The example usage demonstrates how to check if a specific number (29) is prime and how to generate all prime numbers up to 50. The output will indicate whether the number is prime and list the primes up to the specified limit.