Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 104 additions & 30 deletions cs-python-loops-challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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",
"<p>Also, see the line:</p>\n",
"\n",
"```python\n",
Expand All @@ -76,17 +76,28 @@
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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"
]
},
{
Expand All @@ -110,31 +121,42 @@
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -151,31 +173,43 @@
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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)"
Expand Down Expand Up @@ -243,7 +291,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down