Found 10805 Articles for Python

Python Program for Find largest prime factor of a number

Pavitra
Updated on 26-Sep-2019 07:16:48

3K+ Views

In this article, we will learn about the solution to the problem statement given below −Problem statementGiven a positive integer n. We need to find the largest prime factor of a number.ApproachFactorise the given number input by dividing it with the divisor of a number.Now keep updating the maximum prime factor.Example Live Demoimport math def maxPrimeFactor(n):    # number must be even    while n % 2 == 0:       max_Prime = 2       n /= 1    # number must be odd    for i in range(3, int(math.sqrt(n)) + 1, 2):       while n ... Read More

Python Program for Fibonacci numbers

Pavitra
Updated on 25-Sep-2019 14:18:17

385 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Our task to compute the nth Fibonacci number.The sequence Fn of Fibonacci numbers is given by the recurrence relation given belowFn = Fn-1 + Fn-2with seed values (standard)F0 = 0 and F1 = 1.We have two possible solutions to the problemRecursive approachDynamic approachApproach 1 −Recursive ApproachExample Live Demo#recursive approach def Fibonacci(n):    if n

Python Program for Difference between sums of odd and even digits

Pavitra
Updated on 25-Sep-2019 13:20:27

391 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.To reduce the computational time we use the concept of mental mathematics .The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the ... Read More

Python Program for cube sum of first n natural numbers

Pavitra
Updated on 25-Sep-2019 13:16:16

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an input n, we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.Here we will discuss two approach to reach the solution of the problem statement −Brute-force approach using loops.Mathematical solution of sum of n numbers.Approach 1 −Computing sum of each term by adding by iterating over the numbersExample Live Demodef sumOfSeries(n):    sum = 0    for i in range(1, n+1):       sum +=i*i*i    return sum # ... Read More

Python Program for compound interest

Pavitra
Updated on 25-Sep-2019 13:13:09

393 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −We are given with three input values i.e principle, rate & time and we need to compute compound interest.The code given below shows the process of computation of compound interest.The formula used here isCompound Interest = P(1 + R/100)rWhere, P is the principal amountR is the rate andT is the time spanThe implementation is given belowExample Live Demodef compound_interest(principle, rate, time):    CI = principle * (pow((1 + rate / 100), time))    print("Compound interest : ", CI) # main compound_interest(10000, 7.78, 2)OutputCompound ... Read More

Python Program for Bubble Sort

Pavitra
Updated on 25-Sep-2019 12:59:03

1K+ Views

In this article, we will learn about the implementation of bubble sort sorting technique.The figure shown below illustrates the working of this algorithm −ApproachStarting with the first element(index = 0), compare the current element with the next element of the array.If the current element is greater than the next element of the array, swap them.If the current element is less than the next element, move to the next element.Repeat Step 1.Now let’s see the implementation below −Exampledef bubbleSort(ar):    n = len(arr)    # Traverse through all array elements    for i in range(n):    # Last i elements are ... Read More

Python Program for Binary Search

Pavitra
Updated on 25-Sep-2019 12:50:04

6K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement − We will be given a sorted list and we need to find an element with the help of a binary search.AlgorithmCompare x with the middle element.If x matches with the middle element, we return the mid index.Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for the right half.Else (x is smaller) recur for the left halfRecursive AlgorithmExampledef binarySearchAppr (arr, start, end, x): # check ... Read More

Python Program to Print Matrix in Z form

Pavitra
Updated on 25-Sep-2019 12:26:13

274 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given a square matrix of order n*n, we need to display elements of the matrix in Z form.Z form is traversing the matrix in the following steps −Traverse the first rowNow, traverse the second principal diagonalFinally, traverse the last row.We will take an input matrix here implicitly taken to demonstrate the flow of code.demostrateExample Live Demoarr = [[1, 2, 6, 9],    [1, 2, 3, 1],    [7, 1, 3, 5],    [1, 8, 7, 5]] n = len(arr[0]) i = 0 ... Read More

Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Pavitra
Updated on 25-Sep-2019 12:21:44

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer input n, we need to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Here we are implementing for loop, therefore, we get O(n) as the time complexity.Here to reach the efficiency we calculate factorial within the same loop.Here we frame a sumofseries function as described below −Example Live Demodef sumOfSeries(num):    res = 0    fact = 1    for i in range(1, num+1):       fact *= i       res ... Read More

Python Program to find the area of a circle

Pavitra
Updated on 25-Sep-2019 12:18:21

440 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the radius of a circle, we need to find a circle.The area of a circle can simply be evaluated using the following formula.Area = Pi*r*rLet’s see the implementation below −Example Live Demodef findArea(r):    PI = 3.142    return PI * (r*r); # Driver method print("Area is %.6f" % findArea(5));OutputArea is 78.550000All variables and functions are declared in the global scope as shown in the figure below.ConclusionIn this article, we learned about the approach to find whether it is possible to make ... Read More

Advertisements