15-112 - Fundamentals of Programming - Loops
15-112 - Fundamentals of Programming - Loops
1. for loops
2. nested for loops
3. while loops
4. break and continue
5. isPrime
6. nthPrime
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 1 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def sumToN(n):
# helper function
return n*(n+1)//2
print(sumToN(5) == 0+1+2+3+4+5)
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 2 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
Once again:
def sumOfOddsFromMToN(m, n):
if (m % 2 == 0):
# m is even, add 1 to start on an odd
m += 1
total = 0
for x in range(m, n+1, 2):
total += x
return total
And again:
# Here we will range in reverse
# (not wise in this case, but instructional)
And again:
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 3 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
And again:
# This is the worst way so far -- too confusing.
printCoordinates(4, 5)
printStarRectangle(5)
And again:
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 4 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def printMysteryStarShape(n):
for row in range(n):
print(row, end=" ")
for col in range(row):
print("*", end=" ")
print()
printMysteryStarShape(5)
def leftmostDigit(n):
n = abs(n)
while (n >= 10):
n = n//10
return n
print(leftmostDigit(72658489290098) == 7)
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 5 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def nthMultipleOf4or7(n):
found = 0
guess = -1
while (found <= n):
guess += 1
if (isMultipleOf4or7(guess)):
found += 1
return guess
print(sumToN(5) == 1+2+3+4+5)
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 6 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def buggySumToN(n):
# note: this not only uses a "while" instead of a "for" loop,
# but it also contains a bug. Ugh.
total = 0
counter = 0
while (counter <= n):
counter += 1
total += counter
return total
print(buggySumToN(5) == 1+2+3+4+5)
print(sumToN(5) == 1+2+3+4+5)
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 7 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def readUntilDone():
linesEntered = 0
while (True):
response = input("Enter a string (or 'done' to quit): ")
if (response == "done"):
break
print(" You entered: ", response)
linesEntered += 1
print("Bye!")
return linesEntered
linesEntered = readUntilDone()
print("You entered", linesEntered, "lines (not counting 'done').")
5. isPrime video
# Note: there are faster/better ways. We're just going for clarity and simplicit
y here.
def isPrime(n):
if (n < 2):
return False
for factor in range(2,n):
if (n % factor == 0):
return False
return True
fasterIsPrime:
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 8 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
#Note: this is still not the fastest way, but it's a nice improvement.
def fasterIsPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 9 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def isPrime(n):
if (n < 2):
return False
for factor in range(2,n):
if (n % factor == 0):
return False
return True
def fasterIsPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
6. nthPrime video
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 10 of 11
15-112: Fundamentals of Programming 2/24/18, 4(43 AM
def isPrime(n):
if (n < 2):
return False
if (n == 2):
return True
if (n % 2 == 0):
return False
maxFactor = round(n**0.5)
for factor in range(3,maxFactor+1,2):
if (n % factor == 0):
return False
return True
def nthPrime(n):
found = 0
guess = 0
while (found <= n):
guess += 1
if (isPrime(guess)):
found += 1
return guess
https://www.cs.cmu.edu/~112/notes/notes-loops.html Page 11 of 11