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 9020099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:55:53+00:00 2026-06-16T04:55:53+00:00

Here is the problem we should solve with C++: GCD ( 2m , 2n

  • 0

Here is the problem we should solve with C++:

GCD ( 2m , 2n )         =  2 * GCD( m , n )
GCD ( 2m , 2n+1 )       = GCD ( m , 2n+1 )
GCD ( 2m+1,  2n+1 ) = GCD ( n-m , 2m+1 )  (m<n)
GCD ( m , m )       = m

and here is the function i wrote:

int GCD(int n1, int n2)
{
    bool n1Zoj, n2Zoj;
    n1Zoj = (n1%2 == 0);
    n2Zoj = (n2%2 == 0);

    if(n1Zoj && n2Zoj)
        return 2 * GCD(n1/2, n2/2);

    if(n1Zoj && !n2Zoj)
        return GCD(n1/2, n2);

    if(!n1Zoj && !n2Zoj)
        return GCD((n2-n1)/2, n1);

    if(n1 == n2)
        return n1;
}

(*”Zoj” means “Even” in my language(persian) )

when i pass 5 as the second argument, program crashes and prints this message:

Segmentation fault (core dumped)

Exit code is 139. I’m using Code::Blocks on ubuntu 12.04 that uses g++ as compiler.

UPDATE: Program crashes with 5,10,15,20,25,…

UPDATE: i think the correct form of function is:

int GCD(int n1, int n2)
{
    if (n1 > n2)
        std::swap(n1, n2);

    //std::cout<<"GCD is called with params: "<<n1<<" & "<<n2<<std::endl;
    bool n1Zoj, n2Zoj;

    n1Zoj = (n1%2 == 0);
    n2Zoj = (n2%2 == 0);

    if(n1 == n2)
        return n1;

    if(n1Zoj && n2Zoj)
        return 2 * GCD(n1/2, n2/2);

    if(n1Zoj && !n2Zoj)
        return GCD(n1/2, n2);

    if(!n1Zoj && n2Zoj)
        return GCD(n2/2, n1);

    if(!n1Zoj && !n2Zoj)
        return GCD((n2-n1)/2, n1);
}
  • 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-06-16T04:55:55+00:00Added an answer on June 16, 2026 at 4:55 am

    When (n1Zoj && n2Zoj) evaluates to true, what do you do? You call

    return 2 * GCD(n1, n2);
    

    which calls the function with the exact same parameters, resulting in infinite recursion, a blown out stack, and a stack overflow (segmentation fault).

    Protip – learn to debug – I can’t emphasize how extremely important this is.

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

Sidebar

Related Questions

Here is the problem I am trying to solve: Euler 20 . n! means
Here is the problem statement: Calling a setter on the object should result in
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
I have a problem here I can't solve. I have a database of houses
I've got some problem with my function and don't know how to solve this
Here's the problem I'm trying to solve. I want to pass a date, then
Here is the problem that I need to solve: This is the table that
Here is problem I'm trying to solve. It's for logistics company. I got all
I've created an app to solve sudoku grids...but here is my problem : my
I seem to have a 32bit vs. 64bit problem here. It should be simple

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.