當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python sympy.primepi()用法及代碼示例


借助於sympy.primepi()方法,我們可以找到小於或等於給定數的質數數。

用法: primepi(n)

參數:
n – 它表示要計算素數計數的數字。


返回值:返回小於或等於n的質數個數。

示例1:

# import primepi() method from sympy 
from sympy import primepi 
  
n = 10
  
# Use primepi() method  
count_primes = primepi(n)  
      
print("The number of prime numbers less than or equal to {} is {}".format(n, count_primes))

輸出:

The number of prime numbers less than or equal to 10 is 4

示例2:

# import primepi() method from sympy 
from sympy import primepi 
  
n = 150
  
# Use primepi() method  
count_primes = primepi(n)  
      
print("The number of prime numbers less than or equal to {} is {}".format(n, count_primes))          

輸出:

The number of prime numbers less than or equal to 150 is 35


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Python | sympy.primepi() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。