python - Finding prime divisors from a given integer -


i have find divisors of given integer, , divisors have find prime numbers , put them in list lowest highest.

this have far:

def prime_divisors(n):      j = 2     list1 = []     prime_list = []     in range(2,n+1):             if n%i == 0:                 if i==2 or i==3:                     prime_list.append(i)                 elif i%j == 0:                     j in range(2,n+1,2):                         list1.append(j)             elif n%2 == 1 or n%3 == 1:                 prime_list.append(n)                 return prime_list     return prime_list prime_divisors(12)     

your test check if divisor prime incorrect. error seems @ elif i%j == 0: section. also, happens list1?

linked questions regarding prime testing: answer1 answer2. 1 picked below may not efficient, works.

from math import sqrt; itertools import count, islice     def is_prime(n):     return n > 1 , all(n%i in islice(count(2), int(sqrt(n)-1)))  def prime_divisors(n):      prime_list = []     search_list = range(2, n/2 +1) # search n/2     search_list.append(n) # check n       in search_list:         if n%i == 0:         # has been found evenly divide input number n         # determine if prime             if is_prime(i):                 prime_list.append(i)     return prime_list 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -