首页 > 其他分享 >[Typescript] 62. Medium - Fibonacci Sequence

[Typescript] 62. Medium - Fibonacci Sequence

时间:2022-10-21 19:05:21浏览次数:106  
标签:Typescript Sequence U1 list number 62 Fibonacci type

Implement a generic Fibonacci<T> that takes a number T and returns its corresponding Fibonacci number.

The sequence starts: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

For example

type Result1 = Fibonacci<3> // 2
type Result2 = Fibonacci<8> // 21

 

/**
 * list[n - 1] + list[n - 2] = list[n]
 * C 记录计算到第几个了
 * U1 记录第C个斐波那契值
 * U2 记录第C + 1个斐波那契值
 */
type Fibonacci<T extends number, C extends unknown[] = [], U1 extends unknown[] = [], U2 extends unknown[] = [unknown]> = T extends C['length']
  ? U1['length']
  : Fibonacci<T, [unknown, ...C], U2, [...U1, ...U2]>

 

标签:Typescript,Sequence,U1,list,number,62,Fibonacci,type
From: https://www.cnblogs.com/Answer1215/p/16814491.html

相关文章