Python - Get prime numbers within 100

Python 100例Python 100 Examples

Title: Get prime numbers within 100.

Program analysis:A prime number, also known as a prime, has infinitely many. A prime number is defined as a natural number greater than 1 that has no other factors besides 1 and itself, such as: 2, 3, 5, 7, 11, 13, 17, 19.

Method 1:

#!/usr/bin/python # -*- coding: UTF-8 -*- num=[]; i=2 for i in range(2,100): j=2 for j in range(2,i): if(i%j==0): break else: num.append(i) print(num)

Method 2:

import math def func_get_prime(n): return filter(lambda x: not [x%i for i in range(2, int(math.sqrt(x))+1) if x%i ==0], range(2,n+1)) print func_get_prime(100)

The output result is:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Python 100例Python 100 Examples

Other extensions