what is the maximum remainder that bc can take?
the attached prime test works for all numbers I've tried so far except 143. 
Using 143 the test gives the false answer that 143 is 11 squared.

scale=0
testnum=2039
if (testnum < 0){
    testnum = testnum * (-1)
}
if (testnum%2 == 0){
    print testnum; " is an even number"
    print "\n"
    halt
}
testnumroot=sqrt(testnum)
#print "root of the test number is "; testnumroot
#rem=testnum%testnumroot
#print "remainder of division of testnum and it's root is "; rem
if(testnum%testnumroot == 0){
    print "\n"
    print testnum; " is "; testnumroot; "squared"
    print "\n"
    halt
}
quotient=3
while (quotient < testnumroot){
    trying=testnum%quotient
    if (trying !=0){
        quotient = quotient+2
    }
    if (trying == 0){
        otherfactor=testnum/quotient
        print "\n"
        print testnum; " is not a prime"
        print "\n"
        print otherfactor; " and "; quotient; "are factors"
        print "\n"
        halt
    }

}
print testnum; " is prime"
print "\n"

quit   
    
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to