Created
March 7, 2015 21:34
-
-
Save ademar111190/dc22ac5d29cde40052b8 to your computer and use it in GitHub Desktop.
Verify if 0 + 1 + 2 + ... + n is equals to (n * (n + 1)) / 2 by exhaustion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from time import time | |
''' | |
Verify if 0 + 1 + 2 + ... + n is equals to (n * (n + 1)) / 2, this | |
method is not using mathematical induction, it is using exhaustion | |
''' | |
def proof(n): | |
for i in range(n + 1): | |
s = 0 | |
for j in range(i + 1): | |
s = s + j | |
if (s != ((i * (i + 1)) / 2)): | |
return False | |
return True | |
start = time() | |
print proof(10), "Spent {} milliseconds".format(time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment