J'ai fait un code qui ne semble pas très efficace. Il calcule seulement quelques-uns des nombres premiers.Générateur de Mersenne efficace en python
Ceci est mon code:
num=float(1)
a=1
while(num>0): # Create variable to hold the factors and add 1 and itself (all numbers have these factors)
factors = [1, num]
# For each possible factor
for i in range(2, int(num/4)+3):
# Check that it is a factor and that the factor and its corresponding factor are not already in the list
if float(num) % i == 0 and i not in factors and float(num/i) not in factors:
# Add i and its corresponding factor to the list
factors.append(i)
factors.append(float(num/i))
num=float(num)
number=num
# Takes an integer, returns true or false
number = float(number)
# Check if the only factors are 1 and itself and it is greater than 1
if (len(factors) == 2 and number > 1):
num2=2**num-1
factors2=[1, num]
for i in range(2, int(num2/4)+3):
# Check that it is a factor and that the factor and its corresponding factor are not already in the list
if float(num2) % i == 0 and i not in factors2 and float(num2/i) not in factors2:
# Add i and its corresponding factor to the list
factors2.append(i)
factors2.append(float(num2/i))
if(len(factors2)==2 and num2>1):
print(num2)
a=a+1
num=num+2
Comment puis-je faire mon code plus efficace et être en mesure de calculer rapidement le Mersenne nombres premiers. Je voudrais utiliser le programme pour trouver de nouveaux nombres parfaits possibles.
Plutôt que de simplement coller une réponse, pourquoi ne pas expliquer pourquoi votre réponse est meilleure ou plus rapide? – Neil
Pourquoi dit-on une erreur de mémoire et comment puis-je le contourner –