<p>I'm a go beginner and I'm attempting to do a training exercise on code wars, but my code takes too long to execute all test cases. How can this be better?</p>
<pre><code>func SumEvenFibonacci (limit int) int {
var list []int
var sum int
for i:= 1; i <= limit; i++ {
if i == 1 || i == 2 || i == list[len(list) - 1] + list[len(list) - 2] {
list = append(list, i)
if i % 2 == 0 {
sum += i
}
}
}
return sum
}
</code></pre>
<hr/>**评论:**<br/><br/>albatr0s: <pre><p>You shouldn't go from 1 to limit incrementing by one, it's much better to jump the exact number. To be more clear, you should generate the sequence instead of checking if every number is a member of it.</p>
<p>Also, you don't need keep the list of all elements, just the last two.</p></pre>Corollax: <pre><p>This problem is an excellent opportunity to take advantage of two niceties in Go's syntax: flexible loops and succinct variable swaps. </p>
<p>You can actually do most of the work in a single line, like so:</p>
<pre><code>for m, n := 0, 1; n <= limit; m, n = n, m+n {
// Filtering and summing logic goes here
}
</code></pre>
<p>As a bonus, this algorithm should run in constant memory and linear time.</p></pre>barryzxb: <pre><p>You should evaluate the length of list first.</p></pre>__CAFxX: <pre><p>You can make it much faster by knowing that:</p>
<ul>
<li><p>"the sum of the first Fibonacci numbers with odd index up to F(2n−1) is the (2n)th Fibonacci number, and the sum of the first Fibonacci numbers with even index up to F(2n) is the (2n + 1)th Fibonacci number minus 1." [<a href="https://en.wikipedia.org/wiki/Fibonacci_number#Combinatorial_identities" rel="nofollow">https://en.wikipedia.org/wiki/Fibonacci_number#Combinatorial_identities</a>]</p></li>
<li><p>F(n) = round(g<sup>n</sup> /√5), where g=(1+√5)/2 [<a href="https://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding" rel="nofollow">https://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding</a>]</p></li>
</ul>
<p>Assuming n is small enough to not cause FP numerical issues, your algorithm becomes O(1) in both space and time.</p></pre>TechnologyAnimal: <pre><p>It's failing to run the last test case:</p>
<pre><code>SumEvenFibonacci
should return 10 for input 8
Test Passed
Completed in 0.0274ms
should return 60696 for input 111111
Test Passed
Completed in 0.2556ms
should return 4613732 for input 8675309
Test Passed
Completed in 19.4640ms
should return 60696 for input 96746
Test Passed
Completed in 0.2186ms
should return 82790070 for input 144100000
Test Passed
Completed in 321.9203ms
should return 82790070 for input 65140000
Test Passed
Completed in 145.7230ms
</code></pre></pre>fakeNAcsgoPlayer: <pre><p>use benchmark framework? </p>
<p>some generic tips.
- change var list []int to var list = make([]int, 0, limit/2) to avoid unnecessary allocations.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传