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
72 changes: 51 additions & 21 deletions cs-python-loops-challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
},
{
Expand Down Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -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",
" "
]
Expand All @@ -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,
Expand Down Expand Up @@ -243,14 +281,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def primeTest(num):\n",
" \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",
Expand All @@ -265,7 +307,7 @@
"outputs": [],
"source": [
"# run the function\n",
"primeTest(23)"
"primeTest(17)"
]
},
{
Expand Down Expand Up @@ -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,
Expand Down