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


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


借助於sympy.factorint()方法,我們可以找到給定整數的因子及其對應的多重性。對於小於2的輸入,factorint()的行為如下:

  • factorint(1)-返回空的因式分解{}。
  • factorint(0)-返回{0:1}
  • factorint(-n)-添加-1:1到因子然後是因子n
用法:
factorint(n)

參數:
n - 它表示一個整數。


返回:
返回包含n的素數作為鍵的字典
及其各自的多重性作為值。

範例1:

# import factorint() method from sympy 
from sympy import factorint 
  
n = 2**3 * 3**4 * 5**6
  
# Use factorint() method  
factor_dict = factorint(n)  
      
print("Dictionary containing factors of {} with respective multiplicities:{}". 
      format(n, factor_dict))

輸出:

Dictionary containing factors of 10125000 
with respective multiplicities:{2:3, 3:4, 5:6}

範例2:

# import factorint() method from sympy 
from sympy import factorint 
  
n = 6**4 * 13
  
# Use factorint() method  
factor_dict = factorint(n)  
      
print("Dictionary containing factors of {} with respective multiplicities:{}". 
      format(n, factor_dict))

輸出:

Dictionary containing factors of 16848 
with respective multiplicities:{2:4, 3:4, 13:1}


相關用法


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