Home > Backend Development > Python Tutorial > How to find all prime numbers within 100 in python

How to find all prime numbers within 100 in python

coldplay.xixi
Release: 2020-10-30 10:12:43
Original
13457 people have browsed it

Python method to find all prime numbers within 100: Use the method to determine that the number has no other factors except 1 and itself. The code is [i=2 for i in range(2,100): if( i%j==0):break else:num.append(i)].

How to find all prime numbers within 100 in python

Related free learning recommendations: python video tutorial

Python method to find all prime numbers within 100:

Program analysis: Prime number (prime number) is also called prime number, and there are infinite numbers. A prime number is defined as a number that has no other factors except 1 and itself among natural numbers greater than 1, such as: 2, 3, 5, 7, 11, 13, 17, 19.

Method one:

#!/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)
Copy after login

Method two:

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)
Copy after login

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]
Copy after login

The above is the detailed content of How to find all prime numbers within 100 in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template