Find and display prime numbers within 100 based on python

1. Procedure

You can find and display prime numbers within 100 in Python using the following code :

def find_prime_numbers(limit):
    prime_numbers = []
    for num in range(2, limit + 1):
        is_prime = True
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            prime_numbers.append(num)
    return prime_numbers
limit = 100
prime_numbers = find_prime_numbers(limit)
print(prime_numbers)

This code uses two nested loops to iterate through all numbers from 2 to a specified limit ( limit ). For each number, it will check if it is divisible by a number less than its square root, and if so, mark it as non-prime. If a number is not divisible by any number, it is added to the list of prime numbers. Finally, print the list of prime numbers.

2. Example

Running this code will output prime numbers within 100 .

If you do not have a python running environment, you can use an online python editor.

Online python editor - https://c.runoob.com/compile/9/

Enter the program on the left, click Run, and output the results on the right.

Guess you like

Origin blog.csdn.net/weixin_45770896/article/details/132947267