diff --git a/cs-python-loops-challenge.ipynb b/cs-python-loops-challenge.ipynb index ed55ae9..96b698e 100644 --- a/cs-python-loops-challenge.ipynb +++ b/cs-python-loops-challenge.ipynb @@ -81,7 +81,34 @@ "outputs": [], "source": [ "#run the funtion\n", - "sumTo(100)" + "sumTo(200)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sumTo(5) #1+2+3+4+5 =15" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sumTo(2) # 1+2=3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sumTo(10)" ] }, { @@ -115,10 +142,10 @@ "outputs": [], "source": [ "def squaresTo(num):\n", - " squares = 0\n", + " squares=0\n", " #add your code with the required loop here\n", - " \n", - "\n", + " for i in range(1,num+1):\n", + " squares+=i**2 #You tell the program to square the i\n", " return squares" ] }, @@ -158,7 +185,8 @@ "def sumOfFactors(factor, num):\n", " sumF=0\n", " # add the loop here\n", - " \n", + " for i in range(0,num+1,factor):\n", + " sumF+=i\n", " \n", " return sumF" ] @@ -198,8 +226,9 @@ "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", " sum2F = 0\n", " #add your code with loop here\n", - " \n", - "\n", + " for i in range(1,num):\n", + " if i%f1==0 and i%f2==0:\n", + " sum2F+=i\n", " return sum2F\n", " " ] @@ -214,6 +243,15 @@ "sumOfMultiplesOfTwoFactors(3,4,100)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sumOfMultiplesOfTwoFactors(3,5,1000)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -243,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -251,6 +289,10 @@ " \n", " #hint: start by assuming the number IS a prime\n", " isPrime = True\n", + " i = 2\n", + " while(i < num):\n", + " if num % i == 0:\n", + " isPrime = False\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", @@ -265,7 +307,7 @@ "outputs": [], "source": [ "# run the function\n", - "primeTest(23)" + "primeTest(17)" ] }, { @@ -302,18 +344,6 @@ "display_name": "Python 3", "language": "python", "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.2" } }, "nbformat": 4,