answersLogoWhite

0


Best Answer

Dunno about Linux, but I've written mine in C.

It prime factorises the numbers, making a note of the highest power of each prime factor as it goes (in a linked list of malloc()ed structures). Once all the numbers have been factorised, it has a list of all the primes used along with their highest power. The lcm is then the product of the primes raised to their highest power.

You are also not limited to the lcm of 2 numbers - you can keep factorising numbers until you run out of them and find the lcm of them all!

Whilst you're at it you can add finding their hcf very easily: this time it's the product of the common primes to their lowest power.

All that is then needed is the prime factorisation of the numbers.

The normal method is:

  1. try the first prime (2)
  2. If it does not divide the number:
    1. set the prime to the next prime
    2. Try again from step 2
  3. Add one to the power count of this prime
  4. replace the number by the number divided by the prime
  5. if the number is not 1 go back to step 2
  6. Found all primes, stop!

Finding the primes by which to divide is not easy on the fly, so you could check 2 specifically and then all odd numbers 3, 5, 7,..., but an improvement is to specifically check 2 and 3 and then check the numbers 6n ± 1 (which may be prime and why are 6n, 6n ± 2 and 6n ± 3 definitely not prime?) which skips every third odd number - this sequence of potential primes (5, 7, 11, 13, 17, 19, ...) can be easily generated.

And while you're at it, you could display the prime factorisation you've done.

And using that prime factorisation you can list the factors (and factor pairs) for the numbers.

Obviously you'll need to sort out how the numbers are input to the program - I decode argv[], but you could use reading from stdin if you prefer.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program which calculating LCM of 2 numbers in Linux?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program that will call a function to multiply 4 numbers and return the answer to main program?

In Windows, use notepad.exe; in linux, use program xedit.


Write the program in Linux to find the reverse of any string?

i am sam


Write a program in java interface for calculating area?

interface Shape { public double getArea(); }


Write a program in Lex to eliminate white space and collect numbers as a token?

write a lex program to delete space from the program


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Shell program for gcd of three given numbers?

write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html


How do you write a program in objective c numbers 1-100 prime numbers?

fdsgfhgdfhgdf


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


Write a program to add two 8 bit numbers in microprocessor 8051?

write it in 8085


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.