Author Archives: Luis Vargas

#QUIZ09

#WSQ13

Babylonian Method

The function should receive a number and return floating point number. Obviously you should test your function, so create a main program that asks the user a value, calculates the square root and displays that.

def sqrt(n):
    x=n/2
    y=x+1
    while(x!=y):
        z=n/x
        y=x
        x=(x+z)/2
    return x

ans=”y”
while (ans==”y”):
    n = float(input(“Give me a number: “))
    if (n==0):
        print(“The square root of 0 is 0”)
    elif (n<0):
        print(“Error no square root for negative numbers.”)
    else:
        sqrta=sqrt(n)
        print (“The square root of”,n,”is”,sqrta)
    ans = str(input(“Do you want to try again? y/n: “))
if (ans==”n”):
    print (“Have a nice day.”)
else:
    print (“Options were y or n but whatever have a nice day.”)

 

#WSQ12

Greatest Common Divisor

The function should receive two integers and return an integer.Obviously you should test your function, so create a main program that asks the user for two values, calculates the gcd and displays that.

def gcd(x,y):
    if (x==y):
        ans=x
    else:
        if(x>y):
            ans=gcd((x-y),y)
        else:
            ans=gcd(x,(y-x))
    return ans

x=int(input(“Give me the first number: “))
y=int(input(“Give me the second number: “))
agcd= gcd(x,y)
print(“the Greatest Common Divisor of “,x,” and “,y,” is: “,agcd)

#WSQ11

Yo soy 196

Your jobs is to create a program that asks the user for two pieces of data:

  • The lower bound of the sequence
  • The upper bound of the sequence
Then you check the values from the lower bound (inclusive) to the upper bound (inclusive) and make a report of them. During the analysis of each number, if a Lychrel number is found it should be reported immediately with something like “Found a Lychrel number: 196″
 
def palindromes(x):
    x1 = str(x)
    x2 = x1[::-1]
    x3 = int(x2)
    if(x==x3):
        return True
    else:
        return False
nonlycherels = 0
Lycherels = 0
npalindromes = 0
x = int(input(“Give me the lower bound: “))
y = int(input(“Give me the upper bound: “))
print(“Range of numbers analysed: From”,x, “to”,y)
for c in range(x,y+1):
    c1 = palindromes(c)
    if(c1==False):
        c2 = 0
        p1 = False
        candidate = c
        while(c2<30 and p1==False):
            c2 = c2 + 1
            c3 = str(candidate)
            r = c3[::-1]
            r1 = int(r)
            candidate = candidate + r1
            p1 = palindromes(candidate)
        if(p1==True):
            nonlycherels = nonlycherels + 1
        if(c2==30 and p1==False):
            Lycherels = Lycherels + 1
    else:
        npalindromes = npalindromes + 1
print(“The number of natural palindromes is: “, npalindromes)
print(“The number of non-Lycherels is: “, nonlycherels)
print(“The number of Lycherel  is: “, Lycherels)
 

#Mastery18

#Mastery17

#Mastery16

#Mastery15

#Mastery13

#Mastery12