Server Side Programming Articles - Page 2327 of 2650

C++ Program to Implement Fermat’s Little Theorem

Ravi Ranjan
Updated on 23-Apr-2025 17:07:46

993 Views

Fermat's Little Theorem states that if p is a prime number and a is an integer not divisible by p, then a^(p-1) is congruent to 1 modulo p. It can be represented as a(p-1) ≡ 1 (mod p). It can also be said that if any integer a is raised to the (p-1) where p is a prime and gcd(a, p) =1, then a^(p-1)-1 is divisible by p. In this article, we have two integers i.e. 'a' and a prime number 'p' such that a is not divisible by p. Our task is to implement Fermat's Little theorem using these ... Read More

C++ Program to Implement Extended Euclidean Algorithm

Ravi Ranjan
Updated on 07-May-2025 14:10:09

2K+ Views

The extended euclidean algorithms find the greatest common divisor (GCD) of two numbers in the form of ax + by = gcd(a, b). This expression is also known as Bezout's Identity. The extended Euclidean algorithm is an extension of the Euclid algorithm that is also used to find the GCD of two numbers using repetitive division. In this article, we have two numbers and our task is to implement the extended Euclidean algorithm to find the GCD of these two numbers in C++. Steps To Implement Extended Euclidean Algorithm The steps to implement an extended Euclidean algorithm are as ... Read More

C++ Program to Implement Euler Theorem

Ravi Ranjan
Updated on 23-Apr-2025 17:13:22

679 Views

Euler's theorem states that if two numbers (a and n), are co-prime i.e. gcd(a, n)=1, then 'a' raised to the power of Euler's totient function of n (a^φ(n)) is congruent to 1 modulo n i.e. a^φ(n) ≡ 1 (mod n). The Euler's Totient Function is denoted as φ(n) and represents the count of integers from 1 to n that are relatively co-prime to n. In this article, we have two co-prime integers. Our task is to implement the Euler Theorem in C++ using given co-prime numbers. Here is an example to verify the Euler Theorem for two co-prime numbers: ... Read More

C++ Program to Implement Russian Peasant Multiplication

Ravi Ranjan
Updated on 22-Apr-2025 16:50:55

579 Views

The Russian Peasant multiplication is used to multiply two numbers without using the multiplication operator. It involves constant use of halving and doubling the numbers. In this article, we have two integers, our task is to find the product of these two integers by implementing russian peasant multiplication in C++. Steps to Implement Russian Peasant Multiplication We will follow these simple steps to find the product using Russian Peasant multiplication: We have created a function russianPeasant() that accepts both the numbers (n and m) as arguments. Then we used a ... Read More

C++ Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

Schonhage-Strassen Algorithm is used to multiply two numbers. The SchonhageStrassen algorithm is an asymptotically fast multiplication algorithm for large integers. In practice the Schonhage-Strassen algorithm starts to outperform older methods like karatsuba and Toom-CooK for numbers beyond 2215 to 2217 (10, 000 to 40, 000 decimal) digits.AlgorithmBegin    function noOfDigit( x)    Declare a variable n and assign n = 0;    while (x > 0)       x = x /10       Increment n    return n End Begin    Algorithm for schonhageStrassenMultiplication:    schonhageStrassenMultiplication(a, b, n, m)    define an array linearConvolution[n + m ... Read More

C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers

Ravi Ranjan
Updated on 03-Jun-2025 17:46:59

2K+ Views

Booth's algorithm is a multiplication algorithm that multiplies two signed binary numbers in 2's complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. In this article, we have an array of multiplicand bits and an array of multiplier bits. Our task is to use Booth's algorithm to find the multiplication of these two binary numbers. Example of Booth's Algorithm In this example, we have used Booth's algorithm to calculate the multiplication of two signed binary numbers mathematically. Input: Multiplier(M): 01011 = 11 Multiplicand(Q): 01110 = 14 ... Read More

C++ Program to Perform Fermat Primality Test

Ravi Ranjan
Updated on 25-Apr-2025 11:22:19

1K+ Views

Fermat's Little theorem states that if 'p' is a prime number and 'a' is an integer that is not divisible by p, then a^(p-1) ≡ 1 modulo p or a^(p-1) mod p = 1. We will use Fermat's Little theorem to test whether the given number is a prime number or not. In this article, we have a number num and a given number of iterations itr. Our task is to implement a primality test for the given number using Fermat's theorem in C++. Steps to Perform Fermat Primality Test The steps to perform Fermat's Primality test are as ... Read More

C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram

Ravi Ranjan
Updated on 06-May-2025 19:00:51

652 Views

The Sieve of Sundaram method is used to generate the prime number within the given range. In this method, first we mark the indices with prime number using the mathematical formula. Then, we use the unmarked indices to get the prime numbers within the given range. In this article, we have defined a range i.e. 'm = 30'. Our task is to generate prime numbers up to 'm' using the Sieve of Sundaram method in C++. Example Here is an example of generating prime numbers up to 10 using the Sieve of Sundaram method: Input: M = 10 Output: ... Read More

C++ Program to Find the GCD and LCM of n Numbers

Ravi Ranjan
Updated on 11-Apr-2025 17:15:30

4K+ Views

The gcd refers to 'Greatest Common Divisor', i.e. greatest common number which can divide all the given numbers. The lcm refers to the 'Least Common Multiple' i.e. the lowest common multiple of all the numbers. To find the gcd and lcm of n numbers in C++, we can use various approaches like iterative approach, or built-in C++ functions. In this article, we are having 'n' number of elements, our task is to find the gcd and lcm of n number of elements using C++. Example Here is an example of GCD and LCM of 4 numbers: Input: Numbers = ... Read More

C++ Program to Implement the Rabin-Miller Primality Test to Check if a Given Number is Prime

Ravi Ranjan
Updated on 06-May-2025 19:01:02

1K+ Views

Rabin-Miller algorithm is a probabilistic primality test algorithm that is used to checks if a given number is likely to be a prime number or not. It is similar to the Fermat's primality test and the Solovay-Stressen test. In this article, we have a number 'p'. Our task is to implement the Rabin-Miller algorithm to check if the given number is a prime number or not in C++. Example Here is an example of checking prime numbers using the Rabin-Miller algorithm: Input: p = 41 Output: 41 is a prime nnumber Here is the explanation of the ... Read More

Advertisements