python: checking if a number is prime, specifying range - python

I have a problem with a this piece of code that I'm writing to check if a number is prime.
import sys
import math
def is_prime(num):
for j in range(2,(int(math.sqrt(num)+1))):
if (num % j) == 0:
return False
else:
return True
for a in range(1,10):
if is_prime(a):
print a
What I get is: 5, 7, 9
I don't understand why is 3 excluded? I went back to the first portion of the code and tried is_prime(3), but it returned me neither True nor False. Does this mean that 3 is outside the range of divisors I'm checking? How should I correct the code such that 3 is included in my list of primes?
Edit:
Hello again, thanks for the answers so far, so I've tried to solve the problem of truncation using int(round()). I've also tried putting the else block outside the loop, but it doesn't seem to work. Can anyone please explain why this is insufficient and suggest how I should correct it so it works? Thanks in advance! :)
import sys
import math
def is_prime(num):
for j in range(2,int(round(math.sqrt(num)+1)):
if (num % j) == 0:
return False
return True

Your function returns True as soon as it sees that the number is not divisible by any j. It should only return True if the number is not divisible by all j.
As a side-effect of fixing this bug, you'll also fix the bug whereby your function sometimes returns None (for example, when num is 3).

int() truncates, not rounds, so int(math.sqrt(3) + 1) == 2. range(2, 2) is empty, so nothing will be returned.
Another problem is that your else block is inside of the loop. If you test the first 100 numbers, you will quickly notice that your is_prime function will return True for any odd number.

The square root of 3 is less than 2, so the loop is never entered and nothing is returned.

Related

Debugging in Hungarian Maximum Matching

I wrote a code to solve the following algorithm question:
Given a number of positive integers all larger than 1, find the maximum number of pairs whose sum is a prime number. The number of positive integers is always an even number.
For example, given 2 5 6 13 2 11, the answer is 3 since 2+5=7, 6+13=19, 2+11=13.
I wrote the following code to solve the problem. I know this is not the optimal algorithm, but I just cannot find the bug in it that results in my failure in test cases.
def _isPrime(num):
for i in range(2, int(num**0.5)+1):
if num % i==0:
return False
return True
def findPairs(odds, evens, pairDict):
if len(odds)==0 or len(evens)==0:
return 0
key=' '.join(list(map(str, odds+evens)))
if key in pairDict:
return pairDict[key]
n=0
for i in range(len(evens)):
newOdds=odds.copy()
newEvens=evens.copy()
del newOdds[0]
del newEvens[i]
if _isPrime(odds[0]+evens[i]):
n=max(n,findPairs(newOdds, newEvens, pairDict)+1)
else:
n=max(n,findPairs(newOdds, newEvens,pairDict))
pairDict[key]=n
return n
numbers=list(map(int,input().split()))
odds=[i for i in numbers if i%2==1]
evens=[j for j in numbers if j%2==0]
pairDict={}
print(findPairs(odds,evens,pairDict))
Can someone help me find where the problem is. Thanks a lot!
The problem is that the recursion always tries to match the first odd number with some even number. This can go wrong if there are fewer even numbers than odd numbers because it will use up an even number that could have been used for a later match.
For example, consider "13 2 3". This code will return 0, but 2+3 is a prime.
You could fix it by also allowing an extra recursion case where the first odd number is discarded without reducing the even list.
del newOdds[0]
n=max(n,findPairs(newOdds, newEvens, pairDict)) # added line
del newEvens[i]

How can I write this code so that I do not have to round the logarithm?

I was doing a programming challenge question where you need to found out if a given number (n) is equal to 3 raised to some exponent(x)
9 is True since 3^x -> 3^3 = 9
10 is False since no x for 3^x is equal to 10
my problem occurred when 243 was entered into my code, which returned a result of 4.999 repeating.
my solution was to simply add a round function, in which I arbitrarily decided on 5 decimal places, this solved the problem.
now another problem occurred when the input was 531440, this returns an x value of 11.999998287222695 which, according to the challenge, was false, but with my code returned a true since I rounded to the fifth decimal place. I then changed my round to 10 decimal places and this fixed the code.
I was wondering if there was a way to implement my solution without having to round, as I'm sure if I plugged in enough numbers eventually another one would need to round past >10 decimal places and cause my code to fail. Please see the code below.
import math as math
def isPowerOfThree(n):
print(math.log(n,3)) #this line prints the unrounded log result
print(round((math.log(n,3)),10)) #this line prints the rounded result
return n>0 and round((math.log(n,3)),10).is_integer()
n = 243
print(isPowerOfThree(n))
The 'trick' allowing to avoid problems with chosen precision is to round to an integer and then check if this exponent gives the required result. The code below implements this approach:
import math
def isPowerOfThree(N):
if 3**round(math.log(N,3)) == N:
return True
return False
N = 531440 # 243
print(isPowerOfThree(N)) # gives False
Another approach will be to check all powers of 3 against the target number:
def isPowerOfThree(N):
x = 1
while True:
n = 3**x
if n == N:
return True
elif n > N:
return False
x += 1
And here an approach detecting directly without the need to run multiple loop runs if a number is not a power of 3:
def isPowerOfThree(N):
while True:
N , r = divmod(N, 3)
if r != 0:
return False
if N == 1:
return True
P.S. the two last approaches provide code to what Karl Knechtel mentioned in his comment to the question.

Could someone show me a good factorial code?

I just started to study Python. I must use Python3.7.
Could someone show me a working factorial code?
i tried some i found here but i always get this error:
=================== RESTART: C:\programozás\pytutorial.py ===================
Code:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
Your code is working, even though you could simply use the math library:
import math
print(math.factorial(5))
The problem does not come from your script, so maybe you should try to reinstall your python, and avoid paths with special characters as Adam Toth pointed out.
Update: to get the input and return a factorial as asked in comments
import math
print(math.factorial(int(input(">>"))))
The problem is most likely caused because you have a special character in the path to the .py file. So should use a folder like C:\programming, or anything without a special character, like 'á'.
It's very important to do like this, even if it does not solve your current problem, it can prevent many more in the future.
Ps.: Jó kiszúrni magyar programozót is :)
I see a related (older) thread here about this error
For the logic:
We have to consider:
Negative numbers
Zero
Positive numbers
So one way to write it will be:
def factorial(n):
if n < 0:
result = "Factorial doesn't exist for negative numbers"
elif n == 0:
result = 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result
You can try the concept of recursion as well.
To get the factorial of a number "num":
print(factorial(num))
Make sure you indent the code properly, indentation is important in python.
Hope it helps!
Python Code of Factorial Using Recursive Function:
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1)
factorial(5)
Note: The First Condition will only meet when the input is 0 or 1 and in else block n will be recursively multiplying n * n - 1.

Python Debugging: weird event when negative sign is in front

I was doing an assignment question.
Write a program, divisible_by_11(num), to return the divisibility of num
by 11. i.e. divisible_by_11(num) should return True is num is divisible by 11,
or False otherwise. Implement this function recursively and using the following algorithm.
Note: a number is divisible by 11 if the difference between the sum of odd digits
and the sum of even digits is divisible by 11. Note that 0 is divisible by 11.
def divisible_by_11(num):
def helper(num,i):
if num==0:
return 0
elif i%2:
return helper(num//10,i+1)-num%10
Question is about replacing the above line with return -num%10+ helper(num//10,i+1)
elif i%2==0:
return num%10+helper(num//10,i+1)
return helper(num,0)%11==0
this code works, however if I wrote return -num%10+ helper(num//10,i+1) instead of return helper(num//10,i+1)-num%10, my code would fail. Can anyone tell me what's happening?
The issue with the alternate form is due to order of operations. The unary-minus operator binds more tightly than the mod operator. That is, the expression -num%10 is interpreted as (-num) % 10, and the modulus of a negative number is not the same thing as its last digit.
There are a few ways you could fix it. The easiest would probably be to add your own parentheses so that the mod happens first: return -(num%10) + helper(num//10,i+1) should work as expected.
Unrelated to that issue, I'm very skeptical that your assignment allows you to do %11 in the final return, since that operation is all you would need to solve the whole problem without the rest!
Instead, I suspect it wants you to use recursion for that part too (and maybe not for adding up the digits). Instead of return helper(num,0)%11==0, try:
while num > 10:
num = helper(num, 0)
return num == 0

Why does my Python function work in the console, but not when called within code?

I'm trying to learn Python by working through the problems on the Project Euler website. I know exactly what I want my code to do, and my method works on paper, but I can't make the code work.
GitHub link: https://github.com/albyr/euler-python/blob/master/euler3.py
I have created two functions, one which works out the factors of the target number, and one which checks whether a given number is prime.
# Function that finds all the factors of a given number
def findfactors(n):
# for i in range(1,int(sqrt(n)+1)):
for i in range(1,n+1):
if n/i == int(n/i):
factors.append(i)
# Function that checks if a number is prime
def checkprime(n):
# Trial division
for i in range(2,int(sqrt(n)+1)):
if n/i == int(n/i):
# Number gives a remainder upon division and therefore is not prime
isprime = False
break
else:
isprime = True
if isprime == True:
return True
elif isprime == False:
return False
I'm sure that to experts that code looks horrible. But it works if I use the Python shell:
>>> checkprime(9)
False
>>> checkprime(79)
True
>>> checkprime(factors[3])
True
But when I run the program with F5 I get:
Traceback (most recent call last):
File "/home/alby/euler-python/euler3.py", line 45, in <module>
checkprime(factors[i])
File "/home/alby/euler-python/euler3.py", line 32, in checkprime
if isprime == True:
UnboundLocalError: local variable 'isprime' referenced before assignment
If I call the checkprime function from within the program with a hardcoded number (e.g. checkprime(77)) I get no output at all. I'm certain that this is something basic about the way that Python works that I don't understand, but I cannot for the life of me work out what.
Any suggestions?
In your Github code, we can see that you're trying to call checkprime(1) (on the first iteration through your last loop).
# Check each factor to see if it is prime or compound
for i in range(0,len(factors)):
print (factors[i])
# Why can't I call checkprime here, like this? It works in the console.
checkprime(factors[i])
But look at your code:
def checkprime(n):
# Trial division
for i in range(2,int(sqrt(n)+1)):
if n/i == int(n/i):
# Number gives a remainder upon division and therefore is not prime
isprime = False
break
else:
isprime = True
If n = 1, then range(2, int(sqrt(1)+1)) is range(2,2) which is empty... so isprime never gets set, because the loop body never gets run.
Keep in mind that the arguments to range() are a half-open interval - range(x,y) is "integers starting at x and ending before y". Thus range(2,3) = [2] and range(2,2) = [].
Another issue here is that findfactors() is returning 1 as the first factor - this is probably not what you want:
def findfactors(n):
# for i in range(1,int(sqrt(n)+1)):
for i in range(1,n+1):
For prime factorization checking, you probably want to start at 2, not 1 (since everything is divisible by 1).
Also, this code is redundant:
if isprime == True:
return True
elif isprime == False:
return False
You can really just write this as...
return isprime
Or you can go one step better and never use isprime in the first place - just replace isprime = True with return True and isprime = False with return False.
Finally, a shorthand for int(n/i) is n // i - Python's // operator does integer division.
In terms of no output printing, simply use print(checkprime(77)) instead when running from F5 and you should get your output. When running from a call, python prints nothing (or at least, only prints the last command) by default.

Categories