# 数字总和斐波那契

30

``````0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
``````

``````f(0)  = 0
f(1)  = 1
f(2)  = 1   # 0 + 1
f(3)  = 2   # 1 + 1
f(4)  = 3   # 1 + 2
f(5)  = 5   # 2 + 3
f(6)  = 8   # 3 + 5
f(7)  = 13  # 8 + 5
f(8)  = 12  # 8 + 1 + 3
f(9)  = 7   # 1 + 3 + 1 + 2
f(10) = 10  # 1 + 2 + 7
f(11) = 8   # 7 + 1 + 0
f(12) = 9   # 1 + 0 + 8
f(13) = 17  # 8 + 9
f(14) = 17  # 9 + 1 + 7
f(15) = 16  # 1 + 7 + 1 + 7
f(16) = 15  # 1 + 7 + 1 + 6
f(17) = 13  # 1 + 6 + 1 + 5
f(18) = 10  # 1 + 5 + 1 + 3
f(19) = 5   # 1 + 3 + 1 + 0
f(20) = 6   # 1 + 0 + 5
f(21) = 11  # 5 + 6
f(22) = 8   # 6 + 1 + 1
f(23) = 10  # 1 + 1 + 8
f(24) = 9   # 8 + 1 + 0
f(25) = 10  # 1 + 0 + 9
f(26) = 10  # 9 + 1 + 0
f(27) = 2   # 1 + 0 + 1 + 0
(After this point it repeats at the 3rd term, 0-indexed)
``````

24位重复模式： `[2,3,5,8,13,12,7,10,8,9,17,17,16,15,13,10,5,6,11,8,10,9,10,10]`

（也将获得100分，在选择正确答案后最终确定）

2

1

1

1
@Dennis我定义了利用重复的意思是`O(1)`。如果您的代码确实在利用重复，那么它应该在恒定的时间内运行。

1
@Dennis从技术上讲，输入上的％24将使其在27次迭代时达到上限；虽然没有意思，但绝对重要。

7

# 绿洲，5字节

``````ScS+T
``````

``````ScS+10
``````

``````ScS+   = a(n)
0 = a(0)
1  = a(1)
S      # Sum of digits on a(n-1)
c     # Compute a(n-2)
S    # Sum of digits
+   # Add together
``````

Magic Octopus Urn

28

## JavaScript（ES6），45个字节

``f=(n,x=0,y=1)=>n?f(n-1,y,(x%9||x)+(y%9||y)):x``
``<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>``

`x`并且`y`不能两者都是`9`，因为那要求以前的数字是`0`，所以他们的数字和不能超过`17`。这意味着大于的数字的数字根`9`与余数模相同`9`

6

13

# Python 2，53个字节

``f=lambda n:n>1and sum(map(int,`f(n-1)`+`f(n-2)`))or n``

``lambda n:int(('011'+'2358dc7a89hhgfda56b8a9aa'*n)[n],18)``

1

11

# JavaScript（ES6），34个字节

``````f=n=>n<2?n:~-f(--n)%9+~-f(--n)%9+2
``````

``c=[];f=n=>n<2?n:c[n]=c[n]||~-f(--n)%9+~-f(--n)%9+2``
``<input type=number value=0 min=0 step=1 oninput="O.value=f(this.value)"> <input id=O value=0 disabled>``

``````n=>"0x"+"7880136ba5867ffedb834968"[n%24]-(n<3)*9+2
``````

6

# 果冻，8字节

``````;DFS
ç¡1
``````

### 怎么运行的

``````ç¡1   Main link. No arguments. Implicit left argument: 0

1   Set the right argument to 1.
ç¡    Repeatedly execute the helper link n times – where n is an integer read from
STDIN – updating the left argument with the return value and the right
argument with the previous value of the left argument. Yield the last result.

;DFS  Helper link. Arguments: a, b

;     Concatenate; yield [a, b].
D    Decimal; convert both a and b to their base-10 digit arrays.
F   Flatten the result.
S  Compute the sum of the digits.
``````

## 替代解决方案，19个字节，恒定时间

``````;DFS
9⁵ç23Ð¡⁸ịµṠ>?2
``````

### 怎么运行的

``````9⁵ç23Ð¡⁸ịµṠ>?2  Main link. Argument: n

9               Set the return value to 9
⁵              Yield 10.
ç23Ð¡         Execute the helper link 23 times, with initial left argument 10
and initial right argument 9, updating the arguments as before.
Yield all intermediate results, returning
[10,10,2,3,5,8,13,12,7,10,8,9,17,17,16,15,13,10,5,6,11,8,10,9].
⁸ị           Extract the element at index n. Indexing is 1-based and modular.
µ          Combine all links to the left into a chain.
>?2      If n > 2, execute the chain.
Ṡ         Else, yield the sign if n.
``````

1
+1为“让我们只计算在固定时间内整个重复节”的chuzpe：d

4

# JavaScript（ES6），52 46 45字节

``````_=\$=>\$<2?\$:eval([..._(--\$)+[_(--\$)]].join`+`)
``````

# 用法

``````_=\$=>\$<2?\$:eval([..._(--\$)+[_(--\$)]].join`+`)
_(7)
``````

## 输出量

``````13
``````

# 说明

`...`操作者拆分此字符串变换成字符，然后再次转换成一个字符串数组，但现在具有`+`值之间ES。然后将此字符串解释为代码，因此计算总和，然后将其返回。

``````f=(\$,p=1,c=0)=>\$?f(\$-1,c,eval([...p+[c]].join`+`)):c
``````

1
@KritixiLithos这是递归的问题。计算序列中的第n个数字大约需要2 ^（n-2）个函数调用，这会迅速建立。
ETHproductions '02

ETHproductions

4

# 05AB1E，8个字节

``````XÎF‚¤sSO
``````

``````XÎ        # push 1,0,input
F       # input_no times do:
‚      # pair the top 2 elements of the stack
¤     # push a copy of the 2nd element to the stack
s    # swap the pair to the top of the stack
S   # convert to list of digits
O  # sum
``````

3

# CJam，22个 20字节

``````ri2,{(_(jAb\jAb+:+}j
``````

``````ri                    Read an integer from input
2,                  Push the array [0 1]
{             }j  Recursive block, let's call it j(n), using the input as n and [0 1] as base cases
(                 Decrement (n-1)
_(               Duplicate and decrement again (n-2)
jAb            Get the list digits of j(n-2)
\           Swap the top two elements
jAb        Get the list of digits of j(n-1)
+       Concatenate the lists of digits
:+     Sum the digits
``````

# CJam，42个字节

``````ri_2,1+"[2358DC7A89HHGFDA56B8A9AA]"S*~@*+=
``````

3

# Perl 6的， 41  37个字节

``{(0,1,{[+] |\$^a.comb,|\$^b.comb}...*)[\$_]}``

``{(0,1,*.comb.sum+*.comb.sum...*)[\$_]}``

``````{ # bare block lambda with implicit parameter ｢\$_｣
(

0, 1,           # first two values

# WhateverCode lambda with two parameters ( the two ｢*｣ )
*.comb.sum      # digital sum of first parameter
+
*.comb.sum      # digital sum of second parameter

...            # keep using that code object to generate new values until:

*              # never stop

)[ \$_ ]          # index into the sequence
}``````

1

smls

2

# MATL，15字节

``````lOi:"yyhFYAss]&
``````

``````lO       % Push 1, then 0. So the next generated terms will be 1, 1, 2,...
i        % Input n
:"       % Repeat that many times
yy     %   Duplicate top two elements in the stack
h      %   Concatenate into length-2 horizontal vector
FYA    %   Convert to decimal digits. Gives a 2-row matrix
ss     %   Sum of all matrix entries
]        % End
&        % Specify that next function (display) will take only 1 input
% Implicit display
``````

2

# Python 2中，54 46个字节

``f=lambda n:n>2and~-f(n-1)%9+~-f(n-2)%9+2or n>0``

2

# C，96字节

0索引。与其他一些答案类似，我正在为值的查找表建立索引，但已将其压缩为4个字节的块。我没有去研究mod 24版本，因为我认为它没有那么有趣，因为其他人已经这样做了，但让我们面对现实吧，C还是不会赢。

``````#define a(n) n<3?!!n:2+(15&"\x1\x36\xba\x58\x67\xff\xed\xb8\x34\x96\x87\x88"[(n-3)/2%12]>>n%2*4)
``````

``````#define a(n)                                                                                     // using a preprocessor macro is shorter than defining a function
n<3?!!n:                                                                            // when n is less than 3 !!n will give the series 0,1,1,1..., otherwise..
(n-3)/2%12          // condition the input to correctly index the string...
"\x1\x36\xba\x58\x67\xff\xed\xb8\x34\x96\x87\x88"                     // which has the repeating part of the series encoded into 4 bits each number
// these are encoded 2 less than what we want as all numbers in the series after the third are 2 <= a(n>2) <= 17 which conforms to 0 <= a(n>2) - 2 <= 15
>>n%2*4  // ensure the data is in the lower 4 bits by shifting it down, n%2 will give either 0 or 1, which is then multiplied by 4
15&                                                                      // mask those bits off
2+                                                                          // finally, add 2 to correct the numbers pulled from the string
``````

Albert Renshaw

2

# Japt，27 25字节

``````U<2?U:~-ß´U %9+~-ß´U %9+2
``````

ETHproductions's

2

``````a!b=sum\$read.pure<\$>([a,b]>>=show)
g=0:scanl(!)1g
(g!!)``````

2

## Mathematica，49个字节

``````If[#<2,#,Tr[Join@@IntegerDigits[#0/@{#-1,#-2}]]]&
``````

## Mathematica，79 71字节

``````If[#<3,Sign@#,(9@@LetterNumber@"JJBCEHMLGJHIQQPOMJEFKHJ")[[#~Mod~24]]]&
``````

JungHwan Min

1

# Python 2，56个字节

``````a,b=0,1
exec'a,b=b,(a%9or a)+(b%9or b);'*input()
print a``````

`(a%9or a)+(b%9or b)`实际使用起来要短于`sum(map(int(`a`+`b`)))`

1

# PowerShell，79字节

``\$b,\$c=0,1;for(\$a=\$args[0];\$a;\$a--){\$z=[char[]]"\$b\$c"-join'+'|iex;\$b=\$c;\$c=\$z}\$b``

1

## 批处理，85字节

``````@set/ax=0,y=1
@for /l %%i in (1,1,%1)do @set/az=x-x/10*9+y-y/10*9,x=y,y=z
@echo %x%
``````

1

# Pyth，20个字节

``````J,01VQ=+JssjRT>2J)@J
``````

[稍后再讲]

1

## Ruby，58个字节

``````->n{n<3?n<=>0:"9aa2358dc7a89hhgfda56b8a"[n%24].to_i(18)}
``````

1

# 堆叠，40字节

``````{!2:>[:\$digitsflatmap sum\last\,]n*last}
``````

``````{n : 2:>[:\$digitsflatmap sum\last\,]n*last}
``````

1

# 八度，148字节

``````function f = fib(n)
if (n <= 1)
f = n;
else
f = sum(int2str((fib(n - 1)))-48) + sum(int2str((fib(n - 2)))-48);
endif
endfunction
``````

Rɪᴋᴇʀ

1

``````import Numeric
import Data.Char
s i=foldr(\c i->i+digitToInt c)0\$showInt i""
d a b=a:d b(s a+s b)
f 0=0
f 1=1
f 2=1
f i=d 2 3!!fromIntegral(mod(i-3)24)
``````

`f 123456789012345678901234567890123456789012345678`例如，使用调用功能。

``````-- FibonacciDigital
-- Gerhard
-- 13 February 2017

module FibonacciDigital () where

import Numeric
import Data.Char

-- sum of digits
digitSum :: Int -> Int
digitSum i = foldr (\c i -> i + digitToInt c) 0 \$ showInt i ""

-- fibonacci digital sequence function with arbitrary starting values
fibonacciDigitals :: Int -> Int -> [Int]
fibonacciDigitals a b = a : fibonacciDigitals b (digitSum a + digitSum b)

-- index -> fibonacci digital value
f :: Integer -> Int
f 0 = 0
f 1 = 1
f 2 = 1
f i = fibonacciDigitals 2 3 !! fromIntegral (mod (i-3) 24)

-- End
``````

0

# R，90个字节

``````function(n,x=0:1){repeat`if`(n,{x=c(x,sum(scan(t=gsub('',' ',x))))[-1];n=n-1},break);x[1]}
``````

0

## Mathematica，67个字节

``r=IntegerDigits;f@0=0;f@1=1;f[x_]:=f@x=Tr@r@f[x-1]+Tr@r@f[x-2];f@#&``
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.