{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Chapter 4: Functions\n", "\n", "Authors \n", "- Vincent Delecroix\n", "\n", "License \n", "CC BY-SA 3.0\n", "\n", "The aim of this fourth chapter is to introduce you to write yourself\n", "your own function. This is useful if you want to reuse many times the\n", "same piece of code while not copy/pasting it.\n", "\n", "The structure of a function is always the same:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def f(n):\n", " f = n.prime_factors()\n", " return f[-1] + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.1\n", "\n", "Write a function computing the area of a triangle from the length of the\n", "three sides:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.2\n", "\n", "The duration of sunshine $D(\\beta, d)$ on a given place on Earth is\n", "given by the formula\n", "\n", "$$D(\\beta,d) = 24 - \\frac{24}{\\pi}\\arccos\\left( \\tan \\beta \\cdot\n", "\\tan\\left(\\arcsin\\left(\\sin(\\kappa)\\cdot \\sin\\left(\\frac{2\\pi}{365}d\n", "\\right)\\right)\\right)\\right)$$\n", "\n", "where $\\kappa=\\frac{23.44}{180}\\pi$ is the inclination of the earth in\n", "radian, $d\\in[0,365]$ is the number of days after the spring equinox and\n", "$\\beta\\in[-\\pi/2,\\pi/2]$ is the latitude of the place in question. Write\n", "the function `D(beta, d)` :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct the list of duration of sunshine in Marseille for the 31 days\n", "of the month of July 2017:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.3\n", "\n", "Let the sequence $u_{n+1}= \\frac{1}{1+u_n^2}$ with $u_0=0$. Write a\n", "function `U(n)` which returns the value of $u_n$. Compute $u_{20}$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.4\n", "\n", "Write a function `product_of_digits(n)` which returns the product of the\n", "digits of $n$ written in base $10$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.5\n", "\n", "The number 6 is called a *perfect* number, because it is equal to the\n", "sum of its proper divisors: $6=1+2+3$. Write a function `is_perfect(n)`\n", "which returns `True` or `False` whether the number `n` is perfect:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find all four perfect numbers below $10000$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find all four perfect numbers below $10^6$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " (abundant, perfect, non-abundant\n", "numbers)" ] } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } }, "nbformat": 4, "nbformat_minor": 2 }