YouTip LogoYouTip

Python Get Prime Number

# Python - Get Prime Numbers Within 100 [![Image 3: Python 100 Examples](#) Python 100 Examples](#) **Topic:** Get prime numbers within 100. **Program Analysis:** A prime number (also called a prime) has infinitely many prime numbers. A prime number is defined as a natural number greater than 1 that has no other factors except 1 and itself, such as: 2, 3, 5, 7, 11, 13, 17, 19. ## Method 1: ```python #!/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: ```python 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: ```python [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] [![Image 4: Python 100 Examples](#) Python 100 Examples](#)
← Python Func InputJava Print Multiplicationtable β†’