--

CoPilot does it right for Python but not C++

Here is the problem I wanted CoPilot to solve:

Represent a positive number as a sum of fibonacci numbers.

Python Solution

def fibsum (n):
"""
Return a list of fibonacci numbers that sum to n.
"""
fib = [1, 1]
while fib[-1] < n:
fib.append(fib[-1] + fib[-2])
fib.reverse()
result = []
for f in fib:
if n >= f:
result.append(f)
n -= f
return result

The C++ code is obviously wrong:

/*
Represent a positive number as a sum of fibonacci numbers.
*/
vector<int> fibSum(int n) {
vector<int> fibs;
int a = 1, b = 1;
while (n > 0) {
if (n >= b) {
fibs.push_back(b);
n -= b;
}
int c = a + b;
a = b;
b = c;
}
return fibs;
}

--

--

Joseph Mariadassou

Software developer with interest in Politics, Philosophy and Economics