Solved

c to lisp(square root)

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
Problem in finding output of a program 11 104
C#, VS15, StructLayout 1 134
sameEnds challenge 3 157
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

808 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