How to make faster this code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to make faster this code (/thread-5813.html) |
How to make faster this code - Ace - Oct-23-2017 Hello everyone , I have occurred in a problem with this code.The thing is that I had to find all prime numbers in a list and delete from list1 all the numbers with divisors !=t ; until that point it's all "ok", but I must find a way to make faster the finding process(for divisors) for numbers like >10000000000 def findpandd(list1,t): primes=[] d={} for el in list1: cont=0 r=range(2,el) for i in r: if el%i==0: cont+=1 if i>(el**(1/2)) and cont==0:break if cont>t:break d.update({el: cont}) for el in d: if d[el]==0:primes+=[el] if d[el]!=t:list1.remove(el) return list1,primes RE: How to make faster this code - Larz60+ - Oct-23-2017 You have a loop within a loop. That means that each time the outer loops goes through 1 iteration, the inner loop if executed from start to end. so in your case, the loop: for i in r:will run from start to finish from 2 to el for each iteration of list1. Calculating primes is one place where I would recommend recursion. see: https://www.daniweb.com/programming/software-development/threads/331289/python-recursive-prime-number-function |