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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:07:20+00:00 2026-05-23T15:07:20+00:00

I am trying to make the programming challanges from the book written by Steven

  • 0

I am trying to make the programming challanges from the book written by Steven S. Skiena and Miguel A. Revilla.

But I am already having problems with the first challenge called “The 3n+1 problem”.

The challenge can be found here: http://www.programming-challenges.com/english/pdfs/110101.pdf

The solution I though of is.

#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int CalcCycleLenght(int n)
{
    int t = 1;
    while (n != 1)
    {
        if (n % 2)
        {
            n = 3 * n + 1;
        }else{
            n = n / 2;
        }
        t++;
    }
    return t;
}

int main(int argc, char* argv[])
{
    int i;
    int j;
    vector<int> vi;
    vector<int> vj;
    vector<int> vm;

    int p = 0;

    while (scanf("%d %d", &i, &j) != EOF)
    {
        vi.push_back(i);
        vj.push_back(j);
        int max = 0;
        int n;
        for (int t = i; t <= j; t++)
        {
            n = CalcCycleLenght(t);
            if (n > max)
                max = n;
        }
        vm.push_back(max);
    }

    for (int i=0; i < vi.size(); i++)
    {
        printf("%d %d %d \n", vi[i], vj[i], vm[i]);
    }   

    return 0;
}

It compiles correctly (although I think I have one extra include) and the answers are correct according to the sample input and output. But the online judge says “wrong answer”. I think my problem is the way I input or output the numbers.

[EDIT]

I Solved it. Seems that the problem was that sometimes i was bigger than j. I also removed the vectors and made the code more efficient. Here is the new main function.

int main(int argc, char* argv[])
{
    int i;
    int j;

    int a;
    int b;

    int max;
    int p;

    while (scanf("%d %d", &i, &j) != EOF)
    {
        if (i < j)
        { a = i; b = j; }
        else
        { a = j; b = i; }   

        max = 0;
        for (int t = a; t <= b; t++)
        {           
            p = CalcCycleLenght(t);
            if (p > max)
                max = p;
        }
        printf("%d %d %d\n",i,j,max);
    }    
    return 0;
}

Thanks

  • 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-23T15:07:20+00:00Added an answer on May 23, 2026 at 3:07 pm

    You output an extra space at the end of each line. Try removing this space, as automated judging often fails on such things. Other than that, your algorithm looks correct to me.

    By the way, there’s no reason to put the numbers in vectors – you can just output the results for each line as you calculate them.

    Edit: It isn’t clear from the specifications if it’s possible that i>j. Your code will output 0 in that case, so if removing the space doesn’t help, you can try swapping i and j in that case so that you have meaningful output (just make sure you output i and j in the same order that you got them).

    Edit 2: This is most likely the problem: You use an int in your calculations, which on most platforms can hold values up to 2147483647. But The calculation of the cycle length will require numbers greater than that (for example, for the initial value of 113383). If you use long long (assuming it’s supported) you should be OK.

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

Sidebar

Related Questions

I am trying to make my first steps with Firefox addon programming but I
I am trying to make multiprocessing and socket programming work together, but, I am
I'm programming a short Paint program like and I'm trying to make an MDI
I'm a .NET developer trying to make the leap into objective-c iPhone programming. I
I'm new to iPhone programming, and I'm trying to make a simple program without
I'm trying to make a tetris game for android to help learn game programming
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app
i am learning c programming. I am trying to make my own program similar
First and foremost. I'm totally new to programming in Excel. I'm trying to make
Currently I am having issues with QTimer. I am trying to make each Product

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.