Centro del quadrato

Vedi la discussione

Il numero di cifre è pari: 2 | 4 | 6 | …

Osserva

  • x1, quadrato di x3
  • x2, taglio delle cifre a destra
  • x3, taglio delle cifre a sinistra

CIFRE=int(input("Quante cifre?  "))
SEME =int(input("Seme?          "))
NUM  =int(input("Quanti numeri? "))
print()
 
Q=10**(CIFRE//2)
R=10**CIFRE
x3=SEME
for i in range(NUM):
    x1=x3**2
    x2=x1//Q
    x3=x2%R
    print("%2i %8i %6i %4i"
          %(i, x1, x2, x3))
Quante cifre?  4
Seme?          1234
Quanti numeri? 10

 0  1522756  15227 5227
 1 27321529 273215 3215
 2 10336225 103362 3362
 3 11303044 113030 3030
 4  9180900  91809 1809
 5  3272481  32724 2724
 6  7420176  74201 4201
 7 17648401 176484 6484
 8 42042256 420422  422
 9   178084   1780 1780

L’elaborazione di x può essere riassunta in un’unica espressione

CIFRE=int(input("Quante cifre?  "))
SEME =int(input("Seme?          "))
NUM  =int(input("Quanti numeri? "))
print()

Q=10**(CIFRE//2)
R=10**CIFRE
x=SEME
for i in range(NUM):
    x=x**2//Q%R
    print(x, end=" ")
Quante cifre?  4
Seme?          1234
Quanti numeri? 10

5227 3215 3362 3030 1809 2724 4201 6484 422 1780