{ "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": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1000^1001 > 1001^1000" ] }, { "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?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lost" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1# 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": [ "- when the variable `a` is 2?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "you win" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2# 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": [ "- when the variable `a` is 3?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lost" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3# 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": [ "- when the variable `a` is 15?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "lost" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 15# 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": [ "### 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": [ "TwinPrimeNumbers = [];\n", "for p in prime_range(2,10000):\n", " if (p+2).is_prime():\n", " TwinPrimeNumbers.append((p,p+2))\n", "TwinPrimeNumbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "205" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(TwinPrimeNumbers)" ] }, { "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": [ { "data": { "text/plain": [ "-399" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min([a^b-b^a for a in range(6) for b in range(6)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "399" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max([a^b-b^a for a in range(6) for b in range(6)])" ] }, { "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": [ { "data": { "text/plain": [ "[7, 2, 5, 1]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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": [ { "data": { "text/plain": [ "972" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MaximalSumDigits = 0;\n", "for a in srange(100):\n", " for b in srange(100):\n", " Sum = sum((a^b).digits())\n", " if MaximalSumDigits < Sum:\n", " MaximalSumDigits = Sum\n", "MaximalSumDigits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3.6\n", "\n", "Solve [Euler problem 4](https://projecteuler.net/problem=4) about\n", "palindromes. A palindromic number reads the same both ways. The largest\n", "palindrome made from the product of two 2-digit numbers is\n", "$9009 = 91 \u00d7 99$.\n", "\n", "Find the largest palindrome made from the product of two 3-digit\n", "numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "906609" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LargestPalindrome = 0;\n", "for a in srange(100,1000):\n", " for b in srange(a,1000):\n", " if Word((a*b).digits()).is_palindrome():\n", " LargestPalindrome = max(LargestPalindrome,a*b)\n", "LargestPalindrome" ] }, { "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": [ "leven = []; lodd=[]\n", "for x in l:\n", " if x%2 == 0:\n", " leven.append(x)\n", " else:\n", " lodd.append(x)" ] }, { "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": [ { "data": { "text/plain": [ "I am not zero" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool([])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in ZZ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3 in ZZ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3 in QQ" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in [3, 5, 2, 1, 2, 8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' in 'Saint-Flour'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'z' in 'Saint-Flour'" ] }, { "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": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 not in Primes()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "VowelsCount = 0\n", "for i in range(0,len(s)):\n", " if s[i] in 'aeiouy':\n", " VowelsCount = VowelsCount + 1\n", "VowelsCount" ] }, { "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": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "27" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ConsonantsCount = 0\n", "for i in range(0,len(s)):\n", " if s[i] not in 'aeiou ?':\n", " ConsonantsCount = ConsonantsCount + 1\n", "ConsonantsCount" ] }, { "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": [ { "data": { "text/plain": [ "150" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PythagoreanTripleCount = 0\n", "for a in range(100):\n", " for b in range(a,100):\n", " for c in range(b,100):\n", " if a^2+b^2 == c^2:\n", " PythagoreanTripleCount += 1\n", "PythagoreanTripleCount" ] }, { "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": [ { "data": { "text/plain": [ "(200, 375, 425)" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for a in range(1000):\n", " for b in range(a,1000):\n", " for c in range(b,1000):\n", " if a^2+b^2 == c^2 and a+b+c==1000:\n", " PythagoreanTriple = (a,b,c)\n", "PythagoreanTriple" ] }, { "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": [ { "data": { "text/plain": [ "a twin number!" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "TripleTwins = 0;\n", "for p in prime_range(2,10000):\n", " if (p+2).is_prime() and (p+6).is_prime():\n", " TripleTwins += 1\n", "TripleTwins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operator `not` is used for negation of a condition." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "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 38](https://projecteuler.net/problem=39) (integer\n", " right triangles, aka pythagorean triples)\n", "- [Euler problem 39](https://projecteuler.net/problem=53) (binomials\n", " greater than a milion)\n", "- [Euler problem 40](https://projecteuler.net/problem=57) (continued\n", " fractions)" ] } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } }, "nbformat": 4, "nbformat_minor": 2 }