Solved

c to lisp(square root)

Posted on 2004-04-29
3
1,105 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now