|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "4bb0911b-c587-4373-95c6-a455e3dd2aaf", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Variables, conditionals and loops\n", |
| 9 | + "\n", |
| 10 | + "Example:\n", |
| 11 | + "\n", |
| 12 | + "A perfect number is a positive integer whose divisors (excluding itself) sum to\n", |
| 13 | + "itself. For example, $6$ is a perfect number because $6 = 1 + 2 +\n", |
| 14 | + "3$.\n", |
| 15 | + "\n", |
| 16 | + "1. How many perfect numbers are there less than 20?\n", |
| 17 | + "2. What is the next perfect number?" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 2, |
| 23 | + "id": "c80e63a8-867a-4f27-b3ea-dea37a277c5a", |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "def is_perfect(n):\n", |
| 28 | + " \"\"\"\n", |
| 29 | + " Checks if a given integer n is perfect or not.\n", |
| 30 | + "\n", |
| 31 | + " It returns True if perfect, False if not.\n", |
| 32 | + "\n", |
| 33 | + " A number is perfect if the sum of it's divisors is equal to the number itself.\n", |
| 34 | + "\n", |
| 35 | + " For example 6 = 3 + 2 + 1\n", |
| 36 | + " \"\"\"\n", |
| 37 | + " return sum(i for i in range(1, n) if (n % i == 0)) == n" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 3, |
| 43 | + "id": "0fa910a8-02a2-4451-8b10-edd95d0131fc", |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [ |
| 46 | + { |
| 47 | + "data": { |
| 48 | + "text/plain": [ |
| 49 | + "True" |
| 50 | + ] |
| 51 | + }, |
| 52 | + "execution_count": 3, |
| 53 | + "metadata": {}, |
| 54 | + "output_type": "execute_result" |
| 55 | + } |
| 56 | + ], |
| 57 | + "source": [ |
| 58 | + "is_perfect(n=6)" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 4, |
| 64 | + "id": "19e277d1-087c-46c2-ace4-b2410078766c", |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [ |
| 67 | + { |
| 68 | + "data": { |
| 69 | + "text/plain": [ |
| 70 | + "False" |
| 71 | + ] |
| 72 | + }, |
| 73 | + "execution_count": 4, |
| 74 | + "metadata": {}, |
| 75 | + "output_type": "execute_result" |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "is_perfect(n=5)" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 7, |
| 85 | + "id": "984afb25-0fa5-4922-b44a-9c73865149f6", |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "data": { |
| 90 | + "text/plain": [ |
| 91 | + "1" |
| 92 | + ] |
| 93 | + }, |
| 94 | + "execution_count": 7, |
| 95 | + "metadata": {}, |
| 96 | + "output_type": "execute_result" |
| 97 | + } |
| 98 | + ], |
| 99 | + "source": [ |
| 100 | + "checks = [is_perfect(n=value) for value in range(1, 20)]\n", |
| 101 | + "sum(checks)" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 21, |
| 107 | + "id": "ed55c270-7837-43b3-80e2-238641023782", |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "n = 496 + 1\n", |
| 112 | + "while (is_perfect(n) is False):\n", |
| 113 | + " n = n + 1" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 22, |
| 119 | + "id": "04e40eb6-780e-4b2c-b41e-37623b452c2b", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "data": { |
| 124 | + "text/plain": [ |
| 125 | + "8128" |
| 126 | + ] |
| 127 | + }, |
| 128 | + "execution_count": 22, |
| 129 | + "metadata": {}, |
| 130 | + "output_type": "execute_result" |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "n" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": 18, |
| 140 | + "id": "0c34097b-b2db-4188-99fa-ee618d81fdbd", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "data": { |
| 145 | + "text/plain": [ |
| 146 | + "True" |
| 147 | + ] |
| 148 | + }, |
| 149 | + "execution_count": 18, |
| 150 | + "metadata": {}, |
| 151 | + "output_type": "execute_result" |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "is_perfect(28)" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "id": "d152118e-2284-432a-8756-301f44458e3a", |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [] |
| 165 | + } |
| 166 | + ], |
| 167 | + "metadata": { |
| 168 | + "kernelspec": { |
| 169 | + "display_name": "Python 3 (ipykernel)", |
| 170 | + "language": "python", |
| 171 | + "name": "python3" |
| 172 | + }, |
| 173 | + "language_info": { |
| 174 | + "codemirror_mode": { |
| 175 | + "name": "ipython", |
| 176 | + "version": 3 |
| 177 | + }, |
| 178 | + "file_extension": ".py", |
| 179 | + "mimetype": "text/x-python", |
| 180 | + "name": "python", |
| 181 | + "nbconvert_exporter": "python", |
| 182 | + "pygments_lexer": "ipython3", |
| 183 | + "version": "3.13.0" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 5 |
| 188 | +} |
0 commit comments