#Problem 1 - Count the number of divisors of a positive integer n.
def num_div(n):
i=1
divisors=0
while i<=n:
if n%i==0:
divisors=divisors+1
i=i+1
return divisors

num_div(10)

4
num_div(12)

6
num_div(10000)

25


def primetest(n):
#true if n is prime
if n==1:
return false
i=2
while i<=n^(0.5):
if n%i==0:
return False
i=i+1
return True

#Problem 2 - Determines if there is a prime in the interval [n,n+13]
def prime_interval(n):
i=n
numprimes=0
while n<=i<=(n+13):
if primetest(i):
numprimes=numprimes+1
i=i+1
if numprimes>0:
return True
if numprimes==o:
return False


prime_interval(1025)

True
#Problem 3 - Takes two positive integers and returns the maximum of the two.
def int_max(a,b):
if a>b:
return a
if b>a:
return b
if a==b:
return "Error: a=b"

int_max(10,3)

10
int_max(10,10)

'Error: a=b'
int_max(100, 1253323)

1253323
#Problem 4 - Takes a list of numbers and returns the maximum value.
def list_max(L):
if len(L)==0:
print 'Error: no elements'
return
if len(L)==1:
return L[0]
currentmax=L[0]
i=1
while i<=len(L)-1:
b=L[i]
if b>currentmax:
currentmax=b
i=i+1
return currentmax

L=[1,64,23,76,15,26,100]

list_max(L)

100
def trials(n):
i=1
success=0
while i<=n:
if experiment(n):
success=success+1
i=i+1
return (success+0.0)/n

#Problem 5 - True if a and b are relatively prime.
def experiment(n):
a=randint(1,n)
b=randint(1,n)
if gcd(a,b)==1:
return True

trials(100)

0.590000000000000
trials(1000)

0.592000000000000
trials(10^4)

0.609800000000000
trials(10^5)

0.609190000000000
trials(10^6)

0.607294000000000
#Problem 6
def experiment(n):
i=n
while n<=i<=(n+14):
if is_prime(i):
return True
i=i+1
return False

#Problem 7 - True if the number of heads in n random coin tosses is at least 7.
def coinflip(n):
i=1
while i<=n:
toss=randint(0,1) #0 = heads, 1 = tails
if toss==0:
if toss==1:
i=i+1
return True
return False



coinflip(10)

False


def fliptrials(n):
i=1
success=0
while i<=n:
if coinflip(10):
success=success+1
i=i+1
return (success+0.0)/n



fliptrials(100000)

0.623570000000000
N(319/512)

0.623046875000000