>>>>> "tony" == tony polkich <[email protected]> writes:
The problem is that 143 == 11 * 13.
but (because of rounding) sqrt(143) == 11.
So your test, 143%11 yields 0.
You should instead test for 11*11:
You also should test up to and includign the square root because of
rounding.
#!/bin/sh
bc <<EOF
scale=0
testnum=$1
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 its root is "; rem
if (testnum == testnumroot * testnumroot){
print "\n"
print testnum; " is "; testnumroot; "squared"
print "\n"
halt
p}
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
EOF
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html