C Board  

Go Back   C Board > Community Boards > Contests Board

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 09-17-2004, 08:02 AM   #1
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,040
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 : Tomorrow at 02:21 AM. Reason: Time travelling

Last edited by Sang-drax; 09-27-2004 at 04:42 AM.
Sang-drax is offline  
Old 09-17-2004, 02:07 PM   #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
The Brain is offline  
Old 09-17-2004, 08:24 PM   #3
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
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
JaWiB is offline  
Old 09-18-2004, 12:25 AM   #4
Registered User
 
Join Date: Aug 2003
Posts: 782
Im in.
Shakti is offline  
Old 09-19-2004, 05:03 PM   #5
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,040
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 : Tomorrow at 02:21 AM. Reason: Time travelling

Last edited by Sang-drax; 09-19-2004 at 05:09 PM.
Sang-drax is offline  
Old 09-19-2004, 05:26 PM   #6
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
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.
__________________
"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

Last edited by JaWiB; 09-19-2004 at 05:32 PM.
JaWiB is offline  
Old 09-20-2004, 12:20 PM   #7
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,040
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
Sang-drax is offline  
Old 09-20-2004, 12:51 PM   #8
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,215
Emailed
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline  
Old 09-20-2004, 02:55 PM   #9
Registered User
 
Join Date: Aug 2003
Posts: 470
Sure, I'll email my entry to you.
okinrus is offline  
Old 09-20-2004, 04:07 PM   #10
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
>>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
JaWiB is offline  
Old 09-21-2004, 09:05 AM   #11
Registered User
 
Join Date: Mar 2003
Posts: 143
I've only just submitted my entry - bit slow off the blocks...
__________________
DavT
-----------------------------------------------
DavT is offline  
Old 09-21-2004, 02:17 PM   #12
Cheesy Poofs!
 
PJYelton's Avatar
 
Join Date: Sep 2002
Location: Boulder
Posts: 1,728
Ok, I sent in my entry.
PJYelton is offline  
Old 09-21-2004, 07:25 PM   #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
MrWizard is offline  
Old 09-22-2004, 09:03 AM   #14
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,040
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 : Tomorrow at 02:21 AM. Reason: Time travelling

Last edited by Sang-drax; 09-23-2004 at 04:03 AM.
Sang-drax is offline  
Old 09-22-2004, 10:02 AM   #15
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,215
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
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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