Thread: 3n+1 problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    3n+1 problem

    i think i ve solved it, it gives me correct answers, but when i try to submit, it says wrong answer.

    is there something wrong with my code?

    Code:
    #include <stdio.h>
    int main() {
        unsigned long i,x,y,k,max,s,temp;
        while (scanf("%lu %lu",&x,&y)!=EOF){
              if (x>y){
                       temp=y;
                       y=x;
                       x=temp;
                       }
             max=1;
            for (i=x;i<=y;i++){
                s=function(i,1);
                if (s>max){
                              max=s;
                }
                }
                printf("%lu %lu %lu",x,y,max);
                }
                return 0;
    }
    function(unsigned long n, unsigned long m) {
        while (n!=1){
                  if (n%2==1){
                              n=n*3+1;
                              m++;
                              }
                  else {
                       n=n/2;
                       m++;
                       }
                       }
         return m;
                             }
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to make your indentation somewhat more consistent, and using whitespace to make your code more readable:
    Code:
    #include <stdio.h>
    
    unsigned long function(unsigned long n, unsigned long m);
    
    int main() {
        unsigned long i, x, y, max, s, temp;
        while (scanf("%lu %lu", &x, &y) != EOF) {
            if (x > y) {
                temp = y;
                y = x;
                x = temp;
            }
    
            max = 1;
            for (i = x; i <= y; i++) {
                s = function(i, 1);
                if (s > max) {
                    max = s;
                }
            }
    
            printf("%lu %lu %lu", x, y, max);
        }
        return 0;
    }
    
    unsigned long function(unsigned long n, unsigned long m) {
        while (n != 1) {
            if (n % 2 == 1) {
                n = n * 3 + 1;
                m++;
            }
            else {
                n = n / 2;
                m++;
            }
        }
        return m;
    }
    Notice that I have given function an explicit return type of unsigned long (otherwise it would have a default return type of int) and used a function prototype. I have also removed the variable k since it was not used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    17
    thanks, but again it says wrong answer :/

    i think there is something missing but dont know what it is

    ps: im talking about uva online judge.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, at least you have eliminated the possibility of an incorrect range. Now, double check if the format of output produced by your program matches the required format.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    17
    well i found out what it actually was

    if we have input like
    1 10

    it should print out
    1 10 20

    and if we have input like

    10 1

    it should be

    10 1 20

    but the problem is that i printed the first two numbers at the end, after actually changing their value by using the variable temp.

    so the correct is:

    Code:
    #include <stdio.h>
    
    unsigned long function(unsigned long n, unsigned long m);
    
    int main() {
        unsigned long i, x, y, max, s, temp;
        while (scanf("%lu %lu", &x, &y) != EOF) {
              printf("%lu %lu",x,y);
            if (x > y) {
                temp = y;
                y = x;
                x = temp;
            }
    
            max = 1;
            for (i = x; i <= y; i++) {
                s = function(i, 1);
                if (s > max) {
                    max = s;
                }
            }
    
            printf(" %lu\n",max);
        }
        return 0;
    }
    
    unsigned long function(unsigned long n, unsigned long m) {
        while (n != 1) {
            if (n % 2 == 1) {
                n = n * 3 + 1;
            }
            else {
                n = n / 2;
            }
    m++;
        }
        return m;
    }
    thank you very much laserlight for you help and time.
    Last edited by nick2; 06-01-2009 at 03:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM