Solved

GCD (LISP)

Posted on 2004-04-25
6
1,317 Views
Last Modified: 2012-06-27
I am new to LISP programming. Could you please give me the code for
1) finding the GCD of two given numbers
2) Finding the square and square root of a given number
0
Comment
Question by:rmvprasad
6 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 10911610
GCD
---
    int gcd(int a, int b) {
          if (a == 0)
                return b;
          else
                return gcd(b % a, a);


pass in 2 numbers.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 10911622
Complete code is this.

#include <stdio.h>
#include <math.h>

int square(int myint);
int gcd(int a, int b);

int main()
{
      printf("%d\n", square(1));
      printf("%lf\n", sqrt(9));
      printf("%d\n", gcd(10,2));

      return 1;
}

int square(int myint)
{
      return myint*myint;
}

int gcd(int a, int b)
{
      if (a == 0)
            return b;
      else
            return gcd(b % a, a);
}



hongjun
0
 

Author Comment

by:rmvprasad
ID: 10911882
I am intrested in having the code in LISP language. I am new to LISP programming. Please help me.
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10912054
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10912062
BTW,this is the C programming TA.
You should post these questions in either Programming Languages or the Programming TA.
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 40 total points
ID: 10939402
LISP, C, what's the difference? :)

(defun gcd (a b)
    (cond
      ((eq b 0) a)
      (t (gcd b (% a b)))))
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.

830 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