C Board  

Go Back   C Board > Community Boards > Contests Board

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2007, 03:01 PM   #31
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by Sang-drax View Post
brewbuck, you must be doing something wrong. 125 iterations for newton's method?
If you implement it exactly as I did, and graph the number of iterations to achieve the accuracy I specified, that is what comes out. It is what it is.
brewbuck is offline   Reply With Quote
Old 05-10-2007, 03:02 PM   #32
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by dwks View Post
Code:
index *= 0.5;
I think that counts as a sneaky way to avoid using division . . .
I disagree. What if it had read index *= 2? Is that a "sneaky way" to avoid dividing by 0.5? It's just a multiplication. The fact that the number is less than one doesn't really mean anything.
brewbuck is offline   Reply With Quote
Old 05-11-2007, 05:58 AM   #33
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
Perhaps an even greater challenge is no division or multiplication operators

But then I suppose you'd define some macros that multiply by addition... *sigh*
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 05-11-2007, 04:29 PM   #34
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
nah, if I couldnt use multiplication, Id use bit shifting
__________________
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
abachler is offline   Reply With Quote
Old 05-12-2007, 05:28 PM   #35
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,041
Quote:
Originally Posted by brewbuck View Post
If you implement it exactly as I did, and graph the number of iterations to achieve the accuracy I specified, that is what comes out. It is what it is.
Yeah, but a correctly implemented Newton's method should ceonverge much faster than that.
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline   Reply With Quote
Old 06-07-2007, 09:00 AM   #36
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
a solution that meets all the requirements of the rules, but perhaps not the spirit of the contest -

Code:
double sqrtfunc(double Input){
 
     __asm    FLD  Input
     __asm    FSQRT
     __asm    FSTP Input
 
     return Input;
     }
hah, no division one iteration beat that
__________________
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Last edited by abachler; 06-07-2007 at 09:08 AM.
abachler is offline   Reply With Quote
Old 06-07-2007, 10:58 AM   #37
Massively Single Player
 
AverageSoftware's Avatar
 
Join Date: May 2007
Location: Buffalo, NY
Posts: 141
Quote:
Originally Posted by abachler View Post
hah, no division one iteration beat that
Alas, since templates can't take doubles, I can't quite beat that, but...

Code:
template <int r, int x>
struct SqRootCalc
{
    const static int value = x * x == r ? x : SqRootCalc<r, x - 1>::value;
};

template <int r>
struct SqRootCalc<r, 1>
{
    const static int value = -1;
};

template <int r>
struct SqRoot
{
    const static int value = SqRootCalc<r, r / 2>::value;
};

template <>
struct SqRoot<1>
{
    const static int value = 1;
};

template <>
struct SqRoot<0>
{
    const static int value = 0;
};
This will find integer square roots at compile time, so I don't even need instructions.

Usage:
Code:
cout << SqRoot<9>::value << endl;
If you pick an integer that doesn't have an integer square root (5 for example) you'll get -1.
__________________
There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.
AverageSoftware is offline   Reply With Quote
Old 06-07-2007, 02:27 PM   #38
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Lund, Sweden
Posts: 2,041
Quote:
Originally Posted by AverageSoftware View Post
This will find integer square roots at compile time, so I don't even need instructions.
Wow, amazing, except that you are using division.
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline   Reply With Quote
Old 06-07-2007, 02:32 PM   #39
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> Wow, amazing, except that you are using division.

But there are no calls to the division operator at runtime, so its close.
Daved is offline   Reply With Quote
Old 06-07-2007, 02:47 PM   #40
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Quote:
>> Wow, amazing, except that you are using division.

But there are no calls to the division operator at runtime, so its close.
Besides, you can just change it to *0.5 and you'll be just fine.

That code's pretty amazing, though. I would never have thought of it.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 06-07-2007, 02:58 PM   #41
Registered User
 
Join Date: Jan 2005
Posts: 7,252
>> Besides, you can just change it to *0.5 and you'll be just fine.
Except that you can't unfortunately. I believe everything must be integral.
Daved is offline   Reply With Quote
Old 06-07-2007, 03:31 PM   #42
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
This doesn't work?
Code:
static_cast<int>(r * .5)
I'm just guessing, I don't know much about that sort of thing.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 06-07-2007, 04:22 PM   #43
Registered User
 
Join Date: Jan 2005
Posts: 7,252
It does, but that's different than AverageSoftware's template metaprogramming based solution. Yours is a run-time operation, but the templated stuff is compile time, which I believe must be integral (which is also why double can't be used instead of int).
Daved is offline   Reply With Quote
Old 06-07-2007, 04:45 PM   #44
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Oh, I see. Well, the r/2 could be just r or r-1; it would just be less efficient and involve more "recursion".
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 06-07-2007, 05:00 PM   #45
Registered User
 
Join Date: Jan 2005
Posts: 7,252
True. And now that I look closer I see that it only does one division anyway, which is allowed in the rules.
Daved is offline   Reply With Quote
Reply

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
Square Root ?!? Tm687 C++ Programming 1 02-29-2004 04:38 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:43 PM.


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