Found 10802 Articles for Python

Check if two numbers are coprime using Python

AYUSH MISHRA
Updated on 27-Mar-2025 14:42:07

162 Views

In this article, we are going to learn how we can check whether two given numbers are co-prime in Python using different approaches. What are Co-prime Numbers? Two numbers are said to be co-prime numbers if they have no common factors other than 1. Checking if two numbers are co-prime We can check if two given numbers are co-prime numbers by finding their GCD (Greatest Common Factor). GCD is also known as HCF (Highest Common Factor). If the GCD of two numbers is 1, then those numbers are co-prime numbers; otherwise, they are not co-prime numbers. Below are the examples ... Read More

Python Program for finding nth term of H.P.

AYUSH MISHRA
Updated on 20-Mar-2025 13:57:40

377 Views

A Harmonic Progression ( H.P.) is a sequence of numbers where the reciprocals of the terms form an Arithmetic Progression (A.P.). In simple terms, if we take the reciprocal of each term in an H.P., the resulting sequence will be in A.P. In this problem, we are given the first term, common difference, and value of n of which we have to find the nth term of given H.P. Find the nth term of H.P. To find the nth term of Harmonic Progression, we first convert the H.P. into an A.P. by finding reciprocals of the terms. The formula ... Read More

Find the number that appears once, and the other numbers twice in Python

AYUSH MISHRA
Updated on 20-Mar-2025 13:57:21

70 Views

Finding a unique number in a list that appears only once while all other numbers appear twice is a common and important problem in programming. We can find the unique number in a list using multiple ways. The most efficient approach is using an XOR operator, as this method solves this problem in constant time and space complexity. In this article, we are going to discuss various approaches to finding the number that appears once while all others appear twice. Finding the Unique Number When we are given a list of numbers where every number appears twice except one ... Read More

Python Program for Sum of first N even numbers

AYUSH MISHRA
Updated on 17-Mar-2025 13:01:46

1K+ Views

The sum of the first N even numbers is a mathematical operation. In this operation, we add up the firstN even numbers. In Python, there are multiple ways to calculate this sum. In this article, we are going to learn and discuss various approaches to finding the sum of the first N even numbers in Python. Finding the Sum of the First N Even Numbers The formula for finding the sum of the first N even numbers is: Sum = N * (N + 1) Example 1Let's look at a scenario where N=5, The first 5 even numbers ... Read More

Python Program to Calculate nCr and nPr

AYUSH MISHRA
Updated on 05-Feb-2025 13:03:14

1K+ Views

In combinatorics, nCr and nPr are essential formulas used to calculate combinations and permutations. They represent the number of ways to select and arrange objects from a set. nCr refers to combinations, where order does not matter, while nPr refers to permutations, where order does matter. The permutation refers to the arrangement of objects in a specific order.A combination refers to the selection of objects without taking order in reference. In this article, we are going to learn how we can calculate nCr and nPr in Python using different approaches. The formula for nCr and nPr The formulas ... Read More

Python Program to find area of square

AYUSH MISHRA
Updated on 25-Feb-2025 15:09:02

1K+ Views

A square is a closed two-dimensional figure having 4 equal sides. Each angle of a square is 90 degrees. The area of a square is the space enclosed within its four sides. In this problem, we are given the side of a square, and we have to find the area of the square. In this tutorial, we are going to find the area of a given square in Python using different approaches. Example 1 Input: side = 6 units Output: 36 square units Using the formula to calculate the area of the square: side × side = 6 × ... Read More

Python Program for sum of the series: 1 + 1/2 + 1/3 + ... + 1/n

AYUSH MISHRA
Updated on 04-Feb-2025 16:14:23

562 Views

Mathematical series is one of the problems that helps us to improve problem-solving and logical thinking skills One of the engaging problems is calculating the sum of the harmonic series. The series 1 + 1/2 + 1/3 + ⋯⋯ + 1/n is known as the harmonic series. In this article, we are going to learn how to find the sum of the series: 1 + 1/2 + 1/3 + ⋯⋯ + 1/n using Python. What is the Harmonic Series? The harmonic series is a mathematical sequence where each term is the reciprocal of a positive integer. It is represented as, ... Read More

Python Program to Toggle Each Character in a String

AYUSH MISHRA
Updated on 04-Feb-2025 16:17:21

2K+ Views

In this problem, we are given a string, and the task is to toggle the case of each character in the string. This means that all lowercase letters should be converted to uppercase, and all uppercase letters should be converted to lowercase. In this article, we are going to explore different approaches to toggling each character in a string using Python. Let's look at the simple example, where we are going to provide input as "TutorialsPoint". Then output is like "tUTORIALSpOINT" In this case each character is toggled. Such that Uppercase letters become lowercase and lowercase becomes uppercase. Below are ... Read More

Python Program to Implement Ternary Search

AYUSH MISHRA
Updated on 04-Feb-2025 02:33:27

2K+ Views

Searching is one of the most fundamental operations in computer science, and there are multiple algorithms to perform it efficiently. While Binary Search is mainly known to be applied to sorted array searching, Ternary Search is another efficient searching technique that also works on sorted arrays for searching. What is a Ternary Search Ternary Search is a divide-and-conquer algorithm that splits the array into three parts instead of two (as in Binary Search). It repeatedly reduces the search space by two-thirds in each step, making it useful in cases where the dataset is large, and constant-time comparisons are expensive. ... Read More

Python Program to Find Average of Array Elements

AYUSH MISHRA
Updated on 04-Feb-2025 16:17:53

312 Views

In this article, we will learn how to find the average of array elements using Python, along with examples to demonstrate the implementation. In this problem, we are given an array of integers, which may contain both positive and negative numbers. Our task is to calculate the average of all elements in the array. Using the Brute Force Approach This is a simple and direct approach. This approach involves manually iterating through each element in the array to calculate the sum. Then, we divide the total sum by the count of elements to get the average. This approach is ... Read More

Advertisements
close