Results 1 to 3 of 3

Thread: Really cheesy square root function

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Really cheesy square root function

    Code:
    int sqrt(int root)
    {
    	int i = 0;
    	bool done = false;
    
    	while(!done)
    	{
    		i++;
    		if ((i*i) == root)
    			done = true;
    		else if (i == root)
    		{
    			cout<<"No whole root";
    			done = true;
    		}
    	}
    
    	return i;
    
    }
    This works is the square root is a whole number (25 for example), but how can I do a function that works with any number?

  2. #2
    jim mcnamara
    Guest
    Also in C++:

    Wynd - go here for about 8 excellent C algorithms

    http://www.azillionmonkeys.com/qed/sqroot.html

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    The following Thread describes how to compute square root using successive approximations. It also describes how to compute other roots (cube, fourth, et cetera) and describes the Newton Raphson method for solving other types of problems.

    http://www.vbforums.com/showthread.p...threadid=65366
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width