Fast algorithm to test primality
import math
def isprime(num):
if num<2 or int(num)!=num: return False
if not num%2 and num>2: return False
for n in range(3,math.ceil((num-1)**.5)+1,2):
if not num%n:
return False
return True
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
Test.describe("Gets whether a number is prime")
Test.it("takes the square root")
Test.expect(not isprime(-1),"negative numbers can't be prime")
Test.expect(not isprime(5.5),"decimals can't be prime")
Test.expect(not isprime(0),"0 is not prime")
Test.expect(not isprime(1),"1 is not prime")
Test.expect(isprime(2),"2 is prime")
Test.expect(isprime(3),"3 is prime")
Test.expect(not isprime(4),"4 is not prime")
Test.expect(isprime(7),"7 is prime")
Test.expect(not isprime(9),"9 is not prime")
Test.expect(not isprime(49),"49 is prime")
Takes the square root of an int and returns a double.
double sqrt (int a,int accuracy=20) {
double out = a;
for(int i=0;i<accuracy;i++) {
out = (out+a/out)/2;
}
return out;
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(square_root)
{
It(should_take_square_root)
{
Assert::That(sqrt(4), Equals(2));
Assert::That(sqrt(16), Equals(4));
Assert::That(sqrt(169), Equals(13));
}
};
Parses an expression.
def parse(expr): #The main parser
ps = 0 #Number of open parentheses
cval = 0 #Current value
op = "+" #Current operation
accum = "" #Accumulating value
for i in range(len(expr)):
c = expr[i]
if c in ["+","-"] and not ps: #Operation not inside parens
if op=="+": #Addition
cval+=parse_fact(accum)
else: #Subtraction
cval-=parse_fact(accum)
accum = "" #Reset the value
op = c #New operation once that was calculated
else:
if c=="(": ps+=1 #Open paren
if c==")": ps-=1 #Close paren
accum+=c #Add a character to accumulating value
if op=="+": #Do the operation one more time
cval+=parse_fact(accum)
else:
cval-=parse_fact(accum)
return cval
def parse_fact(term):
ps = 0
cval = 1
op = "*"
accum = ""
for i in range(len(term)):
c = term[i]
if c in ["*","/"] and not ps:
if op=="*":
cval*=parse_val(accum)
else:
cval/=parse_val(accum)
accum = ""
op = c
else:
if c=="(": ps+=1
if c==")": ps-=1
accum+=c
if op=="*":
cval*=parse_val(accum)
else:
cval/=parse_val(accum)
return cval
def parse_val(val):
if val[0] == "(": #Parenthetical expression
return parse(val[1:-1]) #Cut off parentheses and reevaluate
else:
return float(val) #Not parenthetical
test.expect(parse("5")==5,"FAILED TEST CASE 1 (5)")
test.expect(parse("5+5")==10,"FAILED TEST CASE 2 (5+5)")
test.expect(parse("5*5")==25,"FAILED TEST CASE 3 (5*5)")
test.expect(parse("5*5+5")==30,"FAILED TEST CASE 4 (5*5+5)")
test.expect(parse("5*5+5/5")==26,"FAILED TEST CASE 5 (5*5+5/5)")
test.expect(parse("5*(5+5)/5")==10,"FAILED TEST CASE 6 (5*(5+5)/5)")
test.expect(parse("5*(5+(10/2))/5")==10,"FAILED TEST CASE 7 (5*(5+(10/2))/5)")
test.expect(parse("(60/5)-4/29-61+96")==46.86206896551724,"FAILED TEST CASE 8")