Last active
March 1, 2024 04:32
-
-
Save shibby360/58c1fdfefec5c3e1fd9c0734219fd58b to your computer and use it in GitHub Desktop.
Code to verify the sequence => difference => factorial
This file contains 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
def setdiff_fac_verify(start, stop, power): | |
lst = list(range(start, stop+1)) | |
if len(lst) < power + 2: | |
raise ArithmeticError('list too small(' + (power+2) + ' terms required, got ' + len(lst) + ')') | |
nlst = [] | |
for i in lst: | |
nlst.append(i**power) | |
def get_diffs(seqlst, diffd): | |
print(diffd, seqlst) | |
if diffd == power: | |
return seqlst | |
else: | |
dlst = [] | |
for i in range(1, len(seqlst)): | |
dlst.append(seqlst[i]-seqlst[i-1]) | |
diffd += 1 | |
return get_diffs(dlst, diffd) | |
def fac(x): | |
if x == 0: | |
return 1 | |
else: | |
return x * fac(x-1) | |
endlst = get_diffs(nlst, 0) | |
return all(i == endlst[0] for i in endlst) and endlst[0] == fac(power) | |
st = input('start: ') | |
sto = input('stop: ') | |
p = input('power: ') | |
result = setdiff_fac_verify(int(st), int(sto), int(p)) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment