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
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.
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].
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]
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].
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.
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.
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.
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?
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.
(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.