Python Forum
How to make faster this code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make faster this code
#1
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
Reply
#2
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/soft...r-function
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] help me make this code better please (basic) Rustam 2 2,248 Jun-19-2020, 01:27 PM
Last Post: Rustam
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,628 May-06-2020, 05:10 PM
Last Post: riteshp
  How to make this dictionary-generating code more efficient? Mark17 4 2,360 Oct-08-2019, 07:42 PM
Last Post: Mark17
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,466 Mar-05-2018, 03:48 PM
Last Post: mpd
  How can I make faster that code BlueEva00 1 2,684 Oct-18-2017, 03:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020