diff --git a/cs-python-loops-challenge.ipynb b/cs-python-loops-challenge.ipynb index ed55ae9..4d2d7bd 100644 --- a/cs-python-loops-challenge.ipynb +++ b/cs-python-loops-challenge.ipynb @@ -110,31 +110,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def squaresTo(num):\n", " squares = 0\n", " #add your code with the required loop here\n", - " \n", - "\n", + " for r in range(1,num+1):\n", + " squares += r**2\n", " return squares" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2870" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", - "squaresTo(100)" + "squaresTo(20)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -151,23 +162,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "def sumOfFactors(factor, num):\n", " sumF=0\n", - " # add the loop here\n", - " \n", - " \n", + " for r in range(1,num+1):\n", + " if r%factor==0:\n", + " sumF += r\n", " return sumF" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the function\n", "sumOfFactors(3, 100)" @@ -175,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ @@ -191,14 +213,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", " sum2F = 0\n", - " #add your code with loop here\n", - " \n", + " for r in range(1,num+1):\n", + " if r%f1== 0 and r%f2==0:\n", + " sum2F +=r\n", "\n", " return sum2F\n", " " @@ -206,9 +229,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#call the function\n", "sumOfMultiplesOfTwoFactors(3,4,100)" @@ -216,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, "outputs": [], "source": [ @@ -225,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ @@ -243,26 +277,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def primeTest(num):\n", - " \n", - " #hint: start by assuming the number IS a prime\n", - " isPrime = True\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", - " return isPrime\n" + " if num > 1:\n", + " for factor in range(2,num):\n", + " if num % factor == 0:\n", + " return False\n", + " else:\n", + " return True\n", + " else:\n", + " return False\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# run the function\n", "primeTest(23)" @@ -270,7 +315,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -280,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -289,11 +334,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "assert not primeTest(15), \"error testing 15 is NOT prime\"" + "assert primeTest(15), \"error testing 15 is NOT prime\"" ] } ],