Saturday, June 22, 2024

Simple Code in Python (for beginners) for Prime numbers;

Def is_prime(n):

    """Check if a number is prime."""

    if n <= 1:

        return False

    if n <= 3:

        return True

    if n % 2 == 0 or n % 3 == 0:

        return False

    i = 5

    while i * i <= n:

        if n % i == 0 or n % (i + 2) == 0:

            return False

        i += 6

    return True


def generate_primes(limit):

    """Generate a list of prime numbers up to a given limit."""

    primes = []

    for num in range(2, limit + 1):

        if is_prime(num):

            primes.append(num)

    return primes


# Example usage

limit = 100

prime_numbers = generate_primes(limit)

print(f"Prime numbers up to {limit}: {prime_numbers}")

No comments: