R/closestToSquareFactors.R

Defines functions closestToSquareFactors

Documented in closestToSquareFactors

closestToSquareFactors <- function(n) {
    f = factors(n)
    d = abs(f - sqrt(n))
    j = which(d == min(d))
    sf = c(0, 0);

    if (f[j] < sqrt(n)) {
        sf[1] = f[j]
        sf[2] = n / sf[1]
    } else {
        sf[2] = f[j]
        sf[1] = n / sf[2]
    }

    return(sf)
}

Try the UNCLES package in your browser

Any scripts or data that you put into this service are public.

UNCLES documentation built on May 2, 2019, 11:11 a.m.