Thread: Riddle Thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Number theory in 21 minutes

    Damn... My argument to why I don't understand any of this is... I haven't finished school xP Now make the algorithm for randomness for me >: D
    Last edited by CornedBee; 03-28-2009 at 11:39 AM.
    Currently research OpenGL

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

    Number theory in 21 minutes

    Well that makes for an interesting optimization for large prime number searches. I wonder what percentage of workload it would reduce to first check that a number met that particular criteria.
    Last edited by CornedBee; 03-28-2009 at 11:39 AM.

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Number theory in 21 minutes

    Quote Originally Posted by abachler View Post
    Well that makes for an interesting optimization for large prime number searches. I wonder what percentage of workload it would reduce to first check that a number met that particular criteria.
    A number is dividable by 2 and 3 if it is dividable by 24, so checking if 2 or 3 divides a given number is probably much faster (and tells you more about the number).

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

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hey that's pretty cool, though. Nice one.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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

    Number theory in 21 minutes

    Its faster to just check if a number, modulo 6469693230 has a prime remainder, since all prime numbers do.
    Last edited by CornedBee; 03-28-2009 at 11:42 AM.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Number theory in 21 minutes

    The greatest common factor explanation of this is great
    Last edited by CornedBee; 03-28-2009 at 11:43 AM.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Number theory in 21 minutes

    >> 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?
    Last edited by CornedBee; 03-28-2009 at 11:43 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    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.

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

    Riddle #5: more dwarfs

    There are seven dwarfs, each one wearing a unique hat. A wind blows off the hats. The dwarfs start running after their hats and each dwarf puts on the first hat that he manages to catch. Eventually every dwarf has a hat again.

    What is the probability that exactly six dwarfs are wearing their own hat now?

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

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

    Riddle #5: more dwarfs

    I would say that equals (6!/(6*5!))-1.
    Last edited by CornedBee; 03-28-2009 at 11:45 AM.

  12. #12
    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

  13. #13
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312

    Riddle #4: if-statement considered harmful

    Oops, I skipped #4, so here it is.

    Consider the following program:

    Code:
    int cmp(int a, int b)
    {
            if(a > b) {
                    return 1;
            } else if(a < b) {
                    return -1;
            } else {
                    return 0;
            }
    }
    Can you come up with an implementation that doesn't use conditionals, i.e. if, for, while, switch?

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

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

    Riddle #4: if-statement considered harmful

    I presume that includes ternary operator, and that this is either C or C++?
    Last edited by CornedBee; 03-28-2009 at 11:45 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

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    Riddle #4: if-statement considered harmful

    Code:
    return (a >b)?1:(a<b)?-1:0;
    --
    Mats
    Last edited by CornedBee; 03-28-2009 at 11:45 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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