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
101 changes: 76 additions & 25 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 Down Expand Up @@ -76,17 +76,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"5050"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the funtion\n",
"sumTo(100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -110,31 +121,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def squaresTo(num):\n",
" squares = 0\n",
" squares=0\n",
" #add your code with the required loop here\n",
" for i in range(1,num+1):\n",
" squares+=i**2\n",
" \n",
"\n",
" return squares"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 164,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"338350"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the funtion\n",
"squaresTo(100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -151,31 +174,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"def sumOfFactors(factor, num):\n",
" sumF=0\n",
" # add the loop here\n",
" \n",
" for i in range(0,num,factor):\n",
" sumF+=i\n",
" \n",
" return sumF"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 174,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1683"
]
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the function\n",
"sumOfFactors(3, 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -191,32 +226,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
"def sumOfMultiplesOfTwoFactors(f1,f2,num):\n",
" sum2F = 0\n",
" #add your code with loop here\n",
" \n",
" for i in range(0,num+1,f1*f2):\n",
" sum2F+=i\n",
"\n",
" return sum2F\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 170,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#call the function\n",
"sumOfMultiplesOfTwoFactors(3,4,100)"
"sumOfMultiplesOfTwoFactors(3,4,50)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 171,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -225,7 +272,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 172,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -253,6 +300,10 @@
" isPrime = True\n",
" \n",
" #write your code using a loop to test whether a number is a prime\n",
" for i in range(2, 10):\n",
" if (num % i == 0):\n",
" isPrime = False\n",
"\n",
" #hint: when is a number NOT a prime??\n",
" \n",
" return isPrime\n"
Expand All @@ -270,7 +321,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 177,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -280,7 +331,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 178,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -289,7 +340,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 179,
"metadata": {},
"outputs": [],
"source": [
Expand Down