Generate and Shuffle Primes

  • Share this:

Code introduction


This function takes two arguments, the first argument is the upper limit for generating prime numbers, and the second argument is the number of primes to return. The function first calculates all the prime numbers within the given range, then shuffles these prime numbers randomly, and returns the specified number of prime numbers.


Technology Stack : math, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zebra(arg1, arg2):
    import math
    import random
    def factorial(num):
        if num == 0:
            return 1
        else:
            return num * factorial(num - 1)
    
    def is_prime(num):
        if num <= 1:
            return False
        for i in range(2, int(math.sqrt(num)) + 1):
            if num % i == 0:
                return False
        return True
    
    primes = [i for i in range(2, arg1) if is_prime(i)]
    random.shuffle(primes)
    return primes[:arg2]

                 
              
Tags: