Solved

c to lisp(square root)

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

757 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

16 Experts available now in Live!

Get 1:1 Help Now