---
# System prepended metadata

title: Algorithms

---

# Algorithms

## Find the Largest Number in a List

Problem: Given a list of positive numbers, return the largest number of the list.

Inputs: A list L of positive numbers. This list must contain at least one number. (Asking for the largest number in a list of no numbers is not a meaningful question.)

Outputs: A number n, which will be the largest number of the list.

### Solution 1

Algorithm:
1. Set max to 0.
2. For each number x in the list L, compare it to max. If x is larger, set max to x.
3. max is now set to the largest number in the list.

Implementation:

```python
def find_max (L):
    max = 0
    for x in L:
        if x > max:
            max = x
    return max
```

### Solution 2

Algorithm:
1. If L is of length 1, return the first item of L.
2. Set v1 to the first item of L.
3. Set v2 to the output of performing find_max() on the rest of L.
4. If v1 is larger than v2, return v1. Otherwise, return v2.


Implementation:

```python
def find_max (L):
    if len(L) == 1:
        return L[0]
    v1 = L[0]
    v2 = find_max(L[1:])
    if v1 > v2:
        return v1
    else:
        return v2
```
