Checking if prime number then printing factors - python

The question asks to check if a number is a prime number. If it isn't, then you have to make a separate function that prints the list of factors of the prime numbers. The exact question is:
Write two functions (isPrime and primeFactors). The function
isPrime will return True if its argument is a prime number, False otherwise. The function primeFactors will return a list of prime factors of a number.
So far I have:
def isPrime(x):
if x==1:
return False
elif x==2:
return True
else:
for i in range(2,x):
if (x % i==0):
return False
This first function checks if the number is prime or not. However, I am not sure how to make the primeFactors function then only work when the result is not a prime number.

Since you already have your function for determining if a number is prime, the function of finding the prime factors of a number would be as follows:
def findPrimeFactors(number):
primeFactors = []
for i in range(2, number + 1):
if number % i == 0 and isPrime(i):
primeFactors.append(i)
return primeFactors

If you want to find all the prime factors including duplicates i.e. #Omari Celestine's answer will only return [2] for the findPrimeFactors(8) rather than [2, 2, 2] etc.
You could do something like this, notice how I am only checking up to the square root of n in each function:
def is_prime(n):
if n < 2:
return False
else:
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
def prime_factors(n):
primes = []
i = 2
while i * i <= n:
if not is_prime(n):
n = n // i
primes.append(i)
else:
i += 1
if n > 1:
primes.append(n)
return primes
print(is_prime(9)) # False
print(prime_factors(8)) # [2, 2, 2]
print(prime_factors(37)) # [37]
print(prime_factors(56)) # [2, 2, 2, 7]
print(prime_factors(23424)) # [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 11]

Related

How to find prime numbers in python

I'm new to Python. I am trying to count the prime numbers in a given range. Some of the answers that developers have shared are like this:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
if all(i%j!=0 for j in range(3,int(math.sqrt(i))+1,2)):
out.append(i)
print(out)
I wrote a one like this:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
for j in range(3, int(math.sqrt(i))+1,2):
if i%j != 0:
out.append(i)
print(out)
but it doesn't work. Could somebody please help me. Appreciated!
Neither of your example count_primes() functions actually counts primes -- they simply print odd primes. Let's implement a working version of your trial division code, not using confusing booleans and a bad algorithm, but rather taking advantage of Python's else clause on for loops:
def collect_odd_primes(number):
primes = []
for candidate in range(3, number, 2):
for divisor in range(3, int(candidate ** 0.5) + 1, 2):
if candidate % divisor == 0:
break
else: # no break
primes.append(candidate)
return primes
print(collect_odd_primes(40))
OUTPUT
> python3 test.py
[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
>
As #MarkRansom comments, the Sieve of Eratosthenes is the better way to go. (+1) Now, let's convert our code to count odd primes instead:
def count_odd_primes(number):
count = 0
for candidate in range(3, number, 2):
for divisor in range(3, int(candidate ** 0.5) + 1, 2):
if candidate % divisor == 0:
break
else: # no break
count += 1
return count
print(count_odd_primes(40))
OUTPUT
> python3 test.py
11
>
Something like this should work. You have to set a variable, because 15%9 != 0, outputs True.
import math
def count_primes(num):
out = []
for i in range(3,num,2):
prime = True
for j in range(3, int(math.sqrt(i))+1,2):
if i%j == 0:
prime = False
if prime:
out.append(i)
print(out)
count_primes(15)
The reason your code and the other is is different is because their use of the all() method. Have a look at how i implemented the method using bools:
import math
def count_primes(num):
out = []
for i in range(3,num,2):
f = True
for j in range(3,int(math.sqrt(i))+1,2):
if i%j==0:
f = False
break
if f:
out.append(i)
print(out)
count_primes(20)
Output:
[3, 5, 7, 11, 13, 17, 19]
You’re appending to the result of the module is unequal to zero. However, it’s only a prime number if all modulo are unequal to zero (there is an all statement missing in your code).
Depending on what program you're running for your code-writing, an alternative method—as opposed to the other answers to this question—would be to write:
n = int(input("Write an integer:"))
m = 2
if n == 1:
print(n, "is a prime number!")
if n == 2:
print(n, "is not a prime number.")
while n > m:
if n % m == 0:
m = m + 1
print(n, "is not a prime number.")
break
if n > n % m > 0:
m = m + 1
print(n, "is a prime number!")
break
It may not be the most efficient, but it gives you a really nice, straight answer to whether or not "x" is a prime number!

Removing multiples of a number from a list in Python

I'm new to Python and i'm working on a program which finds the prime factors of a number. My code so far looks like this:
num = int(input('\nEnter a natural number greater than 1: '))
if num <= 1:
while num <= 1:
num = int(input('\nI said greater than 1: '))
if num == 2:
print('\n', num, 'is a prime number.')
else:
toggle = 0
flist = []
for i in range(2, num - 1):
if num % i == 0:
flist.append(i)
toggle = 1
if toggle == 0:
print('\n', num, 'is a prime number.')
if toggle == 1:
print('\n', num, 'is not a prime number.')
print('\nIt\'s prime factors are:', flist)
With an input of 30 for example i get this output:
Enter a natural number greater than 1: 30
30 is not a prime number.
It's prime factors are: [2, 3, 5, 6, 10, 15]
The prime factors in this case are 2, 3, 5. How can i remove their multiples ?
Thank you.
I would define an extra function to check if a number is prime
def is_prime(n):
if n>1:
for i in range(2,n):
if (n % i)==0:
return False
return True
Then I would use it inside you code to check if a number is prime before to add it to the final list of prime factors.
num = int(input('\nEnter a natural number greater than 1: '))
if num <= 1:
while num <= 1:
num = int(input('\nI said greater than 1: '))
if num == 2:
print('\n', num, 'is a prime number.')
else:
toggle = 0
flist = []
for i in range(2, num - 1):
if num % i == 0:
if is_prime(i): # append only prime numbers
flist.append(i)
toggle = 1
if toggle == 0:
print('\n', num, 'is a prime number.')
if toggle == 1:
print('\n', num, 'is not a prime number.')
print('\nIt\'s prime factors are:', flist)
This gives you the right output:
Enter a natural number greater than 1: 30
30 is not a prime number.
It's prime factors are: [2, 3, 5]
You are getting the multiples of your prime factors, such as 6 = 2 × 3, 10 = 2 × 5 and 15 = 3 × 5.
To avoid those, you should divide your initial number by the prime factors as you find them, so that it won't divide again by the multiples of those prime factors.
Also, you should do that repeatedly, since numbers often have an exponent of a prime factor (for instance, 12 = 2² × 3, so you should divide by 2 twice.)
for i in range(2, num):
while num % i == 0:
flist.append(i)
num = num // i
if num == 1:
break
When you get to 1, you know you can stop.
There are also other optimizations you can do, for instance when i² is larger than num, you know num must be a prime, since you already divided it by all factors smaller than i.
You can also skip even numbers after 2, since those will never be factors.
Using the simple code above, you'll know that num is a prime whenever flist is empty. (You don't need an extra toggle to track that.) That works, since you're never dividing by num itself, so if you get to the end and haven't divided once, that means it's a prime.
For a number such as 12, your flist will have [2, 2, 3]. If you want the unique prime factors, without repetitions, you can use set(flist) to get rid of the duplicates.
Here's a solution using the Sieve of Eratosthenes to first find all primes beneath N and then go through those to find all prime factors, this should be pretty efficient
import numpy as np
def findSieve(num):
allN = np.arange(1,num+1,1)
mask = allN == allN
maskN = np.ma.MaskedArray(allN)
p = 2
while maskN[p:].count() > 0:
mask = mask & ((allN%p != 0) | (allN <= p))
maskN = np.ma.MaskedArray(maskN, mask =~ mask)
p = maskN[p:].min()
return np.asarray(maskN[maskN.mask == False])
def findPrimeFactors(num):
sieve = findSieve(num)
flist = []
num_factored = num
for i in range(1,len(sieve)) :
while num_factored % sieve[i] == 0:
flist.append(sieve[i])
num_factored = num_factored // sieve[i]
if num_factored == 1:
break
prime = len(flist) == 1
if prime:
print('\n', num, 'is a prime number.')
else:
print('\n', num, 'is not a prime number.')
print('\nIt\'s prime factors are:', flist)
num = int(input('\nEnter a natural number greater than 1: '))
findPrimeFactors(num)

Pyschools Prime factorization

I'm doing the pyschools practices and i have a problem at Topic 5 question 12 - Prime Factorization, i have to do this:
Given a positive integer, write a function that computes the prime factors that can be multplied together to get back the same integer.
Examples
>>> primeFactorization(60)
[2, 2, 3, 5]
>>> primeFactorization(1050)
[2, 3, 5, 5, 7]
>>> primeFactorization(1)
[]
This is my code:
import operator
import functools as fun
def primeFactorization(num):
num = num
primes = []
result = []
if num > 1:
[primes.append(x) for x in range(2, num) if all(x % y != 0 for y in range(2, x))]
multy = fun.reduce(operator.mul, result, 1)
for number in primes:
if num % number == 0 and multy != num:
result.append(number)
return result
Which returns me this:
Test Cases Expected Result Returned Result
primeFactorization(33) [3, 11] [3, 11]
primeFactorization(84) [2, 2, 3, 7] [2, 3, 7]
primeFactorization(1) [] []
I've tryed this and i'm getting Private Test Case failed:
import operator
import functools as fun
def primeFactorization(num):
num = num
primes = []
result = []
if num > 1:
[primes.append(x) for x in range(2, num) if all(x % y != 0 for y in range(2, x))]
multy = fun.reduce(operator.mul, result, 1)
for number in primes:
if num % number == 0:
result.append(number)
multy = fun.reduce(operator.mul, result, 1)
y = num/multy
if y != 1 and y in primes:
result.insert(0, int(y))
return result
Test Cases Expected Result Returned Result
primeFactorization(33) [3, 11] [3, 11]
primeFactorization(84) [2, 2, 3, 7] [2, 2, 3, 7]
Private Test Cases Passed Failed
primeFactorization(1) [] []
What can i do to pass?
Why make it so complicated?
The problem of finding all prime factors of a given number X is the same as to find the set of the smallest numbers (that are larger than 1) that we can divide X into.
To solve the problem we can start by finding the smallest number n that divides X.
This is the first prime factor of X. We can then find the rest of the prime factors by finding the prime factors of X/n
def primeFactorization(X):
possible = [2] + range(3,int(ceil(sqrt(X)))+1,2)
for p in possible:
if X % p == 0:
return [p] + primeFactorization(X/p)
return [X]
primeFactorization(3*3*7*5*11*13*31)
> [3,3,5,7,11,13,31]

my prime number finder is just printing odd numbers

So I'm trying to write a short programme that will find prime numbers, and discard non-prime numbers. This was my attempt at writing it:
def prime(x):
while True:
for y in range(2,x):
if x%y == 0 :
x = x + 1
else:
print( str(x) + " is a prime number")
x = x + 1
return x
prime(x)
try:
x = 3
while True:
x = prime(x)
except:
print("NO MORe")
Instead of giving prime numbers this programme just gives all odd numbers, and im fairly sure its to do with the line:
for y in range(2,x):
But I'm not sure how to fix it, any pointers?
Here is a stub for something you can do. Just make a function that classifies if the number is prime. Then do what ever kind of loop you want and check if its prime. If it is then print it.
def isprime(n):
for m in range(2, int(n**0.5)+1): #only check odd numbers and only go till sqrt of the value (you dont need to check all the way till n)
if not n%m:
return False
return True
for m in range(1, 1000):
if isprime(m):
print(m)
Your algorithm seems chaotic. At first, try it this way:
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
def primes_up_to(x):
result = [2]
for number in range(3, x):
if is_prime(number):
result.append(number)
return result
print(is_prime(4))
print(primes_up_to(23))
which results to:
False
[2, 3, 5, 7, 11, 13, 17, 19]

Python loop is escaping a value

I am new to Python. I am using python 2.7.3 and I have written a small function to check if the given number is prime or not.
The code is as follows -
#!/usr/bin/python2.7
def isprime(n):
if n == 1:
print("1 is neither Prime nor Composite.")
return False
for x in range(2, n):
if n % x == 0:
print("{} equals {} x {}".format(n, x, n // x))
return False
else:
print("{} is a prime number".format(n))
return True
for n in range(1, 5):
isprime(n)
And the output is -
1 is neither Prime nor Composite.
3 is a prime number
4 equals 2 x 2
Why is it escaping 2. I tried debugging as well but its simple bypassing 2.
Thanks.
Think about the case where n == 2:
def isprime(n):
if n == 1: # nope
...
for x in range(2, n): # here we go
So what actually happens?
>>> range(2, 2)
[]
Nothing; you are iterating over an empty range.
Also, you have a logic error - you return True if the first value in range(2, n) (i.e. 2) isn't an integer divisor of n - you claim than any odd number is prime:
>>> isprime(9)
9 is a prime number
True # what?!
If you dedent the last three lines by one level, it fixes both issues:
def isprime(n):
if n == 1:
print("1 is neither Prime nor Composite.")
return False
for x in range(2, n):
if n % x == 0:
print("{} equals {} x {}".format(n, x, n // x))
return False
else:
print("{} is a prime number".format(n))
return True
(alternatively, leave out the else and dedent the last two lines a further level). This gives me:
>>> isprime(9)
9 equals 3 x 3
False
>>> isprime(2)
2 is a prime number
True
If n is 2, then on the line for x in range(2, n): the range(2, 2) will return an empty list.

Categories

Resources