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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:05:38+00:00 2026-06-08T04:05:38+00:00

here i have written a program on (3n+1) problem.it’s also a problem from UVa.

  • 0

here i have written a program on (3n+1) problem.it’s also a problem from UVa. I believe it’s a known problem.but when i am going to submit it in that online judge community it is sending me a Time Exceeding Error. the time limit is 3 sec. I have done what my little knowledge can do. If anyone can help me with some more advice I would be glad. my code is:

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
    int loop=1;
    unsigned short *cyclelength;
    while(loop=1){
        unsigned int x,y,i,j,num,count=0,p,k,c,max;
        for(;;){
            cout<<"enter two integers. they must not be equal and must be between 1 and 1000000\n";
            while(!(cin>>i>>j)){
                cout<<"please enter a number\n";
                cin.clear();
                cin.ignore(1000,'\n');
            }
            if(i>=1 && i<1000000 && j>=1 && j<1000000  && i!=j){
                break;
            }
            else{
                printf("try the whole process again\n");
            }
        }
        if(i>j){
            x=i;
            y=j;
        }
        else{
            x=j;
            y=i;
        }/*making x always greater than y*/
        cyclelength=(unsigned short *)malloc(1000000 *sizeof(unsigned short));
        if (NULL==cyclelength){
            printf("process aborted");
            return 0;
        }
        else{
            /*solution part for the range of number. and solution for each number  put into cyclelength.*/
            num=y;
            while(num<=x){
                p=1;
                k=num;
                while(k!=1){
                    if(k%2==0)
                        k=k/2;
                    else
                        k=3*k+1;
                    p+=1;
                    }
                cyclelength[count]=p;
                num+=1;
                count+=1;
            }
            c=0;
            max=cyclelength[c];
            for(c=0;c<x-y-1;c+=1){
                if(max<cyclelength[c+1]){
                    max=cyclelength[c+1];
                }
            }
            free(cyclelength);
            cyclelength = NULL;
            cout<<i<<" "<<j<<" "<<max<<'\n';
        }
    }
}
  • 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-08T04:05:40+00:00Added an answer on June 8, 2026 at 4:05 am

    The problem is that you are not allowing your program to end when online judging engine has finished providing inputs. You need to detect that the judging engine has finished providing inputs and then exit the program.

    There is a sample code (in C) on their website here Spoiler Alert: This is actually a solution to 3n+1 problem that kind of explains this. Notice the following condition in Main.

    while (scanf("%d %d\n",&m,&n)==2){//perform logic}
    

    This will keep the program running only while there are inputs to be processed.

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

Sidebar

Related Questions

Well, i have written a simple python program that parses HTML with HTMLParser. Here
I have written a Java program which I package and run from a JAR
I have written a program that is compiled and run in Netbeans and Eclipse
I have written a WiX installer that works perfectly with Windows XP, but when
I have written a python program with a PyQt GUI that, using the imaplib,
I have a program written in python that uses dbus to detect inserted usb
I have written a program which could sort 5 strings you inputed from small
I have written a small program, while reading a book about swing, that creates
I have a program written in Java that reads in a file that is
Here is the code I have written for Rectangle class. class Rectangle (l: Double,

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.