The idea of Project Euler question 12 is to find the smallest triangular number with a specified number of divisors(https://projecteuler.net/problem=12). As an attempt to solve this problem, I wrote the following code:
# This function finds the number of divisors of a number and returns it.
FUN <- function(x) {
i = 1
lst = integer(0)
while(i<=x)
{
if(x %% i ==0)
{
lst = c(lst, i)
}
i = i +1
}
return(lst)
}
and
n = 1
i=1
while (length(FUN(n))<500)
{
i = i + 1
n = n + i
}
This code is producing the correct answer for few smaller test cases: length(FUN(n))<4
will produce 6
, and length(FUN(n))<6
will produce 28
.
However, this simple looking code is taking over 24 hours to run (and still running) for length(FUN(n))<500
. I understand that for a number to have 500 divisors, the number is probably very big, but I am wondering why is it taking so long to run.