Found 34484 Articles for Programming

C++ Program to Compute DFT Coefficients Directly

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

214 Views

In discrete Fourier transform (DFT), a finite list is converted of equally spaced samples of a function into the list of coefficients of a finite combination of complex sinusoids. They ordered by their frequencies, that has those same sample values, to convert the sampled function from its original domain (often time or position along a line) to the frequency domain.AlgorithmBegin Declare three variables which are the coefficient of linear equation and max value Read the variables Define a class with two variables real, img Create a constructor and ... Read More

C++ Program to Implement the Bin Packing Algorithm

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

1K+ Views

The bin packing problem is a special type of cutting stock problem. In the bin packing problem, objects of different volumes must be packed into a finite number of containers or bins each of volume V in a way that minimizes the number of bins used. In computational complexity theory, it is a combinational NP-hard problem.When the number of bins is restricted to 1 and each item is characterized by both a volume and a value, the problem of maximizing the value of items that can fit in the bin is known as the knapsack problem.AlgorithmBegin    Binpacking(pointer, size, no ... Read More

C++ Program to Perform Partition of an Integer in All Possible Ways

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

699 Views

Here is a C++ Program to get all the unique partitions of a given integer such that addition of a partition results an integer. In this program, a positive integer n is given, and generate all possible unique ways to represent n as sum of positive integers.AlgorithmBegin    function displayAllUniqueParts(int m):    Declare an array to store a partition p[m].    Set Index of last element k in a partition to 0    Initialize first partition as number itself, p[k]=m    Create a while loop which first prints current partition, then generates next partition. The loop stops when the current ... Read More

C++ Program to Solve the Fractional Knapsack Problem

Daniol Thomas
Updated on 17-Feb-2022 13:07:35

5K+ Views

In Fractional knapsack problem, a set of items are given, each with a weight and a value. We need to break items for maximizing the total value of knapsack and this can be done in greedy approach.AlgorithmBegin Take an array of structure Item Declare value, weight, knapsack weight and density Calculate density=value/weight for each item Sorting the items array on the order of decreasing density We add values from the top of the array to total value until the bag is full, i.e; total value

C++ Program to Solve the 0-1 Knapsack Problem

Nancy Den
Updated on 30-Jul-2019 22:30:25

5K+ Views

In 0-1 knapsack problem, a set of items are given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is large as possible.InputValue = [10, 20, 30, 40, 60, 70] Weight=[1, 2, 3, 6, 7, 4] int w=7Outputknapsack value is: 100AlgorithmBegin Input: set of items each with a weight and a value Set knapsack capacity Number of items=sizeof(values) / sizeof(values[0]) ... Read More

C++ Program to Implement Fermat’s Little Theorem

Nancy Den
Updated on 30-Jul-2019 22:30:25

600 Views

Fermat's little theorem is one of the fundamental results of elementary number theory and is the basis for the Fermat primality test. The theorem is named after Pierre de Fermat, who stated it in 1640. The Theorem states that if p is a prime number, then for any integer a, the number a p–a is an integer multiple of p.AlgorithmBegin    Function power() is used to compute a raised to power b under modulo M    function modInverse() to find modular inverse of a under modulo m :    Let m is prime    If a and m are relatively ... Read More

C++ Program to Implement Extended Euclidean Algorithm

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

1K+ Views

The Extended Euclidean Algorithm is just a another way of calculating GCD of two numbers. It has extra variables to compute ax + by = gcd(a, b). It's more efficient to use in a computer programAlgorithmBegin Declare variable a, b, x and y gcdExtended(int a, int b, int *x, int *y) if (a == 0) *x = 0; *y = 1; return b; Take two variables to store the result ... Read More

C++ Program to Implement Euler Theorem

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

378 Views

This is a C++ Program which demonstrates the implementation of Euler Theorem. The number and modular must be coprime for the modular multiplicative inverse to exist.AlgorithmBegin Take input to find modular multiplicative inverse Take input as modular value Perform inverse array function: modInverse(x + 1, 0); modInverse[1] = 1; for i = 2 to x modInverse[i] = (-(y / i) * modInverse[y mod i]) mod y + y return modInverse EndExample Code#include #include using namespace std; vector inverseArray(int x, int y) {    vector modInverse(x + 1, 0);    modInverse[1] = 1;    for (int i = 2; i

C++ Program to Implement Russian Peasant Multiplication

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

407 Views

Russian Peasant algorithm to multiply two numbers. It is a quick algorithm to calculate multiplication of two long numbers.AlgorithmBegin    Russianpeasant(num1, num2)    Int result=0    while (num2 > 0)       if (num2 and 1)          result = result + n;          num1= num1 left shift 1;          num2= num2left shift 1; return result EndExample Code#include using namespace std; unsigned int russianPeasant(unsigned int n, unsigned int m) {    int result = 0;    while (m > 0) {       if (m & 1)          result = result + n;          n = n > 1;    }    return result; } int main() { cout

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

Advertisements