8 Exercises I
8.1 Exercises 1: Writing functions
E1.1 IsMonth
Write a function IsMonth which takes a single numerical value as input and returns TRUE iff that value may represent a month of the year.
E1.2 IsLeapYear
Write a function IsLeapYear which takes a single numerical value as input and returns TRUE iff that value may represent a leap year. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
E1.3 Bmi
Write a function Bmi which calculates the Body Mass Index (BMI) given weight (in kilograms) and height (in meters) as inputs.
\[bmi =\frac{weight}{height^2}\]
E1.4 FeetToMeters
Write a function FeetToMeters which converts a numeric value in feet to meters. Given \[1 ~ feet = 0.3048 ~ meters\]
E1.5 SqFeetToSqMeters
Use your implementation of FeetToMeters to convert a numeric value from square feet to square meters.
8.2 Exercises 2: Control flow
E2.1 SumMultiples
Problem 1, Project Euler: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a function SumMultiples to find the sum of all the multiples of 3 or 5 below 1000.
E2.2 SumEvenFib
Problem 2, Project Euler: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
\[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\]
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
E2.3 Hadley, 6.6.1.1
Hadley, 6.6.1.1: Explain the following results:
E2.4 Hadley, 6.5.4.3
Hadley, 6.5.4.3: Without running the following code, answer the questions in the comment:
E2.5 IsPalindrome
A palindrome is a sequence of characters which reads the same backward as forward. A number is a palindrome if its value is the same if it were written backwards. For example, 121, 53935, 11 are all palindrome numbers.
Write a function IsPalindrome which takes an integer x as input and returns TRUE iff it is a palindrome.
E2.6 MaxPalindrome
Problem 4, Project Euler The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.