473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

recursion square root

59
how to test if a num is a square root?
ex: 9 is square root
3 is not square root
/////////////////////////////////////////////
Expand|Select|Wrap|Line Numbers
  1. x = 0; //always start at 0
  2. y = 101010;  //is this a square root?
  3. square(x, y){
  4.      if((x*x) >= y){       //not a square root
  5.          return false;
  6.      }
  7.      else if((x*x) == y)    //it is a square root
  8.          return true;
  9.      }
  10.      else{                 //inc x by 1
  11.         square(x+1, y);
  12.      }
  13. }
this algorithm works fine when y is small int. ex 4, 10, 20.
but when i put y to be large ex 10000. than i get it to problem.
this is bc x start out with 0 and slowing get inc by 1. this takes alot of time.

is there better way to do this by using recursion? may be there is some formula that i dont know about.
Sep 14 '13 #1
3 4522
Luuk
1,047 Expert 1GB
change the increment part.....

start with increments of i.e. 100
if x*x>y (not x*x>=y), than do :
x=x-100, and change increments to 10

or, if google, and find this
Sep 14 '13 #2
game2d
59
i am not sure how to do this part: x=x-100, and change increments to 10

Expand|Select|Wrap|Line Numbers
  1. x = 100; //always start at 0
  2. y = 101010;  //is this a square root?
  3. square(x, y){
  4.      if((x*x) > y){       //not a square root
  5.          return false;
  6.      }
  7.      else if((x*x) == y)    //it is a square root
  8.          return true;
  9.      }
  10.      else{                 //inc x by 1
  11.         square(x+1, y);
  12.      }
  13. }
Sep 14 '13 #3
Nepomuk
3,112 Expert 2GB
Well, there are a few things you could do. Personally, I'd have a look at the so called Babylonian method for computing square roots; it's quite simple and relatively easy to implement in a recursive manner. Also, it should be much faster than your current method.
Sep 14 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
2
by: Protoman | last post by:
Can you help me? For 4, my square root funct gives 4 instead of 2; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T> T Abs(T Nbr) {
2
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
32
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
10
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever...
4
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.