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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:45:02+00:00 2026-06-09T22:45:02+00:00

So I tried working out a solution to this problem, but my program is

  • 0

So I tried working out a solution to this problem, but my program is acting very strange.

#include <iostream>
using namespace std;

int triangle_numbers(int n, int meh = 0)
{
    int count = 0;

    //calculate how many divisors there are for meh
    for(int i = 1; i <= meh; i++)
        if(meh%i == 0)
            count++;

    //if the number of divisors for meh is over 500, return meh
    if(count > 500)
        return meh;

    //recursive call to increment n by 1 and set meh to the next triangle number
    triangle_numbers(n+1, meh += n);
}

int main()
{
    int cc = triangle_numbers(1);
    cout << cc << endl;
}

If I output meh and count individually I get accurate results, so I’m not sure why my program is giving me the same number (4246934) even if I do, say, if(count > 10). I have a feeling it may have to do with my recursive call, but everything I’ve tried so far hasn’t worked. Any help?

  • 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-09T22:45:04+00:00Added an answer on June 9, 2026 at 10:45 pm

    You are missing a final return statement necessary to complete the recursion (doesn’t the compiler warn that triangle_numbers does not actually return something in all cases?).

    Once the final value of meh has been computed, you need to have

    return triangle_numbers(n+1, meh += n);
    

    so that meh can be returned all the way back up the call stack and finally to main.

    The number you are seeing now is probably a value left over on the stack after the recursion does end.

    Side note: a classic optimization in this algorithm is to have i iterate up to meh / 2 but no further. Obviously numbers greater than half of meh cannot evenly divide it so they can be skipped.

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

Sidebar

Related Questions

I search for a working solution for a rather simple problem, but could not
Has anyone tried out working with the Delete and update command of SPDataSource used
I am tired of working this out myself , i cannot understand the way
I am tired to find out why this simple query not working. SELECT image_url
I tried everything to get this code working, and I hope someone will save
Im working with the Meteor framework and came by this error when I tried
I have tried and failed to get this working. Basically I am trying to
Firstly I tried to get fabric working, but it kept asking me for a
I am working on this problem for two days and this is driving me
I get the famous out of memory error. But i have tried many of

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.