In Mathematics an *abundant number* is defined as follows:

An abundant number is an integer n for which the sum of its proper positive divisors (excluding n itself) add up to more than n.

A simple example of an *abundant number* is the number **12**.

The proper positive divisors of 12 are those integers the divide exactly into 12 with no remainder (excluding 12 itself) they are: 1, 2, 3, 4 and 6

If we add these numbers together then 1+2+3+4+6=16 which is greater than 12. This makes **12** an abundant number.

There are very few abundant numbers which are odd. The smallest odd abundant number is 945. Write a program to find the next two odd abundant numbers after 945.

This exercise is closely related to the *perfect numbers* problem solved in Topic 4 so taking that model answer from would be a good starting point for this exercise.

In [1]:

#start from 945 count = 0 odd_pn_a945=[] n=945 #empty list to add numbers n=945 while count<2: n=n+1 sum_n=0 for i in range(1,n): if (n%i==0): sum_n+=i if (sum_n>n) and n%2!=0: count=count+1 print (n, "is an abundant number.")

1575 is an abundant number.
2205 is an abundant number.

Write a program that reads in a date in the form of 8 integers with **no** spaces (i.e. ''ddmmyyyy'') and returns the date in the form:

**date month year**

where

**date**is in the ordinal form, 1st, 2nd, 3rd, ..., 30th, 31st.**month**is the usual 3 character code, Jan, Feb, Mar, ..., Dec.

The program should check whether the date entered is valid or not and should exit gracefully with an error message if an invalid date is entered.

**Hint** (for checking valid dates): leap years are exactly divisible by 4, century years are only leap years if they are exactly divisible by 400.

In [18]:

n=str(input('enter the date in the form ddmmyyyy')) if len(n)!=8: print ("please enter date in the form ddmmyyyy") year = int(n[4:8]) month_in = int(n[2:4]) day_in = int(n[0:2]) valid_date=0 #if int(n[0])>3 and int(n[0]=0): # print ("Please enter a valid day") if int(n[0:2])==(11 or 12 or 13): day = 'th' elif int(n[1])==1: day ='st' elif int(n[1])==2: day = 'nd' elif int(n[1])==3: day = 'rd' else: day = 'th' months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] month_out = months[month_in-1] if month_in>12 or month_in<1: valid_date=1 print ("Please Enter a valid date, months range from 01-12, ") daysInMonth = [31,29,31,30,31,30,31,31,30,31,30,31] if day_in > daysInMonth[month_in-1]: print ("Please enter a valid date. This month does not have enough days") valid_date=1 if month_out == 'Feb' and int(n[0:2])==29: if year%100==0 and year%400!=0: print ("Please enter a valid date. This month does not have more than 28 days") valid_date=1 elif year%4!=0: #and year%100!=0: print ("Please enter a valid date. This month does not have more than 28 days") valid_date=1 if valid_date==0: print (n[0:2],day, month_out, year)

enter the date in the form ddmmyyyy03112000
03 rd Nov 2000

In [ ]:

print (year/400,year//400)

A very simple (**and inefficient**) sort algorithm is called the **“bubble sort”** algorithm.

Take a list, **a**, with N elements that should be sorted in ascending numerical order, such that the smallest number is the first element and the largest number is the last element, i.e: a[i] < a[i+1] for all i.

Passing through the list once, exchanging neighbouring elements that are in descending order, will end up with the largest integer at the bottom of the list.

Passing through the list a second time, exchanging neighbouring elements that are in reverse numerical order in the same way, we will end up with the next-to-largest integer at the next-to-bottom location in the list.

It follows therefore that to completely sort the list it is necessary to “pass” through the list N-1 times, where N is the number of elements in the array.

This algorithm is illustrated by means of an example:

For example, take an list of 5 integers, where:

a = [1,4,3,5,2]

which are to be sorted in ascending numerical order. In this case we take successive pairs of numbers from the array and swap them over if the ith number is greater than the (i+1)th number so e.g.

First pass through the numbers:

*consider 1 and 4:* 1 is less than 4 so **no change**

*consider 4 and 3:* 4 is greater than 3 so **swap them over**

*consider 4 and 5:* 4 is less than 5 so **no change**

*consider 5 and 2:* 5 is greater than 2 so **swap them over**

So, at the end of the first pass the list of 5 numbers reads:

a=[1,3,4,2,5]

As there are 5 numbers in the list we need 4 passes through the list:

At the end of the 2nd pass the list reads:

a=[1,3,2,4,5]

At the end of the 3rd pass the list reads:a=[1,2,3,4,5]

At the end of the 4th pass the list reads:a=[1,2,3,4,5]

The array is now sorted in ascending numerical order.

Write an algorithm to perform a **"bubble sort"** on a list containing 6 real numbers. The list should be ordered in increasing number. Care should be taken when "swapping" numbers.

In [20]:

def bubblesort(): m=eval(input('enter six numbers to sort, separted by commas ')) l=list(m) for i in range (len(l)): for j in range(i+1,len(l)): if (l[j]<l[i]): temp = l[j] l[j] = l[i] l[i] = temp print (l) bubblesort()

enter six numbers to sort, separted by commas 3,1,6,4,2,8
[1, 2, 3, 4, 6, 8]

In [ ]:

```
```