Write a function that tests if a number is divisible by n - python

def div(num1,num2):
n = 0
while n > 0:
if num1%n == 0 and num2%n==0:
print(True)
n+=1
else:
print(False)
This is probably wrong though.
EDIT:
def div(num1,num2):
if num1%num2==0:
return True
else:
return False

this question is good use for ternary return
def div(number, n):
# check if denominator is zero or modulo not 0
return False if ((n==0) or (number%n!=0)) else True
print div(10,0),div(0,10),div(2,10),div(10,2)
>>>False True False True
possible duplicate
How do you check whether a number is divisible by another number (Python)?

Related

Check prime number

def isPrime(n):
if n>1:
for i in range(2,n):
if (n % i==0):
return False
else:
return True
else:
return False
This code worked in most cases, but when input n=133, it returned True as output. Please show me the error.
Please note that your code could return True in the first iteration of the loop, you need to check for the other values.
Let's see how your loop works.
For value such as 3 your loop will have n%i check i.e. 3%2 which won't satisfy the condition. So it will return true.
But for value such as 9. Loop will check 9%2=0 which again doesn't satisfy the condition and returns True.
So use something like.
def isPrime(n):
if n>1:
for i in range(2,n):
if (n % i==0):
return False
else:
x=False
return x;
else:
return False
So I ran this and it seems this function will return True for any odd number because it returns false on the first iteration, which is checking if it's divisible by 2. Overall, your function is pretty much wrong, here is how I would approach it.
def isPrime(n):
div = True
for i in range(2,int(n**0.5)):
if n%i==0:
div = False
break
return div
Here what I did was I only checked prime factors upto the square root of the number (because if a number has a prime factor greater than its square root(but not the number itself), it must also have a prime factor less than its square root, so we do not need to check further), and I exited from the loop whenever even once it was divisible by something. I checked and it's working correctly.
The problem with your loop is that when you are iterating through the for loop, it always returning either True or False. To prevent that, what you should create a counter which changes its value accordingly.
def PrimeChecker(n):
if n < 1:
return f'{n} is less than 1. So, not prime' # You can add this if you
# want.
else:
for i in range(2, n): # The main loop
counter = True # The counter
if n % i == 0: # If it becomes divisible....
counter = False # Counter becomes false.
break # Break out of loop
return counter # Return False or True
Hope that helped :)
Most efficient Primechecker program :)
from math import sqrt
def checkprime(x):
if x > 2 and x % 2 == 0:
return False
elif x < 2:
return False
else:
for i in range(2, int(sqrt(x)) + 1):
if x % i == 0:
return False
return True
return False

returns wrong result when I use ternary conditional operator

There's a function that returns True if given number is prime and False when the number is not. Here's a bit of code:
def isPrime(n):
...
...
if (k > 0):
return False
else:
return True
if (isPrime(number)):
print(number, "is prime")
else:
print(number, "is not prime")
And it works fine. But I want to use ternary conditional operator in the isPrime function. So I edited the function like this:
False if k > 0 else True
And now every given number is considered to be not prime. Where's the error in my code?
The error in your code is that it doesn't have anything that returns the result of a ternary operator. This makes it automatically return None which is treated as False.
Just return, no ternary operator is needed.
def isPrime(n):
...
...
return k <= 0
Edit:
Or
return not k > 0
It will always return False until the number will be greater than 0. Because False in your code is at the place of True result.
Use this:
True if k>0 else False
In your case:
def isPrime(k):
return False if k<0 else True
Use it like:
return n if n<m else m

Prime numbers multiplied by 3 shown as prime numbers

I was trying to create a function in Python that checks whether a given number is a prime number, so I wrote this code:
def is_prime(x):
if x<2:
return False
elif x==2:
return True
else:
for n in range(2,x):
if (x%n)==0:
return False
else:
return True
For some reason, every number which is a sum of (Prime_number*3) is shown as a Prime Number (for example, these are shown as prime numbers: 9,21,15,25...)
Can anyone see a problem with my code?
Because you return in the very first iteration of the loop. You can't know that something is prime until after the loop has been exhausted. So don't return True until the iterations have finished.
def is_prime(x):
if x<2:
return False
elif x==2:
return True
else:
for n in range(2,x):
if (x%n)==0:
return False
return True
This happens, because you return from is_prime on the very first iteration of the loop. You test if x is dividable by 2, return True if it is and False otherwise.
Remove else clause from the loop and return True after it has ended.
def is_prime(x):
if x<2:
return False
elif x==2:
return True
else:
for n in range(2,x):
if (x%n)==0:
return False
return True
A more efficient method would iterate only upto square root of x.
import math;
def isPrime(x):
if x < 2:
return False;
elif x == 2:
return True;
else:
for n in range(2, int(math.sqrt(x))+1):
if x%n == 0:
return False;
return True;
# Test the method for first 50 natural numbers
for i in range(51):
if isPrime(i):
print i;

how to find a prime number function in python

I started getting back to python coding and realized I couldn't quite figure this out. I'm trying to code a prime number function. Could someone help with this?
Here is my code:
def is_prime(x):
a = True
for n in range(2, x-1):
while n < x:
n+=1
if x % n == 0:
a = False
elif n < 2:
a = False
else:
a = True
break
break
return a
If anyone has an idea on what I'm doing wrong, please let me know. A month ago I tried this and couldn't get the logic down. I think I was stumped and didn't ever ask for help... Also, how long do you think I should try to do this for before I ask for help on average?
As, it has been said, you can optimize the code by just checking the odd numbers and iterating upto the sqrt of the num
import math
def isPrime(num):
if(num==1):
return False
if(num==2):
return True
if(num%2==0):
return False
i = 3
while(i<math.sqrt(num)+1):
if num%i==0:
return False
i += 2
return True
#do the inputs and check if isPrime
#print(isPrime(2))
using your code, and focusing on code structure:
def is_prime(x):
# function contents must be indented
if x == 0:
return False
elif x == 1:
return False
# your base cases need to check X, not n, and move them out of the loop
elif x == 2:
return True
for n in range(3, x-1):
if x % n == 0:
return False
# only return true once you've checked ALL the numbers(for loop done)
return True
adding some optimizations:
def is_prime(x):
if x <= 1:
return False
if x == 2:
return True
for n in range(3, x**(0.5)+1, 2): # this skips even numbers and only checks up to sqrt(x)
if x % n == 0:
return False
return True
def prime(number):
for i in range(2,number,1):
if number % i == 0:
return False
return True
entry = int(input("Please enter the number: "))
while True:
if prime(entry):
print ("It's a prime number. ")
continue
else:
print ("It's not a prime number.. ")
continue
You will entry a number then this function will give you if its a prime or not. Check the function it will solve your problem.
ALSO You can upgrade the speed of your program. You know prime numbers can not be even, so you you dont have to check even numbers
like 4-6-8-26. So in the range function, which is (2,number) add "2"
end of it. like (3,number,2) then program will not check even numbers.
ALSO a factor can not be bigger than that numbers square root. So you dont have to check all of numbers till your main number, its
enough to check your numbers square root. like: (2,number**0.5) that
means from 2 to your number square root. So double up for program
speed.
So the function will be:
def prime(number):
for i in range(3,(number**0.5)+1),2):
if number % i == 0:
return False
return True
The first function enough for you actually. I am working with huge numbers, so I have to upgrade the speed of my program.
def is_prime(x):
for n in range(2, x-1):
if n == 0:
return False
elif n == 1:
return False
elif n == 2:
return True
elif x % n == 0:
return False
else:
return True
What you are doing wrong is , the first three if elif blocks are executed every time in the loop. So, they should be outside the for loop. Also.
if x%n==0:
return True
It is fine. But the later
else:
return True
is wrong because whenever the first n does not divide the x, it will return Composite. So,
return True
block must be outside the for loop.
Other optimizations can be done by yourself.
This is how I solved it
def is_prime(n):
if n==1:
print("It's not a Prime number")
for z in range(2,int(n/2)):
if n%z==0:
print("It's not a Prime number")
break
else:
print("It's a prime number")
or if you want to return a boolean value
def is_prime(n):
if n==1:
return False
for z in range(2,int(n/2)):
if n%z==0:
return False
break
else:
return True

python check prime, stuck at one point

This was the question our teacher gave us:
"One way to determine whether or not a number is a prime number is as follows:
if the number < 2, then return False
if the number is 2, then return True
for each value of i, where i >=2 and i < number:
if i divides the number evenly, then return False
return True"
I've managed to work through most of it, but was stuck when it said 'for each value of i, where i >=2 and i < number:'
how do I write this in code?
if number < 2:
return False
if number == 2:
return True
?????????
if i%2 == 0:
return False
return True
Yo need a loop to check all numbers from 2 to one less than the number being checked. There are better ways to do it (such as only checking up to the square root of the number) but a simplistic algorithm would be:
def isPrime (n):
if n < 2:
return False
for x in range (2, n):
if n % x == 0:
return False
return True
So, in terms of what you need to add to your code:
a loop iterating some variable from two up to one less than the number.
checking modulo with that variable rather than the hard-coded 2.
You will need to start a loop from 2 to the number
for i in range(2,number)
if number%i == 0:
return false
def isprime(num):
#this is the part you are missing
for divider in range(2,num):
if num%divider == 0:
return False
#end of missing part
return not num%2 == 0 or num==2 or num==0
for i in range(0,200):
if isprime(i): print i, isprime(i)

Categories

Resources