3n+1 problem

This is a discussion on 3n+1 problem within the C Programming forums, part of the General Programming Boards category; i think i ve solved it, it gives me correct answers, but when i try to submit, it says wrong ...

  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
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,720
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21