# 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. ## 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).
• 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.

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.

2. this is outta my league man..

maybe next time

3. 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

4. Im in.

5. JaWiB, you should send in your solution.
This problem isn't trivial.

So far, I haven't recieved any (working) solutions.

6. 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.

7. Sorry, I haven't recieved anything from you JaWiB. Please resend your entry via PM or email.

8. Emailed

9. Sure, I'll email my entry to you.

10. >>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.

11. I've only just submitted my entry - bit slow off the blocks...

12. Ok, I sent in my entry.

13. Sent my entry.

14. I'm unable to update the list of entries, but Mr Wizard's entry works fine.

EDIT: Now I'm able to update

15. 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 Last