Thread: Guess the code!

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by King Mir
    That would produce the highest exponent of two storable in each of those datatypes.
    Give that man a cigar!

    Or as I might say, these are several methods that result in "a value with only the most significant bit set".
    Much Ado About Bits

    [edit]New one:
    Code:
    #define DECIMALS(itype) \
    sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Dark_Phoenix
    Well, thats interesting...
    That compiles and runs fine... I get 25
    God! I have to say it again?
    Any number of plus signs! No try and do that with one more plus sign and see if it compiles. Sheesh! What's the point of a brain teaser if there is no teasing? Surely a superior mind like yourself can read what I said before attempting a solution.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #18
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Dave_Sinkula
    Code:
    #define DECIMALS(itype) \
    sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )
    ((itype) -1 < 0) + 1 gives 2 if itype is a signed type, else 1. I'm not sure what 42039 ((itype) -1 < 0) + 1 ) does, though, and I wonder where you got the numbers from... O.O

    Edit: My new one is a complete program this time:

    Code:
    #include <stdio.h>
    #include <complex>
    
    int main() {
        const double h = 0.001;
        std::complex<double> z(1.0, 0.0), f(1.0, h);
        int i = 0;
        do {z *= f; i++;} while (imag(z) > 0);
        printf("%1.9lf\n", i*h - imag(z)/real(z));
        
        system("pause");
        return 0;
    }
    Last edited by TriKri; 11-07-2006 at 07:29 AM.

  4. #19
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    Quote Originally Posted by Mario F.
    What's the other bad thing about having your arms amputated?

    Think of that while you give an answer to this:
    I'm not anywhere close to be a C++ guru to even pretend I can create brain teasers... So, I'll cheat with someone else's brain teaser.

    Without using macros or any other preprocessor tricks, have an apparently infinite number of plus signs appear consecutively, without spaces in between, and still be perfectly valid code? (note: no, not inside a comment. code that the compiler will process)
    Here's your code for creating an apparently infinite number of plus signs...
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int val = 1;
        while (val = 1)
        {
              cout << "+";
        }
        return 0;
    }
    I tested it; untill you exit the program you will get an infinite amount of consecutive + signs!

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I give up.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Let me clarify, I did not know you could use more than two plus signs for an increment until you posted that question yesterday. As I said, interesting... I was not offering a solution.

    As for adding one more plus sign, that will give a non-lvalue error or something I am sure as the increment uses 2 plus signs, having an odd number of +'s should be illegal.....
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Mario F.
    God! I have to say it again?
    Any number of plus signs! No try and do that with one more plus sign and see if it compiles. Sheesh! What's the point of a brain teaser if there is no teasing? Surely a superior mind like yourself can read what I said before attempting a solution.
    I think you could make it work if you made val an object and overrode unairy plus (as in +val) to return a reference. You would also have to override prefix ++ to similarly return a reference.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    These three C programs are of the same length, and essentially do the same thing. What is it they do?

    Code:
    main(a){while(a=getchar())putchar(a-1/((a^32&a)/13*2-11)*13);}
    main(a){while(a=getchar())putchar(a-1/(~(~a|32)/13*2-11)*13);}
    main(a){while(a=getchar())putchar(a+1/(11-(a&32^a)/13*2)*13);}
    It might be helpful to consult the binary representation of ASCII characters, or just input "furrfu sheesh" into stdin.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #24
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by King Mir
    I think you could make it work if you made val an object and overrode unairy plus (as in +val) to return a reference. You would also have to override prefix ++ to similarly return a reference.
    That's the answer yes. The key being the operator+() returning a reference instead of the usual rvalue.

    Code:
    class CClass {
    public:
        CClass(int v):val_(v) {}
        CClass& operator+ () { return *this; }
        CClass& operator++() { ++val_; return *this; }
        friend std::ostream& operator<<(std::ostream&, const CClass&);
    private:
        int val_;
    };
    
    std::ostream& operator<<(std::ostream& os, const CClass& buff) {
        os << buff.val_;
        return os;
    }
    
    int main()
    {
    
        CClass val = 7;
    
        std::cout << +++++++val;
    
        return EXIT_SUCCESS;
    }
    +++++++val is tokenized by the compiler as ++ ++ ++ +. With built-in types this always gives an error because contrary to the ++ operator, the unary + operator returns a rvalue, hence the compiler error. However, if we create a class and overload both operators it is possible to slightly cheat by making the unary operator return a reference while still maintaining the same behavior.

    Of course, this new type CClass is an oddity since it becomes possible to write things like:

    +val = 13;
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #25
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    COORD c; 
       c.X = x - 1 ; 
       c.Y = y - 1 ;
    If you run this into a loop, it would do nothing useful, because you just declared c, you didn't assign any value to it. And even if you did assing, let's say 0 to it, it will always be -1.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #26
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Oops.
    Code:
    #define DECIMALS(itype) \
    ( sizeof(itype) * (CHAR_BIT * 12655UL) / 42039 + ((itype) -1 < 0) + 1 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #27
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try this one:
    Code:
    int firstnum = factorial(a);
    int secondnum = factorial(b);
    int thirdnum = factorial(a-b);
    thenum = firstnum/(secondnum*thirdnum);
    If factorial is a function that gives the factorial.
    Last edited by manutd; 11-07-2006 at 02:52 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #28
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Dave_Sinkula
    Oops.
    Code:
    #define DECIMALS(itype) \
    ( sizeof(itype) * (CHAR_BIT * 12655UL) / 42039 + ((itype) -1 < 0) + 1 )
    Aha, it is the maximum number of characters an integer type itype can require! I didn't see the definition was a macro until I noticed the \ sign, hehe. So I guess 12655/42039 is an approximation of log(2) then, it's pretty close.

  14. #29
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by manutd
    Try this one:
    Code:
    int firstnum = factorial(a);
    int secondnum = factorial(b);
    int thirdnum = factorial(a-b);
    thenum = firstnum/(secondnum*thirdnum);
    If factorial is a function the give the factorial.
    The number of combinations you can choose b things among a things?

  15. #30
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Correct!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 03:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM