Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program to Check If Two Numbers are Amicable Numbers

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

Amicable numbers are two different numbers where the sum of proper divisors of each number equals the other number. For example, 220 and 284 are amicable because the proper divisors of 220 sum to 284, and the proper divisors of 284 sum to 220. What are Proper Divisors? Proper divisors are all positive divisors of a number except the number itself. For example, proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. Method 1: Using Mathematical Approach This method finds divisors efficiently by checking only up to the square ...

Read More

Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When you need to compute a polynomial equation where the coefficients are stored in a list, you can use nested loops to calculate each term. A polynomial like 2x³ + 5x² + 3x + 0 can be evaluated by multiplying each coefficient by the corresponding power of x. Understanding Polynomial Evaluation For a polynomial represented as a list [2, 5, 3, 0], this corresponds to: 2x³ (coefficient 2, power 3) 5x² (coefficient 5, power 2) 3x¹ (coefficient 3, power 1) 0x⁰ (coefficient 0, power 0) Example Here's how to evaluate the polynomial when ...

Read More

Python Program to Check if a Number is a Strong Number

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 7K+ Views

A strong number is a number whose sum of all digits' factorial equals the original number. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145. We can check this using the modulus operator and loops to extract digits and calculate factorials. Example Let's check if 145 is a strong number ? def factorial(n): if n == 0 or n == 1: return 1 fact = 1 ...

Read More

Python Program to Check if a Number is a Perfect Number

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 3K+ Views

A Perfect Number is a positive integer that equals the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6. The first few perfect numbers are 6, 28, 496, and 8128. Basic Method to Check Perfect Number We can iterate through all numbers from 1 to n-1 and check if they divide n evenly ? n = 6 my_sum = 0 for i in range(1, n): if n % i == 0: ...

Read More

Python Program to Convert Binary to Gray Code

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When it is required to convert binary code to gray code, a method is defined that performs the XOR operation. Gray code is a binary numeral system where two successive values differ in only one bit, making it useful in digital circuits and error correction. Below is the demonstration of the same − Understanding Gray Code Conversion The conversion from binary to Gray code follows a simple rule: The most significant bit of Gray code is the same as the binary code For other bits, the Gray code bit is the XOR of the current ...

Read More

Python Program to Generate Gray Codes using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 579 Views

Gray codes are binary codes where consecutive numbers differ by exactly one bit. Python can generate Gray codes using recursion by building sequences iteratively. This approach uses bit manipulation and string operations to create the complete Gray code sequence. What are Gray Codes? Gray codes (also called reflected binary codes) are sequences where adjacent values differ by only one bit position. For example, in 2-bit Gray code: 00 → 01 → 11 → 10, each step changes exactly one bit. Gray Code Generation Algorithm The algorithm starts with the base case (0, 1) and recursively builds ...

Read More

Python Program to Clear the Rightmost Set Bit of a Number

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 538 Views

When it is required to clear the rightmost set bit of a number, the bitwise AND (&) operator can be used with a clever mathematical property. This technique involves performing n & (n-1) which effectively removes the rightmost 1 bit from the binary representation. How It Works The operation n & (n-1) works because when you subtract 1 from a number, all bits to the right of the rightmost set bit get flipped, including the rightmost set bit itself. The AND operation then clears that bit. Binary Visualization Let's see how this works with number 6 ...

Read More

Python Program to Search the Number of Times a Particular Number Occurs in a List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 538 Views

When it is required to search the frequency of a number in a list, Python provides multiple approaches. You can define a custom method, use the built-in count() method, or utilize dictionary-based counting with collections.Counter. Method 1: Using Custom Function Define a function that iterates through the list and counts occurrences ? def count_num(my_list, x_val): my_counter = 0 for elem in my_list: if (elem == x_val): my_counter = ...

Read More

Python Program to Determine all Pythagorean Triplets in the Range

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

A Pythagorean triplet is a set of three positive integers a, b, c, such that a² + b² = c². The most famous example is 3, 4, 5 because 3² + 4² = 5² (9 + 16 = 25). This program finds all Pythagorean triplets where the hypotenuse c is within a given range. Understanding Pythagorean Triplets Pythagorean triplets can be generated using Euclid's formula: a = m² - n² b = 2mn c = m² + n² Where m > n > 0, and m and n are coprime integers. Example ...

Read More

Python Program to Print Numbers in a Range (1,upper) Without Using any Loops

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 664 Views

When it is required to print numbers in a given range without using any loop, we can use recursion. This technique involves defining a function that calls itself with a decremented value until it reaches the base case. Below is the demonstration of the same − Example def print_nums(upper_num): if(upper_num > 0): print_nums(upper_num - 1) print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim) Output ...

Read More
Showing 4891–4900 of 61,299 articles
« Prev 1 488 489 490 491 492 6130 Next »
Advertisements