Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:06:57+00:00 2026-05-26T21:06:57+00:00

I am trying to make a function that can return the prime factors of

  • 0

I am trying to make a function that can return the prime factors of a given number in an array (or multi-set, but I’m trying to use an array).

For example, if I put in 12, I want to get 2, 2, and 3, not 2, and 3 like with a set. This is so that I can use these to see if it is a Smith number or not, so I need the numbers seperately.

Also, I am taking a recursive approach.

I have tried (to no avail) to return the array many ways, including passing an initial pointer into the code which points to a space to store the array.

I’ve tried just initializing the array in the function and then returning it.

From what I can tell, I can get the array back from the base case iteration and then when trying to construct a new array with size oldArray+1 to copy values to, things get messy. This is where I get lost.

From what I’ve read, although this isn’t the most efficient implementation, I should be able to make it work.

I have a function, nextPrime(int n), which given n will give back the next prime up from that number.

See source below:

int* find(int n, int p) {

int root = (int) floor(sqrt(n));
if (p > root) {
    // Base case, array gets initialized and returned
    // depending on value of n and p.
    if (n > 1) {
        factors = new int[1];
        factors[0] = n;
        return factors;
    }
    else {
        factors = new int[0];
        return factors;
    }
}
else
    if (n%p == 0){
        // Inductive step if p is a factor
        int newFloor = (int) floor(n/p);
        factors = find(newFloor, p);

        // Initialize new array.
        int* newFactors;
        newFactors = new int[(sizeof(factors) / sizeof(int)) + 1];

        // Add p to first slot, fill rest with contents of factors.
        factors[0] = p;
        for (int i = 0; i < (sizeof(factors) / sizeof(int)); i++) {
            newFactors[i+1] = factors[i];
        }

        return newFactors;
    }
    else {
        // Inductive step p isn't a factor of n
        factors = find(n, factors, nextPrime(p));
        return factors;
    }
}

As I say, the error is with returning the array and using its value, but why does it seem to return OK from the first iteration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:06:58+00:00Added an answer on May 26, 2026 at 9:06 pm

    Something like this could work. Not terribly efficient !!

    void FindFactors( int number , std::vector<int>&  factors )
    {
        for ( int i = 2; i <= number; ++i )
        {
            if ( number % i == 0 )
            {
                factors.push_back( i );
                FindFactors( number / i , factors);
                break;
            }
        }
    }
    
    int main()
    {
    
        std::vector<int> factors;
        FindFactors( 121 , factors );
        return 0;
    }
    

    After you call the function factors will contain only the prime factors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to make a function that will return an element of type point:
I'm trying to make a function that holds state but is called with foo().
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to make a notification area that will show alerts. return this.each(function() {
I am trying to make a function return data from an ajax call that
I'm trying to make a function that can take an enum type, display all
I'm trying to make a function that can be applied to a value returned
I'm trying to write a function that will return the number of days between
I'm trying to make a function that will take short hand hex color to
I'm trying to make a Javascript function that will take a mathematical expression and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.