Question

In: Computer Science

in C++ Description: For a given integer n > 1, the smallest integer d > 1...

in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.

            Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a prime factor, push it on a stack)

            For example, if n is 3960 the prime factorization is

                                                11 * 5 * 3 * 3 * 2 * 2 * 2

            You may use the code for implementing a stack that we used in class, found in the chapter 7 folder

Solutions

Expert Solution

void leastPrimeFactor(int n)

{

    // Create a vector to store least primes.

    // Initialize all entries as 0.

    vector<int> least_prime(n+1, 0);

  

    // We need to print 1 for 1.

    least_prime[1] = 1;

  

    for (int i = 2; i <= n; i++)

    {

        // least_prime[i] == 0

        // means it i is prime

        if (least_prime[i] == 0)

        {

            // marking the prime number

            // as its own lpf

            least_prime[i] = i;

  

            // mark it as a divisor for all its

            // multiples if not already marked

            for (int j = 2*i; j <= n; j += i)

                if (least_prime[j] == 0)

                   least_prime[j] = i;

        }

    }

  

    // print least prime factor of

    // of numbers till n

    for (int i = 1; i <= n; i++)

        cout << "Least Prime factor of "

             << i << ": " << least_prime[i] << "\n";

}

  

// Driver program to test above function

int main()

{

    int n = 10;

    leastPrimeFactor(n);

    return 0;

}


Related Solutions

Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see if it is divisible by a prime number less than or equal to it’s square root. If it is not divisible by an of these numbers then it is prime. We will show that this is a valid test. prove ∀n, r, s ∈ N+, r s ≤ n → (r ≤ √n ∨ s ≤ √n) b) Prove ∀ n ∈ N+ ,...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output: 2,3,5,7 n=16, output: 2,3,5,7,11,13 In Java please
that, given an integer N and an integer K, returns the minimum number of rounds that...
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1....
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1. a. Find the value of the constant C (in terms of n) that makes this a probability density function. b. For what values of n does the expected value E(X) exist? Why? c. For what values of n does the variance var(X) exist? Why?
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT