def getPrimeList(check):
storedprimes = []
i = 2
while i <= check:
if isPrime(check):
storedprimes = storedprimes + [i]
i = i + 1
return storedprimes
def getPrimeFact(check):
primelist = getPrimeList(check)
prime_fact = []
i = 0
while check !=1:
if check%primelist[i]==0:
prime_fact=prime_fact+[primelist[i]]
check = check/primelist[i]
i = i + 1
if i == len(primelist):
i = 0
return prime_fact
def getGCF(checks):
a=0
listofprimefacts=[]
while a<len(checks):
listofprimefacts=listofprimefacts+[getPrimeFact(checks[a])]
a=a+1
b=0
storedprimes=[]
while b<len(primefactlist):
c=0
while c<len(listofprimefacts[b]):
if listofprimefacts[b][c] not in storedprimes:
storedprimes=storedprimes+[listofprimefacts[b][c]]
c=c+1
b=b+1
prime_exp=[]
d=0
while d<len(storedprimes):
prime_exp=prime_exp+[0]
d=d+1
e=0
while e<len(storedprimes):
f=0
while f<len(listofprimefacts):
if f==0:
prime_exp[e]=listofprimefacts[f].count(storedprimes[e])
elif prime_exp[e]-(listofprimefacts[f].count(storedprimes[e]))>0:
prime_exp[e]=listofprimefacts[f].count(storedprimes[e])
f=f+1
e=e+1
g=0
GCF=1
while g<len(primelist):
GCF=GCF*(storedprime[g]**prime_exp[g])
g=g+1
return GCF
我正在创建一个程序,该程序将使用这些函数来计算分数;但是,在 shell 中测试我的 GCF 函数后,我不断收到列表索引错误。我不知道,考虑到我 99% 确定我的索引没有问题,错误来自哪里,通常我不会在 SO 中发布这样一个“可修复”的问题,但这次我只是不知道问题是什么,再次感谢。
哦,这是确切的错误
File "<pyshell#1>", line 1, in <module>
getGCF(checks)
File "E:\CompProgramming\MidtermFuncts.py", line 31, in getGCF
listofprimefacts=listofprimefacts+[getPrimeFact(checks[a])]
File "E:\CompProgramming\MidtermFuncts.py", line 20, in getPrimeFact
if check%primelist[i]==0:
IndexError: list index out of range