LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-02-2009, 05:21 AM   #1
yoga
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
Lightbulb linux programe for finding GCD of two numbers


i am M.Sc. electronics student.please help me in constructing linux program for G.C.D. of two numbers.And also i want logic of this program

Last edited by XavierP; 09-02-2009 at 05:53 AM. Reason: Moved to Programming
 
Old 09-02-2009, 07:20 AM   #2
hyperactive22
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
A trivial solution to find LCM is as follows:

do
{
if (a < b)
{
temp = a ;
a = b;
b = temp;
}
a = a - b ;
}while(b) ;

Here a and b are the input values to program
and the final value of a is the LCM

Then use this equation:
GCD * LCM = a * b
to find GCD.

Last edited by hyperactive22; 09-02-2009 at 07:21 AM.
 
Old 09-30-2009, 03:55 AM   #3
idgl
LQ Newbie
 
Registered: Aug 2004
Location: Johannesburg
Distribution: RH 9.0 & Fedora Core 2
Posts: 2

Rep: Reputation: 0
Euclid's algorithm to find the greatest common divisor of two
positive integers, m and n. See D. Knuth 'The Art of Computer
Programming'
, Volume 1, page 2.
(1) Divide m by n and let r be the remainder.
(2) If r equals 0 the algorithm terminate; n is the answer.
(3) Set m to n, n to r and go back to step (1).


#!/bin/bash
m=$1
n=$2
while true
do
r=$(( $m % $n )) # (1) Divide m by n and let r be the remainder.
if [ $r -eq 0 ] # (2) If r equals 0 the algorithm terminate; n is the answer.
then
echo $n
break
fi
m=$n # (3) Set m to n, n to r and go back to step (1).
n=$r
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need a linux programe for generating fibonacci series vinki Linux - Newbie 5 09-02-2009 04:21 PM
Greatest common Divisor (GCD) man5237 Programming 21 08-15-2009 06:20 AM
Why a windows programe(*.exe) can't run in a Linux system? prerp Linux - General 32 11-18-2008 07:20 PM
GCD Algo. Help dontcare Programming 2 10-16-2004 09:50 PM
on JAVA: finding perfect numbers randomx Programming 2 10-05-2003 12:36 PM


All times are GMT -5. The time now is 11:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration