Thread: Prime factors of an integer and sorting them in descending order.

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    8

    Prime factors of an integer and sorting them in descending order.

    Code:
    #include<iostream>
    #include<stack>
    using namespace std;
    void PrimeFactors(int n)
    {
      // Stores prime factors of n
      // in decreasing order
      stack < int >x;
      int i = 2;
      while (n != 1) {
    
        if (n % i == 0) {
    
          // Insert i into stack
          x.push(i);
    
          while (n % i == 0) {
    
            // Update n
            n = n / i;
          }
        }
        // Update i
        i++;
      }
    
      // Print value of stack x
      while (!x.empty()) {
    
        printf("%d ", x.top());
        x.pop();
      }
    }
    
    // Driver Code
    int main()
    {
      int n;
      cout << "Enter value:";
      cin >> n;
      // function Call
      PrimeFactors(n);
      return 0;
    }
    insert
    Code:
    Output:
    Enter value:76 19 2 
    im supposed to see 
    19 2 2
    Last edited by Salem; 09-30-2022 at 09:47 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    1. When posting on forums (or indeed anywhere), it's a good idea to use "copy as text" in your IDE and/or "paste as text" in your browser. If the thing you're copying to doesn't understand the fancy markup added by your IDE, then the result is a mess.

    2. Your code only inputs one value, yet you claim to type in three?

    3. If the code doesn't work, try adding some debug prints, like so.
    Code:
      while (n != 1) {
        cout << "DEBUG: Loop: " << n << " " << i << endl; 
        if (n % i == 0) {
          cout << "DEBUG: push: "  << i << endl; 
          // Insert i into stack
          x.push(i);
     
          while (n % i == 0) {
             // Update n
            n = n / i;
          }
          cout << "DEBUG: new n: "  << n << endl; 
        }
        // Update i
        i++;
      }
    When you see a DEBUG that doesn't match your expectation of what you think should have happened, you've found a bug.

    Whether that bug is in your code, or your understanding of the problem is something for you to figure out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    Code:
    
    
    Code:
    #include <stack>
    using namespace std;  
    void PrimeFactors(int n){
        // Stores prime factors of n
        // in decreasing order
        stack<int> x;
        int i = 2;
        while (n != 1) {
            if (n % i == 0) {
     
                // Insert i into stack
                x.push(i);
     
                while (n % i == 0) {
     
                    // Update n
                    n = n / i;
                }
            }
     
            // Update i
            i++;
        }
     
        // Print value of stack x
        while (!x.empty()) {
     
            printf("%d ", x.top());
            x.pop();
        }
    }
    // Driver Code
    int main(){
        int n;
        cout <<"Enter value:";
        cin >> n;
        // function Call
        PrimeFactors(n);
        return 0;
    }
    ive tried the debug and here is the output:
    Enter value:76
    DEBUG: Loop: 76 2
    DEBUG: push: 2
    DEBUG: new n: 19
    DEBUG: Loop: 19 3
    DEBUG: Loop: 19 4
    DEBUG: Loop: 19 5
    DEBUG: Loop: 19 6
    DEBUG: Loop: 19 7
    DEBUG: Loop: 19 8
    DEBUG: Loop: 19 9
    DEBUG: Loop: 19 10
    DEBUG: Loop: 19 11
    DEBUG: Loop: 19 12
    DEBUG: Loop: 19 13
    DEBUG: Loop: 19 14
    DEBUG: Loop: 19 15
    DEBUG: Loop: 19 16
    DEBUG: Loop: 19 17
    DEBUG: Loop: 19 18
    DEBUG: Loop: 19 19
    DEBUG: push: 19
    DEBUG: new n: 1

    my expected output is 19 2 2
    Last edited by kattaboi; 10-01-2022 at 07:44 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > ive tried the debug and here is the output:
    So what did you learn from it?

    You went from 76 down to 19 (presumably via 38), but you only have one '2' pushed onto your stack.

    Maybe you need to be pushing additional results?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    got it. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-28-2017, 09:09 PM
  2. Sorting in descending order.
    By november1992 in forum C Programming
    Replies: 10
    Last Post: 04-14-2012, 02:08 PM
  3. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  4. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM
  5. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM

Tags for this Thread