Cách kiểm tra số có phải là số nguyên tố hay không trong danh sách của Python?

Trong bài đăng này, Chúng ta sẽ xem cách viết chương trình python để tìm các số nguyên tố từ danh sách các số theo nhiều cách khác nhau

Nhấp vào liên kết này. Số nguyên tố

Thí dụ

Input:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output:  Prime Number : -> 2, 3, 5, 7,

Input:  [15, 30, 45, 60]
Output:  No any number from the given list is Prime

Bây giờ, hãy xem các mã

Mã 1. Sử dụng khái niệm for-else python. do đó, bạn không cần lấy biến cờ.
khái niệm vì cái khác. nếu vòng lặp for chạy thành công tôi. e. cho vòng lặp chạy hoàn toàn thì khối lệnh khác không được thực hiện nếu không thì khối lệnh khác được thực thi.

# Code 1:

# Given number list
numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Empty list
ansList = []

# Iterate through each number 
# form the list
for num in numberList :
    
    # 0 and 1 is not a 
    # prime number
    # so skip this number
    if num == 0 or num == 1 :
        continue
        
    # loop from 2 to half of the
    # given number
    for i in range[2, num // 2 + 1] :

        # If number is divisible by any
        # number [i] then it is not
        # a prime number
        if num % i == 0 :
            break

    # If not divisible then it is
    # a prime number
    else :
        
        # if number is prime
        # then append to the list
        ansList.append[num]

# If list is non-empty then
# print th elements
if len[ansList] :
    
    print["Prime Number : ",end = "-> "]
    
    # printing the prime number
    # from the ansList
    for ans in ansList :
        print[ans, end = ", "]
    
else :
    print["No any number from the given list is Prime"]

đầu ra

Prime Number : -> 2, 3, 5, 7,

mã 2. Tạo hàm do người dùng xác định để kiểm tra số có phải là số nguyên tố hay không

# define a user defined 
# function for checking
# a number is prime or 
# not
def checkPrime[number] :

    # loop from 2 to half of the
    # given number
    for i in range[2, number // 2 + 1] :

        # if number is divisible by any
        # number [i] then it is not
        # a prime number
        if number % i == 0 :
            return 0

    # if not divisible then it is
    # a prime number
    else :
        return 1
        
# Main code
if __name__ == "__main__" :
    
    # Given number list
    numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # Empty list
    ansList = []
    
    # Iterate through each number 
    # form the list
    for num in numberList :

        # 0 and 1 is not a 
        # prime number
        # so skip this number
        if num == 0 or num == 1 :
            continue
        
        # function call for checking
        # a number is prime or not
        if checkPrime[num] :
            
            # if number is prime
            # then append to the list
            ansList.append[num]

    # If list is non-empty then
    # print th elements
    if len[ansList] :

        print["Prime Number : ",end = "-> "]

        # printing the prime number
        # from the ansList
        for ans in ansList :
            print[ans, end = ", "]

    else :
        print["No any number from the given list is Prime"]

đầu ra

Prime Number : -> 2, 3, 5, 7,

mã 3. Sử dụng hàm python tích hợp map[] và sum[]

# Code 3:

# define a user defined 
# function for checking
# a number is prime or 
# not
def checkPrime[number] :
    
    # 0 and 1 is not a 
    # prime number
    # so skip this number
    if number == 0 or number == 1 :
        return 0

    # loop from 2 to half of the
    # given number
    for i in range[2, number // 2 + 1] :

        # if number is divisible by any
        # number [i] then it is not
        # a prime number
        if number % i == 0 :
            return 0

    # if not divisible then it is
    # a prime number
    else :
        # return prime number
        return number
        
# Main code
if __name__ == "__main__" :
    
    # Given number list
    numberList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # use of map built-in function to call
    # user defined function for all
    # element present in the list
    # and return final list
    ansList = list[map[checkPrime, numberList]]

    # if sum of ansList is > 0 that 
    # means it contains prime number 
    # otherwise not
    if sum[ansList] :

        print["Prime Number : ",end = "-> "]

        # printing the prime number
        # from the ansList
        for ans in ansList :
            
            # if ans is non-zero 
            # then print the value
            if ans :
                print[ans, end = ", "]

    else :
        print["No any number from the given list is Prime"]

đầu ra

Prime Number : -> 2, 3, 5, 7, 

bài viết liên quan

  • Python - Chương trình cho Tối đa hai số
  • con trăn. Chương trình nối một từ có chứa 'a' trong…
  • Các kiểu dữ liệu trong Python
  • Chương trình Python để tính Trung bình của một danh sách số nguyên
  • Python - Chương trình tính Tổng bình phương n số tự nhiên đầu tiên…
  • Python - Chương trình tính giai thừa của một số
  • Python - chương trình tính lãi kép
  • Python - chương trình in n số tự nhiên chẵn đầu tiên
  • con trăn. chương trình kiểm tra từng chuỗi trong danh sách các chuỗi…
  • Python - Chương trình tìm diện tích hình tròn

Chủ Đề