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)))
Related
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 5 days ago.
This post was edited and submitted for review 5 days ago.
Improve this question
Give an iterative function, gIter(a, b) which gives greatest common denominator(gcd). For example, between 21 and 49 the gcd is 7. And between 54 and 108, 54 is the gcd.
Need to understand why this loop won't work? It just keeps
printing the min(a,b) statement.
How to rewrite but with still using if else?
What is going wrong in code 1 and code2?
Code 1:
def giter(a, b):
if a>=0 and b>=0:
if a > b:
test = b
if b > a:
test = a
if test > 1:
if a % test == 0 and b % test == 0:
return test
else:
if test == 1:
return 1
test -= 1
Code 2:
def giter(a, b):
test = min(a,b)
if a>=0 and b>=0:
if test > 1:
if (a % test) == 0 and (b % test) == 0:
return test
test -= 1
elif test == 1:
return 1
else:
print('Enter value greater than 0')
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
This code throws these error:
while len(digits) > 1:
TypeError: object of type 'int' has no len()
but my variable digits is a list right? i guess there must be something i dont understand on list-comprehensions
def per(n):
if len(str(n)) == 1:
return n
else:
digits = [int(i) for i in str(n)]
count = 1
while len(digits) > 1: # TypeError:object of type 'int' has no len()
result = 1
for j in digits:
result *= j
digits = result
count += 1
return count
print(per(716235))
def per(n):
if len(str(n)) == 1:
return n
else:
digits = [int(i) for i in str(n)]
count = 1
while len(digits) > 1:
result = 1
for j in digits:
result *= j
digits = result # <-------- Right here you are setting digits
# <-------- as result which is an integer
count += 1
return count
print(per(716235))
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
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
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 4 years ago.
Improve this question
It's really basic stuff but it gets on my nerves;
Why this code doesn't work properly?
for i in range(0, 101):
if i % 3 == 0 and i != 0:
print '{} Three'.format(i)
elif i % 5 == 0 and i % 3 == 0 and i != 0:
print '{} FiveThree'.format(i)
else:
print "{}".format(i)
Is it because of conditions? So if I want to write the code in this form I must write the complicated condition first and then the simple one?
Any number that is a multiple of both 3 and 5 will make the first if condition true and will never be checked against the second if condition.
You should flip the conditions:
for i in range(0, 101):
if i % 5 == 0 and i % 3 == 0 and i != 0:
print('{} FiveThree'.format(i))
elif i % 3 == 0 and i != 0:
print('{} Three'.format(i))
else:
print("{}".format(i))
But this is wasteful. It checks i % 3 == 0 and i != 0 twice.
I'm leaving the optimization as an exercise.
Your second condition is subset of first condition, so if will never go in that block. Change the order of conditions, it will work
for i in range(0, 101):
if i % 5 == 0 and i % 3 == 0 and i != 0:
print '{} FiveThree'.format(i)
elif i % 3 == 0 and i != 0:
print '{} Three'.format(i)
else:
print "{}".format(i)
The if, elif, else clauses are mutually exclusive. One option is to put the strongest conditions first as DeepSpace suggests. Another option is to have multiple levels of conditions.
for i in range(0, 101):
if i % 3 == 0 and i != 0:
if i % 5 == 0:
print '{} FiveThree'.format(i)
else:
print '{} Three'.format(i)
else:
print "{}".format(i)