rad_quadrata.py

Created by gianfranco-oddenino

Created on March 07, 2025

894 Bytes


# programma per determinare la radice quadrata di un numero

n = float(input("Dammi il numero di cui vuoi determinare la radice quadrata? "))
a = float(input("Inserisci l'estremo inferiore dell'intervallo di ricerca: "))
b = float(input("Inserisci l'estremo superiore dell'intervallo di ricerca: "))

# il ciclo continua mentre l'ampiezza dell'intervallo [a,b] è maggiore di 1E-10
while b-a>1E-10:
  # calcolo il valore medio di a e b
  m = (a+b)/2
  
  print("a = %.10f   m = %.10f   b = %.10f" % (a, m, b))

  # controllo se m^2 è minore o maggiore del numero inserito
  if m**2<n:
    # se m^2 è minore di n, significa che a è troppo piccolo
    # quindi sposto l'estremo inferiore dell'intervallo fino ad m 
    a = m
  else:
    # se m^2 è maggiore di n, significa che b è troppo grande
    # quindi sposto l'estremo superiore dell'intervallo fino ad m 
    b = m

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.