def primecheck(num):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
return False
break
else:
return True
Im trying to make a function that checks if an input is prime or not. This code does return True if I enter a prime number, but it also enters true whenever I enter a multiple of a prime number? Why is this happening
thanks
The problem with your code is that you have return statements in the first iteration of the loop. Also break statement is not needed. Moving return True outside the loop gives the solution:
def primecheck(num):
for i in range(2, num):
if num % i == 0:
return False
return True
I leave num = 0 or 1 for you.
Or you can simply use for else for this.
def primecheck(num):
if num>1:
for i in range(2, num):
if num%i==0:
return False
break
else:
return True
print(primecheck(15))
The problem is with your else statement, no need for it. And also break is not needed
def primecheck(num):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
return False
return True
or you can solve it without any iterations. You can use Fermat's little theorem to solve this easily(hope you refer it),
def primeChecker(n):
m = 2 #can be any number
if(n > 1):
if (((m**n)-m)%n == 0): #Using Fermat's little theorem
return True
return False
n = int(input());
if primeChecker(n):
print('{} - is prime number'.format(n))
else:
print('{} - NOT a prime number'.format(n))
Doing a little research on the internet will help to solve these types of questions much easier and efficiently. ;). Cheers!
You are using return in the else part. So your code will break after 1st iteration and print the result of first iteration.
You can do something like this:
def primecheck(num):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
return False
return True
print(primecheck(15))
Related
I've worked a lot on this but I can't figure out the problem. Can you help me?
The code should print numbers from 0 to 100 and print next to the prime numbers "Prime". The first function works but if I try to print the numbers all togheter the "prime" tag gets misplaced.
def testPrime(numTest):
if numTest <= 1:
return False
if numTest == 2:
return True
for i in range(2, numTest):
if (numTest % i) == 0:
return False
else:
return True
def printPrimes (lenght):
i = 0
while i <= lenght:
if testPrime(i):
print(str(i) + "Prime")
else:
print(str(i))
i += 1
printPrimes(100)
The logic in testPrime() is flawed. There are faster techniques but here's a trivial approach that may help:
def testPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5)+1, 2):
if n % i == 0:
return False
return True
Since a prime number is defined a number a natural number greater than 1 that is not a product of two smaller natural numbers
I think this is the only lines you need.
def testPrime(n):
for i in range(2,n):
if n%i == 0:
return False
return True
def prime(upper):
while upper >=2:
for num in range(2, upper + 1):
prime = True
for i in range(2, num):
if (num % i == 0):
prime = False
if prime:
print(num, end=",")
if num == upper: #I think there is a problem here
break
prime(7)
How can I stop this function when it reachs 7 value
PS: I want to execute this codes with while loop.
BTW if you can make it this codes without for-loop please do it for me :)
I appreciate you...
EDIT:
I guess my previous is_prime function is problematic. This way is quicker and works well:(Source)
def is_prime(n):
""""pre-condition: n is a nonnegative integer
post-condition: return True if n is prime and False otherwise."""
if n < 2:
return False;
if n % 2 == 0:
return n == 2 # return False
k = 3
while k*k <= n:
if n % k == 0:
return False
k += 2
return True
You should use this function.
BEFORE EDIT:
I would use the is_prime function from here. You can use another way.
And, I would suggest this code for you:
def is_prime(Number):
return 2 in [Number, 2**Number%Number]
def prime(upper):
print([num for num in range(2, upper + 1) if is_prime(num)])
prime(7)
Output:
[2, 3, 5, 7]
But if you insist on using only while loop, you can modify your prime function as:
def prime(upper):
num = 2
while num <= upper:
if is_prime(num):
print(num, end = ",")
num += 1
Then, output will be:
2,3,5,7,
I have to determine if all the numbers in a list are prime numbers and then return a boolean "True" or "False" statement depending on the outcome. I made some conditional statements inside of a for loop to see if the number was prime or not.
Here's the code:
def all_primes(xs):
is_prime = None
for i in xs:
if i < 2:
is_prime = False
return is_prime
break
elif (i % 2 == 0) and (i % i == 1):
is_prime = False
return is_prime
break
else:
is_prime = True
return is_prime
The problem is, and I saw this in the Python Visualizer, the for loop stops iterating after checking the first value in the list. I don't understand why as the syntax is the same as for loops I've used in the past.
I plugged in some example values like: all_primes([5,2,11,37]) or all_primes([5,2,4,37]) and the return value is always true since 5 is the first number on the list and the only number that is being iterated.
Any ideas as to why?
You have a return and a break in your if/else block, you should get rid of them. Also the return in the else should be outside, or it will just return whenever he finds a "prime".
def all_primes(xs):
is_prime = None
for i in xs:
if i < 2:
is_prime = False
return is_prime
elif (i % 2 == 0):
is_prime = False
return is_prime
else:
is_prime = True
return is_prime
After this, you should know, that you are not really checking primes.
Here is not the most efficient way but its clear how to:
def all_primes(xs):
def checkPrime(n):
if n < 2:
return False
for i in xrange(2, n):
if n%i == 0:
return False
return True
return all(map(checkPrime, xs))
EDIT:
without the map functions, you just have to iterate with a for loop:
def all_primes(xs):
def checkPrime(n):
if n < 2:
return False
for i in xrange(2, n):
if n%i == 0:
return False
return True
for n in xs:
if not checkPrime(n):
return False
return True
You should see the problem the other way around.
If you you find a number which is not prime you should return False, and after your loop end you should return True.
I have a little problem when i need to determine if a number , x is a prime number or not.
x will be a randomly generated positive integer and i get the following message when i execute code:
Your function fails on is_prime(2). It returns None when it should return True.
My code:
def is_prime(x):
if x < 2:
return False
else:
for n in range(2, x):
while x % n == 0:
return False
break
else:
return True
i want the while loop to iterate through n == 2 up to n == (x-1) but it doesnt seem to do it!
what am i doing wrong?
You could write this function much more simplier:
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
So your call for is_prime(2) wouldn't pass the if test and wouldn't be used in the for run and would just return True.
All other prime numbers should be tested if they are greater than 3 and not even.
I have been asked to create two functions, the first is_divisible(n,primes) to check if a number is divisible by any other numbers in prime
and then the second which would use the first to find all the prime numbers in a particular range.
I don't know why, but i cannot work out how to get the primes to show. Anything obvious about what I am doing wrong?
def is_divisible(n, primes):
for p in primes:
if n % p == 0:
return True
else:
return False
def find_primes(N):
primes=[]
for n in range(2, N+1):
is_divisible(n,primes)
if False:
primes.append(n)
print(primes)
find_primes(20)
def is_divisible(n, primes):
for p in primes:
if n % p == 0:
return True
return False # Only return False if no matches
def find_primes(N):
primes=[]
for n in range(2, N+1):
if not is_divisible(n,primes)
primes.append(n)
print(primes)
print find_primes(20)
This if statement will never be true:
if False:
primes.append(n)
Rewrite the code like this:
if is_divisible(n,primes)==False:
primes.append(n)
This code is wrong:
is_divisible(n,primes)
if False:
primes.append(n)
You should check if the divisibility inside the if condition. Try this:
if not is_divisible(n,primes):
primes.append(n)
Python evaluates 0 to False, and all numbers otherwise.
So, it's not necessary to do something like "if condition == True". Use just if condition.