I try coding this sequence:
M(0) = 1,
M(1) = 1,
M(2) = 2,
M(2t) = M(t) + M(t + 1) + t (for t > 1),
M(2t + 1) = M(t - 1) + M(t) + 1 (for t >= 1)
So this is my attempt (but still wrong): I don't know how I can differentiate between 2t and 2t +1
function seq(num)
{
var num1=1;
var num2=1;var num3=2
var sum1;var sum2;
var i=0;
if (num>=1){
for (i = 0; i < num; i++) {
sum2=num1+num2+1;
num1=num2;
num2=sum2; } }
if (num>1){
for (i = 0; i < num; i++)
{
sum1=num1+num2+num;
num1=num2;
num2=sum1;
}}
return num2;
}
document.write("seq(1): "+seq(1)+"<br>");```
From what I understand you have 4 cases:
In the first case we return 1, in 2nd case we return 2 and in other cases we need to re-call the same function with different parameters (t, t + 1, t - 1)
function seq(num) {
if(num < 2) {return 1; }
if(num === 2) {return 2; }
if(num % 2 === 1) {
const t = (num - 1) / 1;
return seq(t - 1) + seq(t) + 1;
}
const t = num / 2;
return seq(t) + seq(t + 1) + t;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.