How do I check divisibility in python? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am writing a python script right now to check if a number a Wilson Prime, but it says I have a syntax error on line 9.
I've tried both the mod function with x%y == 0 and x/y == int, but both of them have given me a syntax error.
n = int(input("Type a natural number here"))
fact = 1
for i in range(1,n):
fact = fact * i
finalfact = fact + 1
if finalfact % n == 0
print(n, "is a prime number!")
if (finalfact/n) % n == 0
print(n, "is also a Wilson Prime!")
I'm trying to make it check if (n-1)!+1 is divisible by n (which is a way to find prime numbers) and to check if (n-1)!+1 is divisible by n^2 (Wilson Primes), but it gives me a syntax error on line 9 and nothing happens.

You are missing : at the end of if statements.
Change:
if finalfact % n == 0
To:
if finalfact % n == 0:
And:
if (finalfact/n) % n == 0
To:
if (finalfact/n) % n == 0:
Need to indent the code properly:
Change:
for i in range(1,n):
fact = fact * i
To:
for i in range(1,n):
fact = fact * i
Also:
finalfact / n # 5 / 2 = 2.5
Should be:
finalfact // n # 5 // 2 = 2

Related

Prime number function loop mistake [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I have been told that this code for finding a prime number is wrong. Could anyone explain why? It works for at least the first 100 numbers.
if x<2:
return 'It is neither prime nor composite'
elif x==2 or x==3:
return 'It is a prime number'
else:
for i in range(2,int(x**0.5)+1):
print(i)
if x%i==0 :
return 'It is not a prime number'
break
else:
return 'It is a prime number'
I've tested your function with 1,000,000 integers, starting from 2, by comparing your results with ones from dedicated function from sympy library.
import sympy
def prime(x):
if x<2:
return False
elif x==2 or x==3:
return True
else:
for i in range(2,int(x**0.5)+1):
# print(i)
if x%i==0 :
return False
# break # not necessary, "return" above exits anyway
else:
return True
success = 0
fail = 0
for i in range(2, 1000002):
if prime(i) != sympy.isprime(i):
print(i)
fail += 1
else:
success += 1
print(f'SUCCESSES: {success}\nFAILURES: {fail}')
Output:
SUCCESSES: 1000000
FAILURES: 0
You function seems to work with no issues
Perhaps, it was meant not incorrectness, but inefficiency of the code. I would write differently:
def prime(n):
if n < 3 or n % 2 == 0:
return n == 2
d = 3
while d**2 <= n:
if n % d == 0:
return False
d += 2
return True
print(*filter(prime, range(101)))

Syntax Error : what I am missing from Python "for" loop with a range [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I get the idea of what I am supposed to do, but I believe I am missing a step here somehwere... I get a syntax error before from line 1!
range(1, 51)
For answer in range:
if answer % 3 == 0 and answer % 5 ==0
print (“Wolly Bully”)
elseif answer % 3 == 0 and answer % 5 = <0
print (“Wolly”)
elseif answer % 3 = <0 and answer % 5 == 0
print (“Bully”)
elseif answer % 3 = <0 and answer % 5 = <0
print (str(answer) + " ", end = "")
You can define a range with range(1, 51), but range is a function, so you need to do something with the range it returns.
For example:
my_range = range(1, 51)
for answer in my_range:
...
And because you don't need the range for anything else, this is a better solution:
for answer in range(1, 51):
...
There's some more issues with your code, many of them typos - here's a corrected version (not guaranteed to work correctly, but it runs):
for answer in range(1, 51):
if answer % 3 == 0 and answer % 5 == 0:
print("Wolly Bully")
elif answer % 3 == 0 and answer % 5 < 0:
print("Wolly")
elif answer % 3 < 0 and answer % 5 == 0:
print("Bully")
elif answer % 3 < 0 and answer % 5 < 0:
print(str(answer) + " ", end = "")
A couple of types of changes:
elif instead of elseif
a colon after an if or elif
correct indentation
<= instead of = <
correct quotes
no capitalisation on for
space between a function (print) and its argument list
mutually exclusive if and elif expressions (if something is does not meet if x == y, there's not point in elif x <= y and it's clearer to write elif x < y, since that's the only case for which the code will be executed.
You are probably using an unsuitable editor to write your code, given the capitalisation and strange quotes - it's highly recommended to use a programming editor or IDE, like VSCode (free), PyCharm (free Community) or many other (also free) alternatives.
There are several errors, use range with for, it's elif in Python and most comparison operators need to be corrected (<= instead of =>):
for answer in range(1, 51):
if answer % 3 == 0 and answer % 5 == 0:
print("Wolly Bully")
elif answer % 3 == 0 and answer % 5 <= 0:
print("Wolly")
elif answer % 3 <= 0 and answer % 5 == 0:
print("Bully")
elif answer % 3 <= 0 and answer % 5 <= 0:
print(str(answer) + " ", end="")
Out:
Wolly Bully
Wolly Bully
Wolly Bully

Turn pseudocode into functioning code to find greatest common divisor [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the pseudocode here that says: Initialize d to the smaller of m and n.
While d does not evenly divide m or d does not evenly divide n do
Decrease the value of d by 1
Report d as the greatest common divisor of n and m
and I can't figure out what's preventing my code from working. I'll post my code below, if you could take a look for me and tell me what's preventing my code from working properly I'd appreciate it.
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while True:
if d % m != 0 or d % n != 0:
d -= 1
elif d % m == 0 and d % n == 0:
print(d)
break
To find if a value evenly divides another, you need to have the smaller (divisor) on the right side of the modulus operator. So change your code to:
if (m %d != 0) or (n % d != 0):
This evaluates if d evenly divides mor n, instead of if m or n evenly divide d.
The condition in your if is basically the opposite of your elif condition, and to evenly divide m means m should be the left side of the modulo operator (same thing for n). I made an edit here to show what I mean:
m = int(input("Enter a positive integer: "))
n = int(input("Enter another positive integer: "))
d = min(m,n)
while m % d != 0 or n % d != 0:
d -= 1
print(d)

what is the most efficient way of getting digit sum? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have to find the digit sum until its in single digit.
if input is -9999 then output should be -9
(-(9+9+9+9))==-9)
if input is 9012 then output should be 3 (+(9+0+1+2)==1+2==3))
ps: i have solved this but the negative inputs are giving wrong output.I am using the division by 9 property(you can google it) in order to get o(1) solution
my code:
def digSum(n):
if (n == 0):
return 0
if (n % 9 == 0):
return 9
else:
(n % 9)
just a simple recursion should help, add this to your code
if n < 0:
return - digSum(abs(n))
Your code is close, but you need to check to see if the input is a negative number BEFORE you evaluate if it modulates 9 or not. Your current code evaluates if n % 9 == 0 and returns 9, but you should add a condition to check if it's negative and n % 9 first, otherwise it will always return a positive number.
Modify your code to be this:
def digSum(n):
if (n == 0):
return 0
if (n % 9 == 0 and n < 0):
return -9
if (n % 9 == 0):
return 9
else:
(n % 9)

Getting incorrect answer - Sum of Multiples 3 & 5 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have looked at other's code, and tried the suggestions but it didn't help. I need to add the multiples of 3 and 5 up to but not including 100 in python.
I've tried searching through StackOverflow already.
def multiples():
total2 = 0
for x in range (1,100):
if (x % 3 == 0) or (x % 5 == 0):
total2 += x
return total2
print(multiples())
It says 3 as my output, which is obviously wrong. What am I doing wrong?
The return statement is inside the loop in the if block, so it's going to return on the first matching number, which is 3.
Simply move it out of the loop:
def multiples():
total2 = 0
for x in range (1,100):
if (x % 3 == 0) or (x % 5 == 0):
total2 += x
return total2
print(multiples())

Categories

Resources