Professional Documents
Culture Documents
N Int (Input ("Enter Number of Terms: ") ) A, B 0, 1 I 1 While I N: Print (A, End ',') A, B B, A+b I+ 1
N Int (Input ("Enter Number of Terms: ") ) A, B 0, 1 I 1 While I N: Print (A, End ',') A, B B, A+b I+ 1
(after the first two) is the sum of the previous two, i.e., the sequence is 0, 1,
1, 2, 3, 5, 8, . . ... Write a program that computes and outputs the Fibonacci
number upto n terms, where n is a value entered by the user.
def fib(n):
a, b = 0, 1
while a < n: # Run the program with b < n and see what it prints
print(a,end=',')
a, b = b, a+b
fib(100)
How to find L.C.M(Least Common Multiple, lcm of two numbers x and y is the smallest positive integer
divisible by both x and y) of two numbers using G.C.D(or H.CF.) of two numbers?
#lcm(x,y) = (x*y)/gcd(x,y)
x,y=eval(input("Enter two numbers : "))
x1,y1=x,y
while y:
x, y = y, x % y # Recall swap in Python
print("lcm = ",(x1*y1)//x)
How to find lcm(the smallest integer that divides both) without using gcd?
20
19-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University
GCD of more than 2 integers
Write a function to find GCD of three numbers (using GCD of two numbers)
Write a function to find GCD of more than two (or array) numbers
Write the above program without using math module and sqrt or pow function
22
20-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University
sin(x) infinite sum series
Calculate the value of sin(x) using the following Taylor series expansion of sin(x)
# This program generate and evaluate sin(x) function upto 0.000001% accuracy
import math
x=float(input(" Enter the value for x : ")) # where x is the angle in radians
x=(x*math.pi)/180 # convert degree into radian
# or x=math.radians(x) #Convert angle x from degrees to radians
term, sum, i=x, x, 2
while abs(term) >= 0.000001:
# until the absolute value of the nth term is less than the desired acceptable error
# Determine the number of terms in the above expansion so as to ensure that the difference between the
# computed value of sin(x) and standard value of sin(x) should not be more than 0.000001.
term *= (-x*x)/((2*i-2)*(2*i-1))
sum=sum+term
i+=1
print("The value of Sin(x) = ", sum)
print("The value of Sin(x) = ", math.sin(x))
Write a program for cos(x) infinite sum series 23