Can babylonian algorithm compute the negative integer?
The question is like this
1. Make a 'guess' ( you can pick n / 2 as your initial guess )
2. Compute r = n / guess
3. Set guess = ( guess + r ) / 2
4. Go bck to step 2 for many iterations as necessary. The more that step 2 and 3 repeated, the closer ' guess' will become to the square root of n.
Write a program that inputs an integer for n, iterates through the Babylonian Algorithm until ' guess ' is within 1% of the previous guess and outputs the answer as a double
For example i input n = 2;
i make do - while
the condition is while ( ( prev_guess - guess ) / prev_guess ) > 0.01
prev_guess = 1
r = 2
guess = ( prev_guess + r ) / 2 = 1.5
Condition = ( prev_guess - guess ) / prev_guess = -0.5
So to make it positive , i put
if ( ( (prev_guess - guess ) / prev_guess ) < 0 )
{
(( prev_guess - guess ) / prev_guess ) * -1
}
So when n is negative , it will become infinite loop
I put another if , else
if ( n >= 0)
....
else
cout << "Out of the range"
can i do that??
Babylonian AlgorithmCan babylonian algorithm compute the negative input?
Page 1 of 1
3 Replies - 8437 Views - Last Post: 26 March 2009 - 02:03 AM
Replies To: Babylonian Algorithm
#2
Re: Babylonian Algorithm
Posted 26 March 2009 - 01:45 AM
the most important things are two:
1. will the loops break ?
2. does that code gives you the expected results ?
1. will the loops break ?
2. does that code gives you the expected results ?
#3
Re: Babylonian Algorithm
Posted 26 March 2009 - 01:58 AM
Loops will be break whenever the user input negative input
it will cout << " out of the range"
and the programme is output what i expected..
i just wondering whether is there any square root negative integer....
it will cout << " out of the range"
and the programme is output what i expected..
i just wondering whether is there any square root negative integer....
#4
Re: Babylonian Algorithm
Posted 26 March 2009 - 02:03 AM
mixch_s, on 26 Mar, 2009 - 12:28 AM, said:
Can babylonian algorithm compute the negative integer?
The question is like this
1. Make a 'guess' ( you can pick n / 2 as your initial guess )
2. Compute r = n / guess
3. Set guess = ( guess + r ) / 2
4. Go bck to step 2 for many iterations as necessary. The more that step 2 and 3 repeated, the closer ' guess' will become to the square root of n.
Write a program that inputs an integer for n, iterates through the Babylonian Algorithm until ' guess ' is within 1% of the previous guess and outputs the answer as a double
For example i input n = 2;
i make do - while
the condition is while ( ( prev_guess - guess ) / prev_guess ) > 0.01
prev_guess = 1
r = 2
guess = ( prev_guess + r ) / 2 = 1.5
Condition = ( prev_guess - guess ) / prev_guess = -0.5
So to make it positive , i put
if ( ( (prev_guess - guess ) / prev_guess ) < 0 )
{
(( prev_guess - guess ) / prev_guess ) * -1
}
So when n is negative , it will become infinite loop
I put another if , else
if ( n >= 0)
....
else
cout << "Out of the range"
can i do that??
The question is like this
1. Make a 'guess' ( you can pick n / 2 as your initial guess )
2. Compute r = n / guess
3. Set guess = ( guess + r ) / 2
4. Go bck to step 2 for many iterations as necessary. The more that step 2 and 3 repeated, the closer ' guess' will become to the square root of n.
Write a program that inputs an integer for n, iterates through the Babylonian Algorithm until ' guess ' is within 1% of the previous guess and outputs the answer as a double
For example i input n = 2;
i make do - while
the condition is while ( ( prev_guess - guess ) / prev_guess ) > 0.01
prev_guess = 1
r = 2
guess = ( prev_guess + r ) / 2 = 1.5
Condition = ( prev_guess - guess ) / prev_guess = -0.5
So to make it positive , i put
if ( ( (prev_guess - guess ) / prev_guess ) < 0 )
{
(( prev_guess - guess ) / prev_guess ) * -1
}
So when n is negative , it will become infinite loop
I put another if , else
if ( n >= 0)
....
else
cout << "Out of the range"
can i do that??
Not only babylonian,any ancient square root algorithms won't give results for negative values.Since at that time there is no assumptions over complex numbers..
It is better to go for newton raphson method (also same as heron's method) of square root computing.
In our research we have proved that bakshali square root method is faster than any other methods existing today.
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|