From 8d25d023783f546e87f81e1ae652fc2788ec6825 Mon Sep 17 00:00:00 2001 From: patelnilay Date: Fri, 13 Oct 2017 14:16:08 +0000 Subject: [PATCH] completed document --- cs-python-loops-challenge.ipynb | 134 +++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 30 deletions(-) diff --git a/cs-python-loops-challenge.ipynb b/cs-python-loops-challenge.ipynb index ed55ae9..2cb946b 100644 --- a/cs-python-loops-challenge.ipynb +++ b/cs-python-loops-challenge.ipynb @@ -42,7 +42,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -58,7 +58,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "See how the loop in the code above goes from 1 (incl) up to (but not including) num+1 ?\n", + "See how the loop in the code aboveh goes from 1 (incl) up to (but not including) num+1 ?\n", "

Also, see the line:

\n", "\n", "```python\n", @@ -76,9 +76,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5050" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", "sumTo(100)" @@ -86,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -98,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Exercise - write the code for the following functions as described\n" + "# Exercise - write the code for the following functions as described\n" ] }, { @@ -110,23 +121,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def squaresTo(num):\n", " squares = 0\n", " #add your code with the required loop here\n", - " \n", - "\n", + " for i in range(1,num+1):\n", + " squares = squares + i**2\n", " return squares" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "338350" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", "squaresTo(100)" @@ -134,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -151,23 +173,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def sumOfFactors(factor, num):\n", - " sumF=0\n", - " # add the loop here\n", - " \n", - " \n", - " return sumF" + " num1 = 0\n", + " i = 0\n", + " while factor * i <= num:\n", + " num1 += factor * i\n", + " i=i+1\n", + " return num1\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the function\n", "sumOfFactors(3, 100)" @@ -175,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -191,24 +225,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", " sum2F = 0\n", - " #add your code with loop here\n", + " i = 0\n", + " while (f1 * f2) * i <=num:\n", + " sum2F += (f1*f2) * i\n", + " i = i + 1\n", " \n", - "\n", + " \n", " return sum2F\n", " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#call the function\n", "sumOfMultiplesOfTwoFactors(3,4,100)" @@ -243,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -252,6 +300,14 @@ " #hint: start by assuming the number IS a prime\n", " isPrime = True\n", " \n", + " for i in range(2, 100):\n", + " if num % i == 0 and i != num:\n", + " print(\"not prime\")\n", + " isPrime = False\n", + " break\n", + " \n", + " \n", + " \n", " #write your code using a loop to test whether a number is a prime\n", " #hint: when is a number NOT a prime??\n", " \n", @@ -260,12 +316,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "not prime\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# run the function\n", - "primeTest(23)" + "primeTest(15)" ] }, {