0
So my code to test a prime number is as follows:

 PrimeTest():
    test = input ( "What number would you like to test? " )
    test = int(test)

    true = 0
    divisor = 0

    for divisor in range (2, 10):
        if ( (isinstance( test/divisor, int )== True) &  (test/divisor == int < 2) ):
            print ("This number is divisible by " + str(divisor) )

        if ( (test != divisor) & (isinstance( test/divisor, int)== False) ):
            true = true + 1
            if ( true == 8 ):
                print ("This number is prime.")

PrimeTest()

The value output always have a float type, even though it is an interger (i.e. 2.0). How do I fix that?

3
Contributors
14
Replies
65
Views
4 Years
Discussion Span
Last Post by bnn678
0

Is not syntax error but does not make any kind of sense ot do bitwise arithmetic with boolean:

(isinstance( test/divisor, int )== True) &  (test/divisor == int < 2)

You should be using % (modulus operator or remainder) and integer values.

Edited by pyTony

0

kind

Is not syntax error but does not make any kind of sense ot do bitwise arithmetic with boolean:

reason for that is that I need it to be an interger and not a float, because you need to test if the number is divisible by a whole number. Ex. (4/5 != int) but (5/5 = int = true) Make sense?

0

No, it does not, 4/5 is float number and int is of type 'type', the other formula of yours is assignment syntax error.

Edited by pyTony

0

So how do I change that without messing up the function?

Edited by bnn678: mistake

0
def prime_test():
    test = int(input("What number would you like to test? "))
    assert 0 < test < 11 * 11
    for divisor in range(2, 8): #highest prime under eleven is 7
        if divisor < test and test % divisor == 0:
            print("This number is divisible by " + str(divisor))
            break
    else:
        print('This number is prime')

prime_test()
0

By the way, this test is also supposed to test what a number is divisible by so that is part of the reason why it is not simpler.
Thanks for your help!

Edited by bnn678: to correct

0

I'm curious what line 3 does though, could you explain it?

Also, I'm trying to make this a universally usable method. So for example if you use that code and enter a number like 9997, there is an assertion error

Edited by bnn678: correction

0

Yes, that loop is only applicable until square of next prime after the limit, you should calculate the needed limit or break out of loop when divisor exceed reasonable limit. Are you trying to find smallest factor or all divisors? I havebwritten code snippet for both of those here.

0

all divisors
though all divisors in the range of 2-10 is perfectly fine

Edited by bnn678: corretion

0

I also want to be able to enter a test number to test it for divisors. So for example the program would test for a varible. Then would tell you what that code is divisible by.

0

Recently found out there is a GCD function on Python, was easy from there

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.