Search code examples
rfunctionmathnumber-theory

Finding the closest pentagonal number, 𝑝𝑛 given a positive integer, S where S ≥ 1


I need to create a function in R that takes as input an integer, S ≥ 1 and returns as output the pentagonal number which is closest to S.The output of my function should be the pentagonal number 𝑝𝑛 which satisfies |𝑝𝑛−𝑠|≤|𝑝𝑚−𝑠| for all positive integers m.

However if I could get two different pentagonal numbers which happens when the integer, s is literally in the middle of them. Then it doesn't matter which one it takes (greater or lesser value) which is like when S is 17 and the pentagonal number closest to 17 is 12 and 22 so it can take either one.

Here is the following code that I have created which is used to find the pentagonal number 𝑝𝑛 for a given positive integer, n:

P_n=function(n){
x=(3*n^2-n)/2
if(n == 0){
return (0)
}else{
return(x)
}
}

After writing the code to find pn, I am now stuck with finding the closest pentagonal number for integer, s. I know that the main idea is to distinguish Pm and Pn using ceiling and floor function but I don't really know how to link it to the equation |𝑝𝑛−𝑠|≤|𝑝𝑚−𝑠|.


Solution

  • You can try the code below

    P_n <- Vectorize(function(n)   max((3 * n^2 - n) / 2, 0))
    k <- floor((1 + sqrt(1 + 24 * x)) / 6)
    (n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
    

    Example 1

    > x <- 18
    
    > k <- floor((1 + sqrt(1 + 24 * x)) / 6)
    
    > (n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
    [1] 4
    

    Example 2

    > x <- 17
    
    > k <- floor((1 + sqrt(1 + 24 * x)) / 6)
    
    > (n <- k - 1 + which.min(abs(P_n(c(k,k+1)) - x)))
    [1] 3