# Board index » delphi » HELP!!! Sqrt problem

## HELP!!! Sqrt problem

If anyone knows TP, please write me a algorithm, and code to solve this
problem:

A program using a "divide-and-average" algorithm for approximating square
roots. Accepting positive real values for variables: "Posreal", "Approx",
and "Epsilon", and approximate the square root of "Posreal" by repeatedly
replacing "Approx" by the average of "Approx" and "Posreal/Approx", until
"Approx" and "Posreal/Approx" differ in value by less than "Epsilon", where
value of "Epsilon" is small.

I know it's simple, (like I can do the math), but this problem is driving me
insane! If there's a Pascal wizard out there who can help me, I'll name my
next child after you!

## Re:HELP!!! Sqrt problem

In article <7s75m9\$n7...@bgtnsc02.worldnet.att.net>,
"Joseph Miller" <joseph.p.mil...@worldnet.att.net> wrote:

##### Quote
> If anyone knows TP, please write me a algorithm, and code to solve
this
> problem:

> A program using a "divide-and-average" algorithm for approximating
square
> roots. Accepting positive real values for variables: "Posreal",
"Approx",
> and "Epsilon", and approximate the square root of "Posreal" by
repeatedly
> replacing "Approx" by the average of "Approx" and "Posreal/Approx",
until
> "Approx" and "Posreal/Approx" differ in value by less than "Epsilon",
where
> value of "Epsilon" is small.

> I know it's simple, (like I can do the math), but this problem is
driving me
> insane! If there's a Pascal wizard out there who can help me, I'll
name my
> next child after you!

repeat
until whatever

--
Robert AH Prins
prin...@willis.com

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

## Re:HELP!!! Sqrt problem

Is this some kind of a Students exercise? What is wrong with the sqrt
function?
regards Sven

##### Quote
Joseph Miller <joseph.p.mil...@worldnet.att.net> wrote in message

news:7s75m9\$n7v\$1@bgtnsc02.worldnet.att.net...
##### Quote
>If anyone knows TP, please write me a algorithm, and code to solve this
>problem:

>A program using a "divide-and-average" algorithm for approximating square
>roots. Accepting positive real values for variables: "Posreal", "Approx",
>and "Epsilon", and approximate the square root of "Posreal" by repeatedly
>replacing "Approx" by the average of "Approx" and "Posreal/Approx", until
>"Approx" and "Posreal/Approx" differ in value by less than "Epsilon", where
>value of "Epsilon" is small.

>I know it's simple, (like I can do the math), but this problem is driving
me
>insane! If there's a Pascal wizard out there who can help me, I'll name my
>next child after you!

## Re:HELP!!! Sqrt problem

Hi Joseph,

This is quite simple, it almost sounds like something you would need
to do in school. :-)

function SlowSqrt(PosReal,Approx,Epsilon:REAL):REAL;
var NewApprox:REAL;
begin
NewApprox := PosReal/Approx;
if Abs(Approx-NewApprox) < Epsilon
then SlowSqrt := NewApprox;
else SlowSqrt := SlowSqrt(PosReal,NewApprox,Epsilon);
end;

This is true the most simple recursive algorithm someone could think
of. :-)

Best regards,

Peter de Jong
supp...@kikosoft.com
www.kikosoft.com

On Tue, 21 Sep 1999 01:40:33 -0400, "Joseph Miller"

##### Quote
<joseph.p.mil...@worldnet.att.net> wrote:
>If anyone knows TP, please write me a algorithm, and code to solve this
>problem:

>A program using a "divide-and-average" algorithm for approximating square
>roots. Accepting positive real values for variables: "Posreal", "Approx",
>and "Epsilon", and approximate the square root of "Posreal" by repeatedly
>replacing "Approx" by the average of "Approx" and "Posreal/Approx", until
>"Approx" and "Posreal/Approx" differ in value by less than "Epsilon", where
>value of "Epsilon" is small.

>I know it's simple, (like I can do the math), but this problem is driving me
>insane! If there's a Pascal wizard out there who can help me, I'll name my
>next child after you!

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----

## Re:HELP!!! Sqrt problem

Let's see if you can debug it... haha... <sorry>

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----

## Re:HELP!!! Sqrt problem

##### Quote
Joseph Miller wrote in message <7s75m9\$n7...@bgtnsc02.worldnet.att.net>...
>If anyone knows TP, please write me a algorithm, and code to solve this
>problem:

>A program using a "divide-and-average" algorithm for approximating square
>roots. Accepting positive real values for variables: "Posreal", "Approx",
>and "Epsilon", and approximate the square root of "Posreal" by repeatedly
>replacing "Approx" by the average of "Approx" and "Posreal/Approx", until
>"Approx" and "Posreal/Approx" differ in value by less than "Epsilon", where
>value of "Epsilon" is small.

Yes, it is a school problem, but can not use function or procedure, etc. It
has to be done with a IF ELSE or WHILE DO statements. It is for a Pascal
class. This is like 5th program we've done, but for some reason, I just
can't figure it out.

## Re:HELP!!! Sqrt problem

Joseph Miller <mailto:joseph.p.mil...@worldnet.att.net> said:

##### Quote
>If anyone knows TP, please write me a algorithm, and code to solve this
>problem:

>A program using a "divide-and-average" algorithm for approximating square
>roots. Accepting positive real values for variables: "Posreal", "Approx",
>and "Epsilon", and approximate the square root of "Posreal" by repeatedly
>replacing "Approx" by the average of "Approx" and "Posreal/Approx", until
>"Approx" and "Posreal/Approx" differ in value by less than "Epsilon", where
>value of "Epsilon" is small.

>I know it's simple, (like I can do the math), but this problem is driving me
>insane!

OK, you can do the math. What you need to do is approach the algorithm
in a practical way. You could do this using recursion but I would
suggest that a repeat..until loop would be best for your purposes.

You'll need to declare the variables using one of the relevant types
real/single/double/extended depending on the accuracy you are looking
for in your program. If you have been given a minimum value for
"Epsilon" then that will help determine which data type to use.

Your code is going to look something like {in pseudo code}:

input variables, checking they are in an allowed range
repeat
do the math
until posreal/approx < Epsilon

For general help, the following may be useful:

Think about how you are going to do this if you had only pen and
paper available to you.

[1] What would you need to do to accomplish this and in what order.

[2] Write these steps down in the correct order and think about them.

[3] How are you going to accomplish each of these steps ?

[4] For each step go back to [1] unless you cannot simplify further.
Also check, for each step you want to simplify, if you have already
written the code you need.

[5] When you reach here then you have pseudo-code of each step. Go to [6]
Note: each 'step' will be a procedure, function or statement.

[6] Code your program based on the pen and paper outline. Go to [7]

[7] Test program and note any bugs. Go to [8] for bugs or [10] if none.

[8] Eliminate bug. With postable code, newsgroup is likely to help. Go to [9]

[9] If program works fine then go to [10] else go to [7]

[10] Your program works as expected. Hand in.

--
Pedt

## Re:HELP!!! Sqrt problem

Hi,

on Tue, 21 Sep 1999 at 05:40:33 o'clock, Joseph Miller wrote:

##### Quote
> If anyone knows TP, please write me a algorithm, and code to solve this
> problem:

I have edited your posting a bit:

##### Quote
> Accepting positive real values for variables:
>    "Posreal",
>    "Approx",
>    and "Epsilon",

> and approximate the square root of "Posreal" by
> repeatedly
>    replacing "Approx" by the average of "Approx" and "Posreal/Approx",
> until "Approx" and "Posreal/Approx" differ in value by less than "Epsilon"

It actually looks just like Pascal pseudo code! If you don't know how to
declare variables, "replace" a value or calculate a difference, ask your
teacher.

- Sebastian

--
Signature optimized for 1024x786 resolution in fullscreen mode.

## Re:HELP!!! Sqrt problem

I'm new to Pascal, and I'm new to these forums on the internet.  My
interest in Pascal is very much that of the amateur, i.e., for the love of
it ---  I'm not in school, and do this for my own enjoyment.

Notwithstanding this, I have been reading the messages, and just have to
ask:  Why is there so much sarcasm and rudeness here?  Is this the
prevailing etiquette?  Is it  just one-upmanship?  I'm not sure I would
call it a "community," where so many find it useful to insult others.

##### Quote
Robert AH Prins wrote:
> In article <7s75m9\$n7...@bgtnsc02.worldnet.att.net>,
>   "Joseph Miller" <joseph.p.mil...@worldnet.att.net> wrote:
> > If anyone knows TP, please write me a algorithm, and code to solve
> this
> > problem:

> > A program using a "divide-and-average" algorithm for approximating
> square
> > roots. Accepting positive real values for variables: "Posreal",
> "Approx",
> > and "Epsilon", and approximate the square root of "Posreal" by
> repeatedly
> > replacing "Approx" by the average of "Approx" and "Posreal/Approx",
> until
> > "Approx" and "Posreal/Approx" differ in value by less than "Epsilon",
> where
> > value of "Epsilon" is small.

> > I know it's simple, (like I can do the math), but this problem is
> driving me
> > insane! If there's a Pascal wizard out there who can help me, I'll
> name my
> > next child after you!

> repeat
> until whatever

> --
> Robert AH Prins
> prin...@willis.com

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

## Re:HELP!!! Sqrt problem

##### Quote
Jim Roberts <jsrobe...@jsroberts.com> wrote:
>Notwithstanding this, I have been reading the messages, and just have to
>ask:  Why is there so much sarcasm and rudeness here?  Is this the
>prevailing etiquette?

People don't consider it a good policy for someone to get the
group to do their homework.

Jud McCranie

## Re:HELP!!! Sqrt problem

##### Quote
Jim Roberts wrote:
> I'm new to Pascal, and I'm new to these forums on the internet.  My
> interest in Pascal is very much that of the amateur, i.e., for the love of
> it ---  I'm not in school, and do this for my own enjoyment.

> Notwithstanding this, I have been reading the messages, and just have to
> ask:  Why is there so much sarcasm and rudeness here?  Is this the
> prevailing etiquette?  Is it  just one-upmanship?  I'm not sure I would
> call it a "community," where so many find it useful to insult others.

A good question.  You, at least, "have been reading the messages", so you
probably know that the prevailing opinion of many of the Regular Posters is
that
(1) one SHOULD read the newsgroup before jumping in with a question, (2) one
has already been posted, and (3) one should not post "Please Do My Homework
For Me" requests.

When these "etiquette" rules are not followed, and especially when they
are
egregiously violated, some of us get sufficiently ticked off to fire off a
sarcastic, or
rude, response.  In most cases, we are hoping that the original poster takes
the hint
and reforms his (or her) behavior.  It is true that one could be nice, but
after seeing
the fifth "Help -- I'm Getting an Error 200 Now" message of the week, or
seeing
the fourth "Help solve this problem -- design an inventory system ..."
homework request, it is difficult for some of us to "keep our cool".

Many of us program, and LIKE to program.  We hope others will learn, and
like it, too.  However, we also know that there are few shortcuts to learning
...

Bob Schor
Pascal Enthusiast

## Re:HELP!!! Sqrt problem

It is NOT homework people! Just something I was trying to do on my own, and
it was driving me crazy. I just thought someone would help. If anyone asked
me to help with a hardware, software, homework, programming, etc, -any type
problem and i knew the answer, I would gladly help. I know most computer
programmers are stuffy, and I can always do the math with a TI calculator.
So if you (some of you, that is...) don't want to help, than !@#& you.
##### Quote
Jud McCranie wrote in message ...
>Jim Roberts <jsrobe...@jsroberts.com> wrote:

>>Notwithstanding this, I have been reading the messages, and just have to
>>ask:  Why is there so much sarcasm and rudeness here?  Is this the
>>prevailing etiquette?

>People don't consider it a good policy for someone to get the
>group to do their homework.

>Jud McCranie

## Re:HELP!!! Sqrt problem

Jim Roberts <mailto:jsrobe...@jsroberts.com> said:

Jim,

You've raised a good question and I hope you get a set of decent

##### Quote
>I'm new to Pascal, and I'm new to these forums on the internet.  My
>interest in Pascal is very much that of the amateur, i.e., for the love of
>it ---  I'm not in school, and do this for my own enjoyment.
>Notwithstanding this, I have been reading the messages, and just have to
>ask:  Why is there so much sarcasm and rudeness here?  Is this the
>prevailing etiquette?  Is it  just one-upmanship?  I'm not sure I would
>call it a "community," where so many find it useful to insult others.

In general, I would say that most of what you describe as rudeness and
sarcasm is directed at, primarily, those who have not bothered, unlike
yourself, to read previous messages (even a few days worth) before
posting:
a) One of the VFAQ questions - such as the CRT RTE200 error in T/BP7
b) Can you do my homework, I have not bothered to even try
c) Post binary material to a text only discussion group

Checking my archive quickly, I can come up with 407 requests for help in
(a) above in the last 12 months. Regulars do tend to get fed up with the
seventh question about RTE200 in a week when it is apparent the poster
has not read any of the group when a Mini-FAQ is posted weekly, now also
a Meta-FAQ posted weekly in the other half of the week. In fact I have a
post, just after I took over posting the Mini-FAQ, that had an RTE200
question as a direct reply to the Mini-FAQ !

replies, often from a few people, and that answers to questions are
subject to 'peer review' and wrong / incomplete answers are quickly
corrected / added to which help the person asking the question. You will
also find that regular posters will help those stuck on homework when
they show that they have tried and are stuck on a part of their program.

Also, discussions between regular posters are likely to be more short
and to the point than when answering questions from those new to Pascal
or the newsgroup.

Stick around, ask questions if you need, I'm sure you'll get helpful