digit = 3
first = 1
second = 2
while digit < 1000:
answer = first * second
first = answer
second = digit
digit += 1
print(str(digit - 1) + "factorial is: " + str(answer))
digit = 3
first = 1
second = 2
while digit < 5:
answer = first * second
print("current digit: " + str(digit))
print("incremental answer: " + str(answer))
first = answer
second = digit
digit += 1
print(str(digit - 1) + "factorial is: " + str(answer))
digit = 2
first = 1
second = 2
while digit < 5:
answer = first * second
print("current digit: " + str(digit))
print("incremental answer: " + str(answer))
first = answer
digit += 1
second = digit
print(str(digit - 1) + "factorial is: " + str(answer))
# AN ALMOST CORRECT ONE
digit = 1
first = 1
second = 1
while digit < 5:
answer = first * second
print("current digit: " + str(digit))
print("incremental answer: " + str(answer))
first = answer
digit += 1
second = digit
print(str(digit - 1) + "factorial is: " + str(answer))
digit = 1
first = 1
second = 1
while digit < 4:
answer = first * second
print("first increment")
print(answer)
print(first)
print(second)
first = answer
print("second increment")
print(answer)
print(first)
print(second)
digit += 1
second = digit
print("third increment")
print(answer)
print(first)
print(second)
print(str(digit - 1) + "factorial is: " + str(answer))
# AN INCORRECT ONE:
digit = 1
first = 1
second = 1
while digit < 4:
answer = first * second
print("first increment")
print(answer)
print(first)
print(second)
first = answer
print("second increment")
print(answer)
print(first)
print(second)
second = digit
print("third increment")
print(answer)
print(first)
print(second)
digit += 1
print(str(digit - 1) + "factorial is: " + str(answer))
def factorial(number):
answer = 1
for x in range(1, number + 1):
answer = answer * x
return answer
print(factorial(4))
def alternate_factorial(number):
digit = 1
first = 1
second = 1
while digit <= number:
answer = first * second
first = answer
digit += 1
second = digit
return answer
print(alternate_factorial(4))
Optional homework assignment¶
Figure out why factorial()
and alternate_factorial()
are doing the same thing.
def recursive_factorial(number):
if number <= 1:
return number
else:
return number * recursive_factorial(number - 1)
print(recursive_factorial(4))
def loud_recursive_factorial(number):
print("calculating factorial of " + str(number))
if number <= 1:
return number
else:
return number * loud_recursive_factorial(number - 1)
print(loud_recursive_factorial(5))
print(recursive_factorial(10000))
def simple_times_tables(number):
for x in range(1, number + 1):
for y in range(1, number + 1):
print(str(x) + " times " + str(y) + " = " + str(x * y))
simple_times_tables(4)
def complicated_times_tables(number):
calculated = set()
for x in range(1, number + 1):
for y in range(1, number + 1):
multiplication = tuple(sorted((x, y)))
if multiplication not in calculated:
print(str(x) + " times " + str(y) + " = " + str(x * y))
calculated.add(multiplication)
complicated_times_tables(4)
post-class footnote about recursion and self-reference.¶
A recursive function, as I said in class, is just a function that calls itself. This seems paradoxical because of how we usually think about variable assignment: we can't make use of a variable until we define it. For reasons that we don't need to get into, however, that doesn't really apply to functions, so functions can call themselves in their own definitions... just not too many times, or else it runs out of memory ("blows the stack," in programmer-ese---and the website Stack Overflow is named after this phenomenon).
As I've been saying, programming and lawyering are very similar, and in law as well we've had lots of opportunity to think about the opportunities and problems posed by self-reference. Consider Article V of the Constitution:
The Congress, whenever two thirds of both Houses shall deem it necessary, shall propose Amendments to this Constitution, or, on the Application of the Legislatures of two thirds of the several States, shall call a Convention for proposing Amendments, which, in either Case, shall be valid to all Intents and Purposes, as Part of this Constitution, when ratified by the Legislatures of three fourths of the several States, or by Conventions in three fourths thereof, as the one or the other Mode of Ratification may be proposed by the Congress; Provided that no Amendment which may be made prior to the Year One thousand eight hundred and eight shall in any Manner affect the first and fourth Clauses in the Ninth Section of the first Article; and that no State, without its Consent, shall be deprived of its equal Suffrage in the Senate.
One question you might ask is: Does Article V apply to itself? Intuitively, it seems like it does---after all, presumably there isn't any reasonable method to amend the process for amending the Constitution except by using the process of the Article that says how you amend the Constitution.
Another question you might ask, to bring out the legal problems is this. Suppose the last two clauses of Article V instead read:
Provided that no Amendment which may be made prior to the Year One thousand eight hundred and eight shall in any Manner affect the first and fourth Clauses in the Ninth Section of the first Article; that no State, without its Consent, shall be deprived of its equal Suffrage in the Senate; and that no Amendment may ever be made to the fifth Article.
Does that rule work?
Change to problem set due dates¶
As I said in class, I'm pushing back the problem set due dates so that we can proceed a little bit more slowly through some of the earlier material.
Problem set 1 was originally due February 7; it is now due on February 14. (Happy Valentine's day?)
Problem set 2 was originally due February 28; it is now due March 6.
The other two problem sets will retain their original due dates.
These changes have been reflected on the syllabus on the course website, but I haven't yet updated anything else (like ICON), and might not get around to doing so any time soon.