Display all Prime Factors from user input
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.
# int ----> list
# returns factor list of a num
def factor(num):
factors =
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
return factors
# int ----> bool
# returns true if num is prime
def prime(num):
for i in range(num):
if not(i == 0 or i == 1):
if num % i == 0:
return False
return True
# main
# prints the factors of a number which are prime
def primeFactorization():
num = int(input("Enter the number: "))
factors = factor(num)
prime_factors =
for item in factors:
if prime(item):
prime_factors.append(item)
print(prime_factors)
primeFactorization()
python python-3.x primes
add a comment |Â
up vote
2
down vote
favorite
The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.
# int ----> list
# returns factor list of a num
def factor(num):
factors =
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
return factors
# int ----> bool
# returns true if num is prime
def prime(num):
for i in range(num):
if not(i == 0 or i == 1):
if num % i == 0:
return False
return True
# main
# prints the factors of a number which are prime
def primeFactorization():
num = int(input("Enter the number: "))
factors = factor(num)
prime_factors =
for item in factors:
if prime(item):
prime_factors.append(item)
print(prime_factors)
primeFactorization()
python python-3.x primes
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.
# int ----> list
# returns factor list of a num
def factor(num):
factors =
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
return factors
# int ----> bool
# returns true if num is prime
def prime(num):
for i in range(num):
if not(i == 0 or i == 1):
if num % i == 0:
return False
return True
# main
# prints the factors of a number which are prime
def primeFactorization():
num = int(input("Enter the number: "))
factors = factor(num)
prime_factors =
for item in factors:
if prime(item):
prime_factors.append(item)
print(prime_factors)
primeFactorization()
python python-3.x primes
The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.
# int ----> list
# returns factor list of a num
def factor(num):
factors =
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
return factors
# int ----> bool
# returns true if num is prime
def prime(num):
for i in range(num):
if not(i == 0 or i == 1):
if num % i == 0:
return False
return True
# main
# prints the factors of a number which are prime
def primeFactorization():
num = int(input("Enter the number: "))
factors = factor(num)
prime_factors =
for item in factors:
if prime(item):
prime_factors.append(item)
print(prime_factors)
primeFactorization()
python python-3.x primes
edited May 10 at 20:08
Sam Onela
5,77461543
5,77461543
asked May 10 at 19:50
Aditya Garg
576
576
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
For both of your functions prime
and factor
you have something like:
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
when you could shorten by using range(start,stop)
:
for i in range(2,num):
if num % i == 0:
factors.append(i)
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
For both of your functions prime
and factor
you have something like:
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
when you could shorten by using range(start,stop)
:
for i in range(2,num):
if num % i == 0:
factors.append(i)
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
add a comment |Â
up vote
2
down vote
accepted
For both of your functions prime
and factor
you have something like:
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
when you could shorten by using range(start,stop)
:
for i in range(2,num):
if num % i == 0:
factors.append(i)
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
For both of your functions prime
and factor
you have something like:
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
when you could shorten by using range(start,stop)
:
for i in range(2,num):
if num % i == 0:
factors.append(i)
For both of your functions prime
and factor
you have something like:
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
when you could shorten by using range(start,stop)
:
for i in range(2,num):
if num % i == 0:
factors.append(i)
answered May 10 at 19:58
depperm
30319
30319
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
add a comment |Â
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
Oh I did not notice that! Thanks for pointing out.
â Aditya Garg
May 10 at 20:01
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194138%2fdisplay-all-prime-factors-from-user-input%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password