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

  • Home
  • SEARCH
  • 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 7180223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:18:10+00:00 2026-05-28T17:18:10+00:00

have wrote the code for what i see to be a good algorithm for

  • 0

have wrote the code for what i see to be a good algorithm for finding the greatest prime factor for a large number using recursion. My program crashes with any number greater than 4 assigned to the variable huge_number though. I am not good with recursion and the assignment does not allow any sort of loop.

#include <stdio.h>

long long prime_factor(int n, long long huge_number);

int main (void)
{
    int n = 2;
    long long huge_number =  60085147514;
    long long largest_prime = 0;

    largest_prime = prime_factor(n, huge_number);
    printf("%ld\n", largest_prime);

    return 0;
}

long long prime_factor (int n, long long huge_number)
{
    if (huge_number / n == 1)
        return huge_number;
    else if (huge_number % n == 0)
        return prime_factor (n, huge_number / n);        
    else
        return prime_factor (n++, huge_number);
}

any info as to why it is crashing and how i could improve it would be greatly appreciated.

  • 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-28T17:18:11+00:00Added an answer on May 28, 2026 at 5:18 pm

    Even fixing the problem of using post-increment so that the recursion continues forever, this is not a good fit for a recursive solution – see here for why, but it boils down to how fast you can reduce the search space.

    While your division of huge_number whittles it down pretty fast, the vast majority of recursive calls are done by simply incrementing n. That means you’re going to use a lot of stack space.

    You would be better off either:

    • using an iterative solution where you won’t blow out the stack (if you just want to solve the problem) (a); or
    • finding a more suitable problem for recursion if you’re just trying to learn recursion.

    (a) An example of such a beast, modeled on your recursive solution, is:

    #include <stdio.h>
    
    long long prime_factor_i (int n, long long huge_number) {
        while (n < huge_number) {
            if (huge_number % n == 0) {
                huge_number /= n;
                continue;
            }
            n++;
        }
        return huge_number;
    }
    
    int main (void) {
        int n = 2;
        long long huge_number =  60085147514LL;
        long long largest_prime = 0;
    
        largest_prime = prime_factor_i (n, huge_number);
        printf ("%lld\n", largest_prime);
    
        return 0;
    }
    

    As can be seen from the output of that iterative solution, the largest factor is 10976461. That means the final batch of recursions in your recursive solution would require a stack depth of ten million stack frames, not something most environments will contend with easily.

    If you really must use a recursive solution, you can reduce the stack space to the square root of that by using the fact that you don’t have to check all the way up to the number, but only up to its square root.

    In addition, other than 2, every other prime number is odd, so you can further halve the search space by only checking two plus the odd numbers.

    A recursive solution taking those two things into consideration would be:

    long long prime_factor_r (int n, long long huge_number) {
        // Debug code for level checking.
    
        // static int i = 0;
        // printf ("recursion level = %d\n", ++i);
    
        // Only check up to square root.
    
        if (n * n >= huge_number)
            return huge_number;
    
        // If it's a factor, reduce the number and try again.
    
        if (huge_number % n == 0)
            return prime_factor_r (n, huge_number / n);
    
        // Select next "candidate" prime to check against, 2 -> 3,
        //   2n+1 -> 2n+3 for all n >= 1.
    
        if (n == 2)
            return prime_factor_r (3, huge_number);
    
        return prime_factor_r (n + 2, huge_number);
    }
    

    You can see I’ve also removed the (awkward, in my opinion) construct:

    if something then
        return something
    else
        return something else
    

    I much prefer the less massively indented code that comes from:

    if something then
        return something
    return something else
    

    But that’s just personal preference. In any case, that gets your recursion level down to 1662 (uncomment the debug code to verify) rather than ten million, a rather sizable reduction but still not perfect. That runs okay in my environment.

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

Sidebar

Related Questions

I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I have a problem. I wrote example code and I want to build it
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I have been refactoring throwaway code which I wrote some years ago in a
I've wrote this simple piece of code. And I have a slight problem with
i'm developing an web application in .net platform. i have wrote an Handler code
I have a test program that I wrote to try and debug a GMutex
Here is some code I wrote (using GCC's __restrict__ extension to C++): #include <iostream>
Sometimes I have to write code that alternates between doing things and checking for

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.