Skip to content

Instantly share code, notes, and snippets.

@ademar111190
Created March 7, 2015 21:34
Show Gist options
  • Save ademar111190/dc22ac5d29cde40052b8 to your computer and use it in GitHub Desktop.
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
#!/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