LCM is the tricky one. One needs to know that every number greater than 1 can be represented as products of prime numbers. Once factors each number m,n into its corresponding prime factors and then by multiplying the numbers with highest order we get the LCM. I have never implemented it on C++, it would be a good idea to do it now :D
I dont like to use recursion, unless it is very much necessary. If i were to calcualte GCD between 2 numbers, I would do it somthing like this.
#define min(a, b) (a)<(b)?(a):(b)
int main ( )
int a, b, i, c, d, gcd = 1, lcm ;
cin >> a >> b ;
c = a ; d = b ;
for ( i = min(a, b) ; i > 1 ; i-- )
if ( a % i == 0 && b % i == 0 )
gcd = gcd * i ;
a = a / i ;
b = b / i ;
i = min ( i, min ( a, b ) );
lcm = c / gcd * d;
cout << gcd << lcm ;
> I dont like to use recursion, unless it is very much necessary
Don't fear recursion, it's cool! ;) Actually, Ed doesn't use recursion either unless it makes the code much shorter or simpler. It's a great tool if you know when to use it and more importantly, when not to use it.
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...
Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...