{ "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 3: `if` statements\n", "\n", "Authors \n", "- Vincent Delecroix\n", "\n", "License \n", "CC BY-SA 3.0\n", "\n", "## Comparisons and `if`\n", "\n", "The comparison signs in Python and many other programming languages are\n", "as follows\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
==equality
!=difference
<less than
>greater than
<=lesser than or equal to
>=greater than or equal to
\n", "\n", "### Exercise 3.1\n", "\n", "Which number is the largest $1000^{1001}$ or $1001^{1000}$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.2\n", "\n", "Let us consider the following code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = # enter a value for a\n", "if a != 2: \n", " print('lost')\n", "elif a == 3:\n", " print('an instant, please')\n", "else: \n", " print('you win')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the above program doing\n", "\n", "- when the variable `a` is 1?\n", "- when the variable `a` is 2?\n", "- when the variable `a` is 3?\n", "- when the variable `a` is 15?\n", "\n", "### Exercise 3.3\n", "\n", "Two prime numbers $p$ and $q$ are said *twin* if $q = p + 2$. Find all\n", "twin prime numbers below 10000." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.4\n", "\n", "Find the smallest and largest integers in the set\n", "\n", "$$\\{ a^b - b^a : a \\in \\{1, 2, \\ldots, 5\\}, b \\in \\{1, 2, \\ldots, 5\\} \\}$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.5\n", "\n", "Recall that the method `digits` of an integer returns the list of its\n", "digits:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1527.digits()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve [Euler problem\n", "56](https://projecteuler.net/problem=56%20(digits%20of%20a%5Eb)) by\n", "finding the maximal sum of digits of numbers of the form $a^b$ with both\n", "$a$ and $b$ lesser than $100$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.6\n", "\n", "Solve [Euler problem 4](https://projecteuler.net/problem=4) about\n", "palindromes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.7\n", "\n", "Let us consider the following list of integers:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [123, 414, 264, 18, 689, 21, 5571, 28, 589, 12, 111, 231,\n", "158, 551, 250, 68, 5728, 2222, 4198, 571, 28, 518, 999, 444,\n", "112, 689, 672, 334, 680, 273]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Construct two lists `leven` and `lodd` that contain respectively the\n", "even and odd elements of `l`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `in` and `not in`\n", "\n", "The condition of an `if` or `elif` statement is not necessarily a\n", "comparison. Basically, any Python object would fit!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 5\n", "if a:\n", " print('I am not zero')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens under the hood is that the object `a` (here an integer) is\n", "converted to a boolean value (`True` or `False`). You can see the\n", "boolean value of an object by using `bool`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool([])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool([0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A useful construction is obtained with the keyword `in` : the result of\n", "`a in b` is whether `a` belongs to the object `b`. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 in ZZ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2/3 in ZZ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2/3 in QQ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 in [3, 5, 2, 1, 2, 8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'o' in 'Bonn'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'z' in 'Bonn'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check that an element is not in a given object use `a not in b` :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "10 not in Primes()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "5/2 not in ZZ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.8\n", "\n", "Using an `if` statement involving `in` inside a `for` loop, count the\n", "number of vowels in the string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 'How many vowels are present in this sentence?'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Count the number of consonant in the string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 'How many consonants are present in this sentence?'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.9 (Pythagorean triples)\n", "\n", "A Pythagorean triple is a triple `(a, b, c)` of positive integers so\n", "that $a^2 + b^2 = c^2$. An example is $3^2 + 4^2 = 5^2$. How many\n", "Pythagorean triples are there with $a$, $b$ and $c$ smaller than 100?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve [Euler problem](https://projecteuler.net/problem=9) by finding the\n", "unique Pythagorean triple so that $a + b + c = 1000$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combining conditions `or`, `and` and `not`\n", "\n", "To make even more complicated tests you can combine them. The main\n", "operators for this are `or`, `and`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 17\n", "if n.is_prime() and (n+2).is_prime():\n", " print('a twin number!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.10\n", "\n", "Let us call a positive integer `n` a triple twin if all of `n`, `n+2`\n", "and `n+6` are primes. How many triple twins are there smaller than\n", "$10000$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operator `not` is used for negation of a condition." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More exercises\n", "\n", "For more exercises in the same veine you can challenge yourself with\n", "\n", "- [Euler problem 30](https://projecteuler.net/problem=30) (sum of\n", " certain numbers)\n", "- [Euler problem 33](https://projecteuler.net/problem=33) (digit\n", " cancelling fractions)\n", "- [Euler problem 34](https://projecteuler.net/problem=34) (numbers\n", " which are sum of factorials of their digits)\n", "- [Euler problem 35](https://projecteuler.net/problem=35) (circular\n", " primes)\n", "- [Euler problem 36](https://projecteuler.net/problem=36) (integers\n", " palindromic in base 2 and 10)\n", "- [Euler problem 37](https://projecteuler.net/problem=37) (truncatable\n", " primes)\n", "- [Euler problem 39](https://projecteuler.net/problem=39) (integer\n", " right triangles, aka pythagorean triples)\n", "- [Euler problem 49](https://projecteuler.net/problem=49) (prime\n", " numbers with identical digits)\n", "- [Euler problem 52](https://projecteuler.net/problem=52) (numbers\n", " whose multiples have same digits)\n", "- [Euler problem 53](https://projecteuler.net/problem=53) (binomials\n", " greater than a milion)\n", "- [Euler problem 57](https://projecteuler.net/problem=57) (continued\n", " fractions)" ] } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } }, "nbformat": 4, "nbformat_minor": 2 }