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
117 changes: 81 additions & 36 deletions cs-python-loops-challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -151,31 +162,42 @@
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -191,32 +213,44 @@
},
{
"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",
" "
]
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -225,7 +259,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -243,34 +277,45 @@
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -280,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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\""
]
}
],
Expand Down