program to find prime numbers
import math
def is_prime2(num): ''' Better method of checking for primes. ''' if num % 2 == 0 and num > 2: return False for i in range(3, int(math.sqrt(num)) + 1, 2): if num % i == 0: return False return True
is the program correct, because for ever number it just returns true
yea its woking
Indentation Error. Correct One import math
def is_prime2(num):
if num % 2 == 0 and num > 2:
return False
for i in range(3, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
return False
return True
The is_prime2 function defined below incorrectly responds that 1 is prime. In your lecture, you state: "By convention 0 and 1 are not prime."
import math
def is_prime2(num):
'''
Better method of checking for primes.
'''
if num % 2 == 0 and num > 2:
return False
for i in range(3, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
return False
return True
i thought that num being a string has to be converted to int before doing a sqrt on it
Should I change num to be a int first
i thought that num being a string has to be converted to int before doing a sqrt on it
As I read the function definition, num should be passed in as a number. By convention, it should be an int, but I would not expect num to be a str in the context of this function. In the example that is used, it is passed an int:
is_prime2(18)
Can someone please help me with the following code for a function that returns the count of prime numbers up to and including the input number?
def count_primes(num):
start = 2
end = 1000
count=0
for num in range(start, end + 1):
if num > 1 and num%num==0 and num%1==0:
for x in range(2, num):
for x<num:
if num%x==0:
not count+=1
elif num%x!=0:
count+=1
return count
It returns nothing with: Check count_primes(100)
Try this:
def count_primes(num):
primes = []
start = 2
counter = 0
for i in range(start, num + 1):
if i > 1:
for n in range(2, i):
if (i % n) == 0:
break
else:
primes.append(i)
counter+=1
print(primes)
print("Total prime numbers: ", counter)
In Python you can use else not just with if but also with loop, Let me explain you in a theoretical way:
Else statement will execute only if the loop is executed without any interruption, For example, if you've used Break function in your loop and if it'll break the loop then else block will not execute. But if loop executes without break then it'll execute else block also, Here is a simple example of it:
for i in 'Hello World': ##Run with space and without space
if i == ' ':
break
print(i, end=' ')
else:
print('\n\n\nI am else - block')
I hope this will give you clear picture of else with loop you can also use else with while loop.
@LeeZee1 Please read Definition I posted You'll understand use case scenario of else with loop, In simple term If Break is executed else won't execute. Please run the same script without space in HelloWorld I've already mention that as a comment.
Rajsardhara, Thank you. So, I see how if the 'if' statement condition is met then break is executed and then the 'else' statement does not execute.
But, I'm wondering why the code when it looks like the following doesn't work and instead gives an error:
for i in 'Hello World': ##Run with space and without space
if i == ' ':
break
print(i, end=' ')
else:
print('\n\n\nI am else - block')
it gives error:
File "
shouldn't the else statement be exactly opposite of the if statement as we've learned with prior examples in this python bootcamp course?
Like in the following example, the 'if' statement is exactly opposite the 'else' statement, and the code operates in both 'if' 'else' pairs:
for num in arr:
if arr[0]==6 and arr[1:-1]!=9:
return sum(arr)
while add:
if num != 6:
total += num
break
else:
add = False
while not add:
if num != 9:
break
else:
add=True
break
return total
Because your code is incorrect here is correct one, just be careful with indentation:
for i in 'Hello World':
if i == ' ':
break
print(i, end=' ')
else:
print('I am else - block')
num = int(input("Enter a number: ")) #prime numbers are greater than 1 if num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") # if input number is less than # or equal to 1, it is not prime else: print(num,"is not a prime number") Works flawlessly M Dhanush
On Fri, 8 Nov 2019, 4:15 pm samarthp6, [email protected] wrote:
import math
def is_prime2(num): ''' Better method of checking for primes. ''' if num % 2 == 0 and num > 2: return False for i in range(3, int(math.sqrt(num)) + 1, 2): if num % i == 0: return False return True
is the program correct, because for ever number it just returns true
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Pierian-Data/Complete-Python-3-Bootcamp/issues/95?email_source=notifications&email_token=AFK7HO5SC5IYHVTY2T2UNJDQSU7L5A5CNFSM4JKVS5N2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HX5WQPA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFK7HO7TF7ZFSPONP6DZ6W3QSU7L5ANCNFSM4JKVS5NQ .