Complete-Python-3-Bootcamp icon indicating copy to clipboard operation
Complete-Python-3-Bootcamp copied to clipboard

program to find prime numbers

Open samarthp6 opened this issue 7 years ago • 13 comments

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

samarthp6 avatar Nov 08 '19 10:11 samarthp6

yea its woking

ofirlevy6658 avatar Nov 09 '19 20:11 ofirlevy6658

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

nikhilrana015 avatar Nov 22 '19 20:11 nikhilrana015

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

jfdahl avatar Jan 04 '20 21:01 jfdahl

i thought that num being a string has to be converted to int before doing a sqrt on it

longwuyuan avatar Jan 05 '20 03:01 longwuyuan

Should I change num to be a int first

allan-1 avatar Mar 18 '20 11:03 allan-1

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)

jfdahl avatar Mar 18 '20 11:03 jfdahl

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)

LeeZee1 avatar Mar 20 '20 19:03 LeeZee1

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)

rajsardhara avatar Mar 21 '20 03:03 rajsardhara

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.

rajsardhara avatar Mar 21 '20 23:03 rajsardhara

@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 avatar Mar 22 '20 20:03 rajsardhara

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 "", line 5 else: ^ SyntaxError: invalid syntax

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

LeeZee1 avatar Mar 22 '20 23:03 LeeZee1

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')

rajsardhara avatar Mar 23 '20 00:03 rajsardhara

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 .

dhanushMindia avatar Mar 27 '20 03:03 dhanushMindia