Python Forum
How can I make faster that code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make faster that code
#1
Hello everyone! The problem Im experiencing with this exercise is that I should make it faster. But i dont know how to do it.
The thing is: I have to find the prime numbers of list1 and print them + find the non-prime numbers and print them if they have b divisors


def alpha(list1, b):
    
    numprim=[]
    accadiv=[]
    for i in list1:
        cont=0
        for j in range(2, i):
            if i%j==0:
                cont+=1
                if cont>b:
                    break
        if cont!=0:
            numprim+=[i]
    print(list(set(list1)-set(numprim)))
    for y in list1:
        kappa=0
        for x in range(2, y):
            if y%x==0:
                kappa+=1
                if kappa>b:
                    break
        if kappa==b:
            accadiv+=[x]
    print(accadiv)
Reply
#2
The code goes through the list twice. Can you make it go through the list only once?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] help me make this code better please (basic) Rustam 2 2,250 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,630 May-06-2020, 05:10 PM
Last Post: riteshp
  How to make this dictionary-generating code more efficient? Mark17 4 2,363 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 to make faster this code Ace 1 2,954 Oct-23-2017, 12:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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