Lab VIII (27/10/2016)

A NUMBER THEORY PACKAGE


In this lab, we will write and use our own Python package. The package implements a few number theory functions which are easily understood by everyone.

The nbrs Package

Implement the functions in the order given. You can then use the functions that you have just written to write the subsequent functions.

Number theory studies the properties of integers. There are many properties easily understood by everyone. The simplest of the number theoretic properties deal with prime numbers, composite numbers and their factors. We will build a package that will help us explore these concepts.

Implement the following as functions using def. Write all your code in a file named nbrs.py

  1. proper_factors(n):
    This function finds the factors of n from 1 upto but not including the number n itself. For example, the proper factors of 12 are 1, 2, 3, 4 and 6.

  2. primes_below(n):
    This function lists all the prime numbers from 2 upto and including the number n. For example, primes_below(20) should give the answer as [2, 3, 5, 7, 11, 13, 17, 19].

  3. prime_factors(n):
    This function lists the prime factors of n, i.e., those primes that divide it. For example, prime_factors(20) should return [2, 5]

  4. perfect_numbers(n):
    This function lists all perfect numbers below n. A perfect number is one for which the sum of its proper factors equals itself. For example, 28 is a perfect number because the sum of its proper factors ([1, 2, 4, 7, 14]) = 28. If you call this function as perfect_numbers(1000), you should get [6, 28, 496].

  5. amicable_numbers(p,q):
    This function returns the value True if the given pair of numbers p and q form an amicable pair. That is, the sum of the proper factors of p should equal q and the sum of proper factors of q should equal p. Otherwise, it returns FalseCheck with the pair 220 and 284.

  6. deficient_numbers(n):
    This function lists all deficient numbers below n. A number is deficient if the sum of its proper factors is less than itself.

  7. abundant_numbers(n):
    This function lists all abundant numbers below n. A number is abundant if the sum of its proper factors is greater than itself.

Problems

Solve the following using your nbrs package. Open a terminal and start Python in it. Type type import nbrs. Then you can find all abundant numbers below 30 as nbrs.abundant_numbers(30). If everything goes well, you should get the answer as [12, 18, 20, 24, 30].
  1. Take any small prime number p (less than 15). Compute the product of (2**p)-1 and 2**p. Is it always a perfect number?

  2. Find how many primes there are below 10, 50, 100, 150, ..., 1000. Plot a graph of the number of primes. Make your observations about the growth of prime numbers.

  3. (A little trickier!!) List all the twin-primes below a given number n. Twin-primes are a pair of primes whose difference is 2. For example, the following are the twin-primes below 50.

    (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43)


  4. HAVE FUN!