'Trying to find the recursive function for a sequence of numbers. (Python)
| n | F(n) |
|---|---|
| 0 | 1 |
| 1 | 4 |
| 2 | 10 |
| 3 | 19 |
| 4 | 37 |
| 5 | 70 |
| ... | ... |
| 10 | 2062 |
Trying to determine the recursive function for he above sequence of numbers. Its also given that an even and an odd n are computed differently. Any help would be appreciated.
Edit: this is what I have so far:
def F(N):
if N <= 0:
return 1
elif N % 2 == 0:
return F(N-1) + (N-1) * 6
else:
return F(N-1) + (N) * 3
Solution 1:[1]
It could be this formula:
2n+1 + ?3n/2? ? 1
Here is JavaScript snippet that outputs the first 11 results:
function F(n) {
return 2**(n+1) + ((3*n)>>1) - 1;
}
for (let n = 0; n <= 10; n++) {
console.log(n, F(n));
}
Python:
def F(n):
return 2**(n+1) + ((3*n)//2) - 1
for n in range(11):
print(n, F(n))
Recurrence relationship
F0 = 1
Fn = 2Fn?1 ? 3?(n+1)/2? + 5
function F(n) {
if (n == 0) return 1;
return 2*F(n-1) - 3*((n+1)>>1) + 5;
}
for (let n = 0; n <= 10; n++) {
console.log(n, F(n));
}
Python:
def F(n):
if n == 0:
return 1
return 2*F(n-1) - 3*((n+1)//2) + 5
for n in range(11):
print(n, F(n))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
