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

Suppose you are given an integer c and an array, A, indexed from 1 to n,...
Suppose you are given an integer c and an array, A, indexed from 1 to n, of n integers in the range from 0 to 5n (possibly with duplicates). i.e. 0 <= A[i ] <= 5n " I = {1, .., n}. a.) Write an efficient algorithm that runs in O(n) time in a pseudo code for determining if there are two integers, A[i] and A[j], in A whose sum is c, i.e. c = A[i] + A[j], for 1...
C program //In this assignment, we will find the smallest positive integer that // can be...
C program //In this assignment, we will find the smallest positive integer that // can be expressed as a sum of two positive cube numbers in two distinct ways. // More specifically, we want to find the smallest n such that n == i1*i1*i1 + j1*j1*j1, // n == i2*i2*i2 + j2*j2*j2, and (i1, j1) and (i2, j2) are not the same in the sense that // not only (i1, j1) not euqal to (i2, j2), but also (i1, j1)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT