Hi there, I'm currently working as an Analysts of a System of a bank, so I don't program there but I know a little bit (barely to get in).
I started working there with the hope to work as a developer but my hopes died out when they put me in charge of a whole dead project (which I revived btw). I been there for one year and started again the collage being 27 years old (yeah it sucks).
#rantover#
So...
I don't program a lot, not at all you can say.
It took me literally 3 hours to get this exercise working in a rather awful way.
You have any ideas or pointer that you can give me to make it work smoother and don't repeat too much code?
RESTRICTIONS:
ONLY using: if, elif, else, and, or, not, while, break, continue, for, in, range(), lambda, modules and packages.
No lists or anything similar.
Exercise:
For an integer N less than 100 received as a parameter, write a program
that uses a function to return the sum of the squares of those
numbers between 1 and N that are separated from each other by four units. (12 + 52 +
92 + 132 + ...)
PS: SORRY! The code is in spanish but you can understand the meaning no?
I started working there with the hope to work as a developer but my hopes died out when they put me in charge of a whole dead project (which I revived btw). I been there for one year and started again the collage being 27 years old (yeah it sucks).
#rantover#
So...
I don't program a lot, not at all you can say.
It took me literally 3 hours to get this exercise working in a rather awful way.
You have any ideas or pointer that you can give me to make it work smoother and don't repeat too much code?
RESTRICTIONS:
ONLY using: if, elif, else, and, or, not, while, break, continue, for, in, range(), lambda, modules and packages.
No lists or anything similar.
Exercise:
For an integer N less than 100 received as a parameter, write a program
that uses a function to return the sum of the squares of those
numbers between 1 and N that are separated from each other by four units. (12 + 52 +
92 + 132 + ...)
PS: SORRY! The code is in spanish but you can understand the meaning no?
# Práctica 1, Ejercicio 3. Autor: KoFu
def calcularcuadrado(n):
""" Devuelve la suma de los cuadrados del numero ingresado (entre 1 y 100) de aquellos
números entre 1 y N que están separados entre si por cuatro unidades"""
n1,n2,n3,n4,n5=0,0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
acumulador=0
if (1 <= n <= 100):
while (n > 0):
cuadrado = n**2
if (n1 == 0):
n1 = n
c1 = cuadrado
elif (n2 == 0):
n2 = n
c2 = cuadrado
elif (n3 == 0):
n3 = n
c3 = cuadrado
elif (n4 == 0):
n4 = n
c4 = cuadrado
elif (n5 == 0):
n5 = n
c5 = cuadrado
if (n5 - n1 == -4):
sumaparcial = c1+c5
acumulador = sumaparcial + acumulador
n1,n2,n3,n4=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n5 - n == 4):
sumaparcial = cuadrado
acumulador = sumaparcial + acumulador
n1,n2,n3,n4=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n4 - n1 == -4):
sumaparcial = c1+c4
acumulador = sumaparcial + acumulador
n1,n2,n3,n5=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n4 - n == 4):
sumaparcial = cuadrado
acumulador = sumaparcial + acumulador
n1,n2,n3,n5=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n3 - n1 == -4):
sumaparcial = c1+c4
acumulador = sumaparcial + acumulador
n1,n2,n5,n4=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n3 - n == 4):
sumaparcial = cuadrado
acumulador = sumaparcial + acumulador
n1,n2,n5,n4=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n2 - n1 == -4):
sumaparcial = c1+c4
acumulador = sumaparcial + acumulador
n1,n4,n3,n4=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
elif (n2 - n == 4):
sumaparcial = cuadrado
acumulador = sumaparcial + acumulador
n1,n4,n3,n5=0,0,0,0
c1,c2,c3,c4,c5=0,0,0,0,0
sumatotal = acumulador
n = n - 1
else:
sumatotal = -1
return sumatotal;
# Programa principal
num=int(input("Ingrese un numero (entre 1 y 100) para calcular la suma de los cuadradros separados entre cuatro digitos: "))
suma = calcularcuadrado (num)
if (suma==-1):
print("El numero ingresado no esta en el rango permitido")
else:
print("La suma de los cuadrados es de: ", suma)
