0

Suppose I have a function gcf(x,y) that returns the greatest common factor of x and y. So, for example,

gcf(75,85) = 5

Now, I'm trying to create a function lcm(v) that takes a vector of integers and returns the least common multiple.

I know mathematically it will be

lcd(a,b) = a*b/((gcf(a,b))

But I have a vector as the argument. How do I start writing the code? Also, how do I make sure that the vector has at least two integers and no more than 100?

Peter
  • 11,500
  • 5
  • 21
  • 31
Jayden Rice
  • 301
  • 1
  • 14

2 Answers2

4

No need to reinvent the wheel. You can try the package library(pracma) and the vectorized functions gcd & Lcm like

a1 = c(75, 30)
b1 = c(85, 10)
pracma::gcd(a1,b1)
[1]  5 10
pracma::Lcm(a1,b1)
[1] 1275   30

Check View(pracma::gcd) or hit F2 in RStudio to learn from the source code. The second question is a typical if statement:

foo <- function(x, y){
  require(pracma)
  if(length(x) < 2 | length(x) > 100 ) return("Length of x must be at least 2 and not more than 100")
  if(length(y) < 2 | length(y) > 100 ) return("Length of y must be at least 2 and not more than 100")
  
  list(gcd=pracma::gcd(x,y),
       lcm=pracma::Lcm(x,y))
}
foo(a1, b1)  
$gcd
[1]  5 10

$lcm
[1] 1275   30

Edit

As you commented you want to find the least common factor of three or more numbers. Thus, you can start and play with the following lines of code e.g. for a vector of length of six. The idea is to test all combinations of length 2 and finally reduce them from left to right.

a =c(21, 45, 3 , 90, 72, 99)
combn(a, 2, simplify = F) %>% 
  map(~gcd(.[1], .[2])) %>% 
  Reduce(function(x,y) gcd(x, y),.)

But without any guarantee for correctness of the result.

Roman
  • 17,008
  • 3
  • 36
  • 49
  • Thank you, but I'm trying to come up with my own function. – Jayden Rice Jul 01 '20 at 09:50
  • Yes, I tried View(pracma::Lcm), but the argument should be vector. Lcm() takes two arguments but I'm trying to come up with a code that takes vector between length 2 and 100. – Jayden Rice Jul 01 '20 at 09:59
  • @JaydenRice this is not true. `Lcm()` as well as `gcd` are vectorised functions. Simply try two vectors of length 100 `pracma::gcd(sample(1:100, replace = T),sample(1:100, replace = T))` – Roman Jul 01 '20 at 10:24
  • You are right; `pracma::gcd(sample(1:100, replace = T),sample(1:100, replace = T))` will give 100 numbers, but what I was wondering is how I should plug a vector for lcm and find the lcm for all elements in the vector. For example, `lcm(c(20,50,75)) = 300` – Jayden Rice Jul 01 '20 at 10:31
  • Ok, now I understand.... but this is rather complex, right. If you have a vector of three I assume that this is correct: `a=20;b=50;c=75 gcd(a,b,c)=gcd(gcd(a,b),c)=gcd(a,gcd(b,c))=gcd(gcd(a,c),b)`. If there are four or even more numbers, this is even more complex, at least for me....and I'm out since I'm not an math expert :) – Roman Jul 01 '20 at 10:45
1

Package numbers contains number-theoretic functions, and function mLCM() therein does what you want (I think):

> numbers::mLCM(c(20,50,75))
[1] 300

If you want to write your own function, you may still want to take a look into this function -- the logic behind is simple.

Hans W.
  • 1,799
  • 9
  • 16