Riddler Express 2022-12-16

Riddler Integer Sequences

Can You Make It To 2023?

Ryan McShane https://ryanmcshane.com
2022-12-18
Leonardo Fibonacci [(Wikipedia Commons)](https://upload.wikimedia.org/wikipedia/commons/0/04/Fibonacci5.jpg)

Figure 1: Leonardo Fibonacci (Wikipedia Commons)

The question from fivethirtyeight.com:

The Fibonacci sequence begins with the numbers 1 and 1, with each new term in the sequence equal to the sum of the preceding two. The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on.

One can also make variations of the Fibonacci sequence by starting with a different pair of numbers. For example, the sequence that starts with 1 and 3 is 1, 3, 4, 7, 11, 18, 29, 47, 76 and so on. Generalizing further, a “tribonacci” sequence starts with three whole numbers, with each new term equal to the sum of the preceding three.

Many tribonacci sequences include the number 2023. For example, if you start with 23, 1000 and 1000, then the very next term will be 2023. Your challenge is to find starting whole numbers \(a\), \(b\) and \(c\) so that 2023 is somewhere in their tribonacci sequence, \(a \le b \le c\), and the sum \(a + b + c\) is as small as possible..

An R Function to Check Whether \(a, b\), and \(c\) Lead to 2023

trib.check = function(a, b, c){
  looping = TRUE
  check = FALSE
  while(looping){
    sumABC = a + b + c
    a = b
    b = c
    c = sumABC
    if(sumABC == 2023) check = TRUE
    if(sumABC >= 2023) looping = FALSE
  }
return(check)
}
## Function Tests
trib.check(23, 1000, 1000) ## Should be TRUE
[1] TRUE
trib.check(23, 1000, 1001) ## Should be FALSE
[1] FALSE
## Create all combinations of 1 through 10
expand.grid(list(a = 1:10, b = 1:10, c = 1:10)) %>%
  ## remove any cases which don't meet the condition a <= b <= c
  filter(b >= a, c >= b) %>% 
  rowwise() %>%
  ## find the cases where 2023 is in the sequence
  mutate(is.trib = trib.check(a = a, b = b, c = c)) %>% 
  filter(is.trib == TRUE) %>% 
  triad::kable()
a b c is.trib
1 1 6 TRUE
1 6 8 TRUE

Solution: \(a = 1, b = 1, c = 6\)

And the sequence leading to 2023: \(1, 1, 6, 8, 15, 29, 52, 96, 177, 325, 598, 1100, 2023\).