I want to find the closest number given that I have three numbers x, y and z . I want to find the closest multiple to z which is closest to x^y.
Some examples:
x <- 349
y <- 1
z <- 4
x <- 395
y <- 1
z <- 7
x <- 4
y <- -2
z <- 2
The result should look like:
4 to 349 is 348
7 to 395 is 392
2 to 1/16 is 0
We can use
f = function(x, y, z) round(x^y/z)*z
For example
f(349,1,4)
# [1] 348
f(395,1,7)
# [1] 392
f(4,-2,2)
# [1] 0
foo = function(x, y, z) {
tmp = x^y
r = tmp %% z #take modulus to find remainder
tmp - r #subtract remainder from x^y
}
foo(4, -2, 2)
#[1] 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With