Professional Documents
Culture Documents
COMP1126 Lab2
COMP1126 Lab2
Department of Computing
COMP1126–Introduction to Computing I
Lab 2
1. The roots of a quadratic equation ax2+bx+c are given by the quadratic formula
b b 2 4ac b b 2 4ac
root1 root 2
2a 2a
Write a function quadRoots in python which returns the greater of the two roots.
However, the root is returned when the discriminant (i.e. b2-4ac) is positive; when it is
negative print a message that there are no real roots.
e.g.
quadRoots(15,3,6) -> “No real roots”
quadRoots(6,23,20) -> -1.3333333333333333
>>> perfectSquare(6)
False
>>> perfectSquare(9)
True
3. A function isPrime() is given to you that given an integer as an argument returns True
if it is prime and False otherwise. [Note: A number is said to be prime if it divisible by
only 1 and itself. For example 2,3,5,7 are prime numbers, but 4, 6, 8 and 9 are not.]
e.g.
isPrime(5) -> True
isPrime(6) -> False
def isPrime(number):
if number > 1 :
for x in range(2,number):
if number % x == 0:
return False
return True
else:
return False
Copy this function in your code file. Write a function addPrimes that sums all prime
numbers between 1 and x (where x is greater than 2). You need to use the function
isPrime to check if a given number is prime.
e.g.
>>> addPrimes(5)
10
>>> addPrimes(200)
4227
>>> addPrimes(3)
5