Solved

c to lisp(square root)

Posted on 2004-04-29
3
1,109 Views
Last Modified: 2011-09-20
//This is the c program. I want you to convert it to lisp program. Please help me.

void main( void )
{
    double f, g, N;

    puts( "Square Root Program" );
    printf( "Please enter a number: " );
    scanf( "%lg", &N );
    f = N/2;            // The initial approximation.

    for (;;) {
        g = N/f;  
        if ( g*g == N ) break;
        f = (f + g) / 2;  // Next guess is average of prior pair.
    }
    printf( "The square root of %g is %g\n", N, g );
}
0
Comment
Question by:rmvprasad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10951264
Install Emacs
http://www.gnu.org/software/emacs/windows/faq2.html (if you are on windows, if you are on unix/linux I'm sure you know where to get it :)

Use it to make your program in Lisp
http://www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_toc.html
0
 
LVL 24

Accepted Solution

by:
fridom earned 40 total points
ID: 10956416
Emacs Lisp is surely a list but I have the feeling that is not the Lisp he wants to have. But I found that a nice idea and have thought, let's hack a bit ;-)

So how about:
(defun my-square (from)
  (interactive "nGive me a number: ")
  (let ((val (/ from 2.0)))
    (loop for estimate = (/ from val) then (/ from val)
      until (floating-eq (* estimate estimate) from 0.001)
      do (setf val (/ (+ val estimate) 2))
      finally return estimate)))
     
(defun floating-eq (val-1 val-2 epsilon)
  (<= (abs (- val-1 val-2)) epsilon))

(format "The sqare root of %.4f = %.4f" 2 (my-square 2))
"The sqare root of 2.0000 = 1.4142"

Well easier would be of course using the built-in functions....

Have  nice weekend
Friedrich
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10957079
Hmm was thinking about something recursive - cool to come back to Emacs fun :)
Function root: (alt-x root) enter the number to be rooted, and the max number of recursive loops

(defun approx (N f loops)
  (let ((g (/ N f)))
    (if (or (= (* g g) N)
          (< loops 1))
      g
      (approx N (/ (+ f g) 2) (- loops 1)))))

(defun root (N loops)
  (interactive "nNumber to root: \nnMax loops: ")
  (message "Root of %f approximated to %f" N (approx N 2.0 loops)))
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question