In: Computer Science
USE R
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. Now you need to create a function to find the smallest positive number that is evenly divisible by two numbers you input into the function. (For example, your input is 6 and 9, you need to find the smallest number which can be divided by 6, 7, 8 and 9)
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
=
# Find the GCD of the numbers x and y.
gcd = function (x, y) ifelse(x == 0, y, gcd(y %% x, x))
# Find the LCM here
lcm = function (x, y) x*y/gcd(x,y)
# Use the reduce function to do the process
# Pass the arguments start:stop to the function
print(Reduce(lcm, 1:10))
print(Reduce(lcm, 1:20))
print(Reduce(lcm, 6:9))
print(Reduce(lcm, 3:7))
=================