Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created September 17, 2024 02:43
Show Gist options
  • Save Silva97/b97866566ea61dec3b6c3c3e1f2b2872 to your computer and use it in GitHub Desktop.
Save Silva97/b97866566ea61dec3b6c3c3e1f2b2872 to your computer and use it in GitHub Desktop.
|a[n] - a[m]| / |n - m| = r
#!/usr/bin/env python3
import sys
def main():
"""
Demonstração de que dado uma P.A. com razão constante, existe uma proporção
entre a diferença entre os índices de 2 termos quaisquer e a diferença de
seus valores.
Ou seja, esse script demonstra que:
|a[n] - a[m]| / |n - m| = r
Exemplo:
P.A.: 3, 6, 9, 12, 15, 17 ...
r = 3
|a3 - a5| / |3 - 5| = 3
"""
if len(sys.argv) < 3:
print("Usage: ./pa-demo.py <a1> <r> [nvalues]")
exit(0)
a1 = int(sys.argv[1])
r = int(sys.argv[2])
nvalues = 10 if len(sys.argv) <= 3 else int(sys.argv[3])
first_values = [a1 + i*r for i in range(nvalues)]
print(f"First {nvalues} values:", first_values)
for ix, x in enumerate(first_values):
for iy, y in enumerate(first_values):
if ix == iy:
continue
calculated_r = abs(x - y) / abs(ix - iy)
print(
f"|{x} - {y}| / |{ix + 1} - {iy + 1}| =",
calculated_r,
)
if calculated_r != r:
print(f"ERROR! {calculated_r} is not equal to {r}")
exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment