Advanced: Calculate square root with templates

This is a discussion on Advanced: Calculate square root with templates within the Contests Board forums, part of the Community Boards category; I've decided to host a contest in C++. This contest involves template metaprogramming, therefore, the language is required to be ...

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Exclamation Advanced: Calculate square root with templates

    I've decided to host a contest in C++.

    This contest involves template metaprogramming, therefore, the language is required to be C++. If you find this task hard, don't worry, because template programming is quite tricky!

    The goal is to write a templated struct or class which contains a member which is the square root of the template argument. The struct must be written in this form:
    Code:
    template<int x /*other arguments allowed, but must have default values*/ >
    struct SquareRoot
    {
    static const int v = /* integer part of sqrt(x) */
    /*other members allowed*/
    };
    it must be called 'SquareRoot' and it must have a member called 'v' which contains the integer i for which
    i <= sqrt(x) < i + 1
    which means that i contains the truncated value of sqrt(x).

    Example;
    Code:
    cout << SquareRoot<9>::v; //Must output 3
    cout << SquareRoot<1000>::v; //Must output 31
    cout << SquareRoot<82>::v; //Must output 9

    //
    The following criterias are considered (in order) to determine the winner of the contest:
    1. The maximum value of x for which the program compiles (maximum 2^31-1 on my machine) and gives the correct value.
      x will not be smaller than 1.
    2. The shortest source code, measured in bytes
    3. The time when the email with the solution arrived.
    Some other rules:
    • It is allowed to 'update' your solution if you find a better one.
    • The deadline is Sunday, October, 3rd
    • Entries should be mailed to contest@barbapapqa.cjb.net
    • My primary compiler is MSVC++.NET; if you use another one, please specify in your entry (please use free compilers).
      Microsofts latest C++ compiler can be downloaded for free here.
    • The calculation must be done entirely in the compiler. As an example, this code must be able to compile:
      Code:
      char data[ SquareRoot<16>::v ];
    After the deadline, it'll probably take a day or two to judge the contest, depending on the number of entries and different compilers used.

    The winner will recieve... well... some reputation.

    I cannot think of anything more right now.

    UPDATE:
    The first criteria of judgement is very compiler-dependent. Hopefully, every entry will will compile in the same compiler.

    Please post in this thread if you're participating!

    These are the people that have submitted entries which complies for at least some values of x:
    • Fordy
    • Okinrus
    • Mr Wizard
    • LuckY
    • xErath
    A number of people (4) have submitted solutions involving the use of std::sqrt. These solutions will not work, because the value of SquareRoot<x>::v must be known at compile time.
    Last edited by Sang-drax; 09-27-2004 at 05:42 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    this is outta my league man..

    maybe next time
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well I have a solution that works, as far as I can tell, according to your guidelines. It's not exactly fast or anything and It's probably not that short either, but it seems to work. I guess I'll send it in and see if I can "update" it later
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Im in.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    JaWiB, you should send in your solution.
    This problem isn't trivial.

    So far, I haven't recieved any (working) solutions.
    Last edited by Sang-drax; 09-19-2004 at 06:09 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Wait, so you didn't receive mine or it didn't work? I sent it to you the other day...

    Edit: I really hope you received it because I have assignments for my english class that I sent in by email, and the first assignment already showed up as a zero for my grades.
    Last edited by JaWiB; 09-19-2004 at 06:32 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Sorry, I haven't recieved anything from you JaWiB. Please resend your entry via PM or email.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Emailed

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Sure, I'll email my entry to you.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>Sorry, I haven't recieved anything from you JaWiB. Please resend your entry via PM or email.

    Ah well I guess there's a bug with gmail then...Not sure what's going on because I can see it in my sent items. I'll try from my yahoo address.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    I've only just submitted my entry - bit slow off the blocks...
    DavT
    -----------------------------------------------

  12. #12
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, I sent in my entry.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Sent my entry.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I'm unable to update the list of entries, but Mr Wizard's entry works fine.

    EDIT: Now I'm able to update
    Last edited by Sang-drax; 09-23-2004 at 05:03 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by Sang-drax
    I'm unable to update the list of entries, but Mr Wizard's entry works fine.
    I can edit it so if you want to PM me I'll fix the list

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 01:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM

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