1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Olegator [25]
2 years ago
7

Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the in

put number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key.
Computers and Technology
1 answer:
notka56 [123]2 years ago
8 0

Answer:

import math

#Initialize tolerance

tolerance = 0.000001

def newton(x):

   """ Returns the square root of x """

   #Performs the successive approximations

   estimate = 1.0

   while True:

       estimate = (estimate + x / estimate) / 2

       difference = abs(x - estimate ** 2)

       if difference <= tolerance:     # Break out of loop if difference is less than tolerance

           break            

       return estimate     # While the difference value is > TOLERANCE, the process continues

def main():

   """Allows the user to obtain square roots."""

   while True:

       #Receive the input number from the user

       x = input("Enter a positive number or enter/return to quit: ")

       if x == "":     #if user presses "Enter" then exit the program

           break       # Otherwise, continue the process of allowing new numbers

       x = float(x)

       #Output the result

       print("The programs estimate of the square root of ", x, "is ", round(newton(x),2))

       print("Python's estimate: ", math.sqrt(x))

main()

You might be interested in
Give a pseudo-code description of the O(n)-time algorithm for computing the power function p(x,n).Also draw the trace of computi
laila [671]

Answer:

p(x,n)

1. if(n==0)    [if power is 0]

2.    then result =1.

3.else

4. {    result=1.

5.      for i=1 to n.

6.          {  result = result * x. }  [each time we multiply x once]

7.       return result.

8.  }

Let's count p(3,3)

3\neq0, so come to else part.

i=1: result = result *3 = 3

i=2: result = result *3 = 9

i=2: result = result *3 = 27

Explanation:

here the for loop at step 4 takes O(n) time and other steps take constant time. So overall time complexity = O(n)

6 0
3 years ago
You want to securely erase data from your hard drive what can you use to do this and what is the process called
tankabanditka [31]
You could Factory Reset a computer or you could smash it with  a hammer
7 0
2 years ago
Read 2 more answers
If classes C1 and C2 both implement an interface Cint, which has a method whichIsIt, and if C1 c = new C1( ); is performed at on
Assoli18 [71]

Answer:

FALSE

Explanation:

Because C1 and C2 implement the same interface, they both implement whichIsIt. The variable c is known as a polymorphic variable, meaning that it can change from being an C1 to a C2. So, the message c.whichIsIt( ); may invoke C1's whichIsIt or C2's whichIsIt. This can only be known at runtime.

5 0
3 years ago
1. Scrieţi un program care citeşte un număr natural n şi determină produsul cifrelor impare ale lui n. De exemplu, pentru n = 23
saveliy_v [14]

Answer:

1.  

num1 = input("Enter the value of n")  

n = int(num1)  

def calc(n):  

Lst = []  

while n > 0:  

remainder = n % 10  

Lst.append(remainder)  

quotient = int(n / 10)  

n = quotient  

i = len(Lst) - 1  

sum = 1  

for i in range(0, len(Lst)):  

if(i % 2 != 0):  

sum *=Lst[i];  

else:  

continue  

print(sum)  

return(0)  

\ r2

num=input("Enter the value of n")

arr=[12,-12,13,15,-34,-35,35,42]

def div7positive():

  sum = 0

  m = 0

  i = 0

  while m <= 7:

      if(arr[i]>=0 and arr[i]%7 == 0):

          sum +=arr[i]

      i = i + 1

      m += 1

       

   

  print("sum of number divisible by 7 and positive is", +sum);

div7positive()

\ r

\ r3.Write an algorithm that reads a natural number n and calculates the sum:

\ rS = 1 / (1 * 2) + 1 / (2 * 3) + 1 / (3 * 4) +… + 1 / ((n-1) * n)

\ r

num=input("Enter the value of n")

def sumseries():

  sum = 0

  m= 0

  while m <= 7:

      sum +=1/((int(num)-1)*int(num))

      m += 1

  print("sum of series is", +sum)

sumseries()

\ R4. Read a natural number n. Calculate the sum of its own divisors n. For example, for n = 12, the sum of its own divisors is 2 + 3 + 4 + 6 = 15

num=input("Enter the value of n")

def sumdivisors():

  sum = 0

  m= 2

  while m <= int(num):

      if int(num) % m == 0:

          sum += m

      m += 1

  print("sum of divisors is", +sum)

sumdivisors()\ r

\ R5. We read a natural number n and then whole numbers. Calculate and display the sum of the natural numbers between 10 and 100. For example, if n = 5 and then read 30, –2, 14, 200, 122, then the sum will be 44 (that is, 30 + 14).

\ r

Lst = []  

num=input("Enter the value of n")

i = 0

while i<= int(num):

  num1=input("Enter the element of array")

  Lst.append(int(num1));

  i += 1

def numbet0and100():

  sum = 0

  m= 0

  while m <= int(num):

      if Lst[m] <= 100 or Lst[m] >=0:

          sum += Lst[m]

      m += 1

  print("sum of numbers between 0 and 100 is", +sum)

numbet0and100()

\ R6. A natural number n of maximum 4 digits is read. How many digits are in all numbers from 1 to n? For example, for n = 14 there are 19 digits, and for n = 9 there are 9 digits.

\ r

num = input("Enter the value of n")

n = int(num)

print(“sum as required is:” (n -9) + n)  

 

\ R7. Read the natural numbers n and S, where n can be 2, 3, 4 or 5. Show all the numbers of n digits that have the numbers in strictly ascending order, and the sum of the digits is S. For example, for n = 2 and S = 10, 19, 28, 37, 46 will be displayed.

num = input("Enter the value of n")

n = int(num)

Lst = []

def calc(n):

  i = 1

  total = pow(10,n)

  while i <= total:

      j = i

      while j > 0:

          remainder = j % 10  

          Lst.append(remainder)

          quotient = int(j / 10)

          if quotient > 0:

              j = quotient

          else:

              length = len(Lst) - 1

              sum = 0

              while length >= 0:

                  sum += Lst[length]

                  length = length - 1

              if sum == pow(10,n):

                  print(j)

              k = len(Lst)

              del Lst[0:k]

      i = i + 1

  return(0)

calc(n)

\ r

\ R8. We consider the row 1, 1, 2, 3, 5, 8, 13, ... in which the first two terms are 1, and any other term is obtained from the sum of the preceding two. Write an algorithm that reads a natural number n and displays the first n terms of this string. For example, for n = 6, 1, 1, 2, 3, 5, 8 will be displayed.

\ r

nterm = int(input("What number of terms do you want?"))

a, b = 0, 1

totalcount = 0

if nterm <= 0:

 print("Please input a positive number")

elif nterm == 1:

 print("Fibonacci number upto which",nterm,":")

 print(a)

else:

 print("Fibonacci series:")

 print(nterm)

 while totalcount < nterm:

     print(a)

     nth = a + b

     a = b

     b = nth

     totalcount += 1

 

\ 9. Write an algorithm that reads two natural numbers n1 and n2 and displays the message "yes" if the sum of the squares of the digits of n1 is equal to the sum of the numbers of n2 or "no" otherwise. For example, for n1 = 232 and n2 = 881, "yes" will be displayed, and for n1 = 45 and n2 = 12, "no" will be displayed.

num1 = input("Enter the value of n")

num3 = input("Enter the value of n")

n = int(num1)

num2 = int(num3)

def calc(n):

  Lst = []

  while n > 0:

      remainder = n % 10  

      Lst.append(remainder)

      quotient = int(n / 10)

      n = quotient

       

  i = len(Lst) - 1

  sum = 0

  while i >= 0:

      sum += Lst[i]* Lst[i]

      i = i - 1

  return(sum)

def calc1(num2):

  Lst = []

  while num2 > 0:

      remainder = num2 % 10  

      Lst.append(remainder)

      quotient = int(num2 / 10)

      num2 = quotient

       

  i = len(Lst) - 1

  sum = 0

  while i >= 0:

      sum = Lst[i] + Lst[i]

      i = i - 1

  return(sum)

a = calc(n)

b = calc1(num2)

if a == b:

  print("yes")

else:

  print("No")

\ r

\ R10. Write an algorithm that reads a natural number n and displays the message "yes" if all of its n numbers are distinct, or "no" if n does not have all the distinct digits. For example, for n = 37645 it will display "yes" and for 23414 it will show "no".

\ r

num1 = input("Enter the value of n")

n = int(num1)

def calc(n):

  Lst = []

  while n > 0:

      remainder = n % 10  

      Lst.append(remainder)

      print( remainder)

      quotient = int(n / 10)

      n = quotient

       

  flag = 1

  for i in range(0, len(Lst)):

      for j in range(i+1, len(Lst)):    

          if Lst[i] == Lst[j]:    

              flag = 0

              break

           

  if flag == 1:

      print("YES")

  else:

      print("NO")

  return(0)

Explanation:

Please check answer.  

3 0
2 years ago
What does the following if statement do?<br><br> if (num1 == Math.abs(num1))
Gennadij [26K]

This statement checks if num1 is equal to the absolute value of num1

For instance,

num1 = 4 and the absolute value of num1 = 4. This would run the code inside the if statement but if num1 = -1 the absolute value of num1 = 1 and the if stamtent would be skipped because -1 does not equal 1

6 0
2 years ago
Other questions:
  • If you use the ___ template, as opposed to a formatted theme, you must make all design decisions?
    15·1 answer
  • Which of the following best reflects why lighting systems are used when filming on location outdoors? (Select all that apply.)
    10·1 answer
  • This procedure protects against the loss of data
    5·1 answer
  • A typeface in which each character has the same width and is often used to display programming code is _
    8·1 answer
  • Which of the following is a malicious program that can replicate and spread from computer to computer?
    13·2 answers
  • And there you go <br> sorry its saying my thing is tooo small
    14·2 answers
  • If I wanted to include a picture of a dog in my document, I could use _____. SmartArt WordArt clip art AutoCorrect
    13·1 answer
  • 13. By adding built-in writable memory to its cartridge, Nintendo’s _________________ was the first console game that gamers cou
    9·1 answer
  • Most significant effect the rotary press had on printing
    9·1 answer
  • Where would you click to see the list of microsoft word styles?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!