Advertisement
LightProgrammer000

Soma de hipotenusas [python 3]

Feb 20th, 2020
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. '''
  2. Programa: Soma de hipotenusas
  3. '''
  4.  
  5. # Principal
  6. def main():
  7.  
  8.     # Resultado
  9.     print(soma_hipotenusas(25))
  10.  
  11. # Metoo: Hipotenusa
  12. def e_hipotenusa(hip, i, j):
  13.  
  14.     # Calculos: Hipotenusa
  15.     verf_1 = int(hip ** 2 == i ** 2 + j ** 2)
  16.  
  17.     # Verificando existencia do triangulo
  18.     a = abs(i - j) < hip < i + j
  19.     b = abs(hip - j) < i < hip + j
  20.     c = abs(hip - i) < j < hip + i
  21.     verf_2 = a and b and c
  22.  
  23.     # Estrutura de decisao
  24.     if verf_1 and verf_2:
  25.         return hip
  26.  
  27.     else:
  28.         return 0
  29.  
  30. # Metodo: Somatorio
  31. def soma_hipotenusas(n):
  32.  
  33.     # n² = i² + j²
  34.     hip = 1
  35.     soma = 0
  36.     aux = 0  # Controle para bloquear numeros repetidos
  37.  
  38.     # Estruturas de repeticao
  39.     while hip <= n:
  40.  
  41.         # # Variavel reiniciada
  42.         i = 1
  43.  
  44.         # Estrutura de repeticao: Cateto <= Hipotenusa
  45.         while i <= hip:
  46.  
  47.             # Variavel reiniciada
  48.             j = 1
  49.  
  50.             # Estrutura de repeticao: Cateto menor <= Cateto maior
  51.             while j <= i:
  52.  
  53.                 # Chamada de metodo
  54.                 a = e_hipotenusa(hip, i, j)
  55.  
  56.                 # Estrutura de decisao: Controle de repeticao de numeros
  57.                 if a != 0 and a != aux:
  58.  
  59.                     # print(a)
  60.                     soma += a
  61.                     aux = a
  62.  
  63.                 # Incrementador do cateto menor
  64.                 j += 1
  65.  
  66.             # Incrementador do cateto maior
  67.             i += 1
  68.  
  69.         # Incrementador da hipotenusa
  70.         hip += 1
  71.  
  72.     return soma
  73.  
  74. # Execucao
  75. if __name__ == '__main__':
  76.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement