<s>[INST] import random
import math

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

def eratosthenes_sieve(n):
    sieve = [True] * (n + 1)
    for i in range(2, n + 1):
        if sieve[i]:
            for j in range(i * 2, n + 1, i):
                sieve[j] = False
    return sieve

def generate_prime(n):
    if n == 1:
        return 2
    sieve = eratosthenes_sieve(n * 5)
    primes = [x for x in range(2, n * 5) if sieve[x]]
    while True:
        random_prime = random.choice(primes)
        if random_prime > n:
            return random_prime

def main():
    num = input("Enter a number: ")
    try:
        num = int(num)
        print(generate_prime(num))
    except ValueError:
        print("That's not a valid number!")

if __name__ == "__main__":
    main()

This code generates a prime number larger than the number entered by the user. However, it takes a very long time to generate a prime for large numbers. How do I optimize it?[/INST]