Reputation: 11
I have been trying to make a 'Prime Number Checker' that looks like this:
Please enter a number: 7
The factors are:
1
7
7 is a prime number
Try again? Y
Enter a number: 6
The factors are:
1
2
3
6
6 is not a prime number
It has 6 factors
I am stuck on the very last portion, which counts how many factors a non-prime number has (listed as 'num_factors' in the code below). Thank you! *Edited for clarity.
def main():
num = int(input("Please enter an integer between 2 and 5,000: "))
def list_factors(x):
print("The factors of your number are: ")
for i in range(1, x + 1):
if x % i == 0:
print(i)
list_factors(num)
def is_prime(num):
if num>1:
for i in range(2,num):
if (num%i) == 0:
print(num, "is NOT a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
is_prime(num)
def num_factors(x):
for i in range(1, x + 1):
if x % i == 0:
list = []
print("It has", len(list), "factors")
num_factors(x)
print()
again=input("Try again? (y/n): ")
if again == "y":
print()
main()
else:
print("Bye!")
if __name__ == "__main__":
main()
Upvotes: 0
Views: 7533
Reputation: 6573
If you would like to capture all the factors, you could use the sub below. If you give it a number like 1024, it returns a list of ten 2's (all the factors)
def main():
x = int(input('Enter a number to find the prime factors '))
primes = prime_factors(x)
print(primes)
def prime_factors(x):
factors = []
y = 2
while (y <= x):
while x % y != 0:
y += 1
x /= y
factors.append(y)
return factors
main()
Upvotes: 0
Reputation: 9
# input and validation integer number
try:
num = int(input(
"Please enter an integer number between 2 and 5,000 : "))
except ValueError:
print("Please enter an integer number between 2 and 5,000!!!")
# for check number.
# If number is prime it's true
# If number not prime it's false
# default is true
status = True
print("%d divided by %d" % (num, 1))
for i in range(2, int(num / 2)+1):
if num % i == 0:
status = False
print("%d divided by %d" % (num, i))
print("%d divided by %d" % (num, num))
if status:
print("%d is prime" % (num))
else:
print("%d is not prime" % (num))
Upvotes: 0
Reputation: 4606
You were very close, the only adjustment would be that you would want to append those values that pass if x % i == 0
which can also be stated if not x % i:
and you would want to move the empty list outside of your loop. then just return the length of that list.
def num_factors(non_prime):
l = []
for i in range(1, non_prime+1):
if not non_prime % i:
l.append(i)
return len(l)
print('It has {} factors'.format(num_factors(10)))
# It has 4 factors.
Future References:
Just to open you to why it would be important to explore built in functions, you could accomplish this using filter
in one expression
def num_factors(non_prime):
return len(list(filter(lambda x: not non_prime % x, range(1, non_prime +1))))
As well as with list comprehension
def num_factors(non_prime):
return len([i for i in range(1, non_prime +1) if not non_prime % i])
You can find information on both at https://docs.python.org/3/
Upvotes: 0
Reputation: 1304
Change
def num_factors(x):
for i in range(1, x + 1):
if x % i == 0:
list = []
print("It has", len(list), "factors")
num_factors(x)
for
def num_factors(x):
list = []
for i in range(1, x + 1):
if x % i == 0:
list.append(x)
print("It has", len(list), "factors")
num_factors(num)
Your number is called num
, not x
. Then you should create the factor list outside the for loop and append each factor to the list.
As somebody recommended in the comments, you should print outside of the functions. Also you don't need to check for factors bigger than x//2
. So I would recommend something like this:
def num_factors(x):
list = []
for i in range(1, x//2 + 1):
if x % i == 0:
list.append(x)
return facts
facts=num_factors(num)
print("It has", len(facts), "factors")
Upvotes: 0
Reputation: 13016
I suggest you build a list of factors first, instead of iterating at each step.
def get_factors(n):
factors = []
for i in range(1, n+1):
if n % i == 0:
factors.append(i)
return factors
Now you can define your other functions in terms of the list of factors:
def is_prime(factors):
return len(factors) == 2
def num_factors(factors):
return len(factors)
If you want to make your implementation more efficient, I suggest you read up on prime factorization.
Upvotes: 1
Reputation: 298
a=int(input("Enter number: "))
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print("Number is prime")
else:
print("Number isn't prime")
Upvotes: 0