Thread: Riddle Thread

  1. #46
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516

    Riddle #4: if-statement considered harmful

    How about
    Code:
    (a>b)-(a<b)+(a==b)-(a==b)
    Last edited by CornedBee; 03-28-2009 at 11:45 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  2. #47
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #3: three dwarfs, two paths, one life

    Quote Originally Posted by laserlight View Post
    I wanted to propose the solution of just capturing the dwarves and dragging them in a direction to see their response, but then I realised that I could not be sure that the dwarves were not so bored with their existence that they intended suicide, hence I would have to ask them questions anyway
    I love this solution. My little brother suggested killing the dwarves until one of them decides to tell the truth anyway, but this solution suffers in a similar way.

    And after all, a 50% chance is not too bad, so simply asking for food and a cold beer is probably ok.

    Greets,
    Philip

    PS: ok, I looked it up and will use "dwarves" from now on. Funny language...
    Last edited by CornedBee; 03-28-2009 at 11:46 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  3. #48
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #4: if-statement considered harmful

    I presume that includes ternary operator, and that this is either C or C++?
    Yes.

    Code:
    return (a >b)?1:(a<b)?-1:0;
    Nice try.

    (a>b)-(a<b)+(a==b)-(a==b)
    Right! But you may safely skip the "+(a==b)-(a==b)" part.

    Greets,
    Philip
    Last edited by CornedBee; 03-28-2009 at 11:46 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #49
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Riddle #3: three dwarfs, two paths, one life

    I was inspired by this: Security.
    Last edited by CornedBee; 03-28-2009 at 11:47 AM.
    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. #50
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #3: three dwarfs, two paths, one life

    And I was inspired by this: Labyrinth Puzzle.
    Last edited by CornedBee; 03-28-2009 at 11:47 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Riddle #4: if-statement considered harmful

    Oh, this is good. Every since iMalc pointed out that a simple return a - b; is vulnerable to arithmetic overflow, I have been rather depressed over having to use the more verbose ternary operator version, or resort to an if-else chain. So, return (a > b) - (a < b); is good enough
    Last edited by CornedBee; 03-28-2009 at 11:48 AM.
    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

  7. #52
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #5: more dwarfs

    Right!

    Can someone else explain it?

    Greets,
    Philip
    Last edited by CornedBee; 03-28-2009 at 11:50 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #53
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

    Riddle #4: if-statement considered harmful

    They are not equivalent if I remember correctly. (a < b) will return 0 if false and something non-zero otherwise (but not necessarily 1). The original program returned 1.
    Last edited by CornedBee; 03-28-2009 at 11:48 AM.

  9. #54
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Riddle #4: if-statement considered harmful

    Quote Originally Posted by Perspective
    (a < b) will return 0 if false and something non-zero otherwise (but not necessarily 1).
    No, it will return 1 for non-zero (or true, in the case of C++, which will be converted to 1).
    Last edited by CornedBee; 03-28-2009 at 11:48 AM.
    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

  10. #55
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #6: natural languages

    I introduce two new adjectives fooish and barish.

    A word is fooish if it denotes a property that it has itself.
    A word is barish if it doesn't.

    Examples for fooish words:
    - "short" is a short word.
    - "English" is an English word.
    - "unfrequent" is an unfrequent word.

    Examples for barish words:
    - "long" is not a long word.
    - "German" is not a German word.
    - "rife" is not a rife word.


    I claim that every adjective is either fooish or barish.


    Is "barish" fooish or barish?

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #56
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Number theory in 21 minutes

    Quote Originally Posted by Sebastiani View Post
    >> Its faster to just check if a number, modulo 6469693230 has a prime remainder, since all prime numbers do.

    Are you sure about that? Can you elaborate?
    Yes I'm sure, no I won't elaborate. Well, maybe a little. Specifically, any prime number P modulo a primorial X such that P=> primorial X will have a prime remainder. Obviously it also works for P< X btu then you just check it agaisnt he known primes. This is not a guarantee that the number tested is prime, but it will guarantee it is composite if it fails. It will nto however give you any of the factors of the composite number. The greater the value of X the stronger the evidence for being prime. This method of checking very large primes is an embarrisingly parallelizable method. Since as X grows, it increases teh number of independant checks very quickly. It does however require a very large and comprehensive database of known primes for values of X larger than ~31. The size of the database approaches (X primorial / ln(X primorial)) * sizeof(N) where N is the type of teh storage unit for a single prime. For X = 23 and N is a DWORD its about 46MB, while for 29 its over 1GB, and it grows more rapidly after that.
    Last edited by CornedBee; 03-28-2009 at 11:48 AM.

  12. #57
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396

    Riddle #4: if-statement considered harmful

    This would be more fun if you also banned '<' and '>'
    Last edited by CornedBee; 03-28-2009 at 11:50 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #58
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613

    Riddle #4: if-statement considered harmful

    This reminds me when I listened to Steve Gibson talk about a version of assembly that had no OR. You had to implement your own.
    Last edited by CornedBee; 03-28-2009 at 11:50 AM.

  14. #59
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #4: if-statement considered harmful

    This would be more fun if you also banned '<' and '>'
    I can see some ugly bit twiddling here. Any volunteers? )

    This reminds me when I listened to Steve Gibson talk about a version of assembly that had no OR. You had to implement your own.
    Nothing easier than that if you know that double negation doesn't change the value:
    a OR b == !(!a AND !b)

    I once had to show that AND and NOT form a logical basis, i.e. that one can express every boolean formula by only using AND and NOT (and variables, of course). An even smaller logical basis is NOR. Sounds like a good riddle...

    A far more intriguing example of simplicity is the "One Instruction Set Computer", which only has a single instruction. One possible solution is the SBN instruction, subtract-and-branch-if-negative. It has three operands a, b and c. It works by subtracting the contents of memory location a from those at address b, storing the result at address b and jumping to c if *a > *b. Of course, this machine can be proven to be Turing-complete.

    This takes the meaning of RISC to a higher level.

    Greets,
    Philip
    Last edited by CornedBee; 03-28-2009 at 11:51 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  15. #60
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413

    Riddle #4: if-statement considered harmful

    Quote Originally Posted by Snafuist
    I once had to show that AND and NOT form a logical basis, i.e. that one can express every boolean formula by only using AND and NOT (and variables, of course). An even smaller logical basis is NOR. Sounds like a good riddle...
    I had the impression that proving that NAND and NOR are universal gates is a pretty common assignment for first or second year computing and engineering students.
    Last edited by CornedBee; 03-28-2009 at 11:51 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM