最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Finding the Least Common Multiplier to Turn a Vector of Numbers Into Integers - Stack Overflow

programmeradmin0浏览0评论

I have the following vector.

Vector_1 <- c(0.1, 0.9, 0.5, (1 / 3), 0.5344)

I want to multiply each element of this vector by the same number such that each number in the resulting vector is an integer. I want this multiplier to be the lowest it possibly can be.

What is the best way to accomplish this task?

For example, the vector c(0.5, (1 / 3), (2 / 7)) could be multiplied by 42 (2 * 3 * 7) to make the vector c(21, 14, 12).

Thanks!

I have the following vector.

Vector_1 <- c(0.1, 0.9, 0.5, (1 / 3), 0.5344)

I want to multiply each element of this vector by the same number such that each number in the resulting vector is an integer. I want this multiplier to be the lowest it possibly can be.

What is the best way to accomplish this task?

For example, the vector c(0.5, (1 / 3), (2 / 7)) could be multiplied by 42 (2 * 3 * 7) to make the vector c(21, 14, 12).

Thanks!

Share Improve this question edited Mar 12 at 13:13 David Moore asked Mar 12 at 12:42 David MooreDavid Moore 1,0306 silver badges16 bronze badges 3
  • 4 This seems like a dupe question (it has been asked), but most of the dupes (e.g., stackoverflow/q/62681308/3358272) are either integer-only or the code just doesn't work. (For instance, cheapr::scm(Vector_1) returns 3006, which does not work on 0.5344.) This is an interesting effort, but it shows no research and frankly it is not a programming problem so much as a mathematical problem that can be programmed. Good luck finding your efficient solution! (BTW, it's really looking for the LCD of the imperfect fractions, not always straight-forward.) – r2evans Commented Mar 12 at 13:10
  • My close vote got deleted; closed and re-opened by someone. I do not see any programming question, the method to implement is fully missing. – Friede Commented Mar 12 at 13:27
  • 1 Sorry, that may have been me, inadvertently ... I dupe-closed with the link in the remaining comment, realized the code did not work correctly, then reversed my own dupe-close. I didn't realize it would erase previous votes, sorry about that. – r2evans Commented Mar 12 at 13:29
Add a comment  | 

2 Answers 2

Reset to default 5

Update

As pointed out by @Mark Dickinson's comment, it is found that the naive solution cannot secure the correct solution due to precision loss. To play things safe, the idea from @Tim G's solution based on MASS::fractions should be applied.

v <- c(0.5, (1 / 49), (2 / 7))

library(MASS)
d <- Reduce(
  \(x, y)  (x * y) / (\(x, y)  ifelse(!y, x, Recall(y, x %% y)))(x, y),
  read.table(text = attr(fractions(v), "fracs"), sep = "/")$V2
)

and we will obtain

> d
[1] 98

Naive Solution

Given v <- c(0.1, 0.9, 0.5, (1 / 3), 0.5344), a naive (not optimized for efficiency) solution is using repeat

d <- 1
repeat {
  if (all((v * d) %% 1 == 0)) {
    break
  }
  d <- d + 1
}

where you will obtain

> d
[1] 3750

You can also use numbers::LCM on the denominators of v obtained by as.numeric(sub(".*/(\\d+).*", "\\1", MASS::fractions(v)))

library(MASS)
library(numbers)
v <- c(0.1, 0.9, 0.5, (1/3), 0.5344)
Reduce(numbers::LCM, as.numeric(sub(".*/(\\d+).*", "\\1", MASS::fractions(v))))
[1] 3750
发布评论

评论列表(0)

  1. 暂无评论