Recursion 递归序列的迭代方法

Recursion 递归序列的迭代方法,recursion,discrete-mathematics,Recursion,Discrete Mathematics,递归序列a(n)=-a(n-1)+n-1将如何求解? 我尝试了前向和后向迭代,但未能得到a(n)的显式解。您的第一步应该是写出一个结果表 f(n)=x n | x ----- 0 | 7 1 | -7 (-7 + 1 - 1) 2 | 8 ( 7 + 2 - 1) 3 | -6 (-8 + 3 - 1) 4 | 9 ( 6 + 4 - 1) 5 | -5 (-9 + 5 - 1) 6 | 10 ( 5 + 6 - 1) 7 | -4

递归序列a(n)=-a(n-1)+n-1将如何求解?
我尝试了前向和后向迭代,但未能得到a(n)的显式解。

您的第一步应该是写出一个结果表

f(n)=x

n | x
-----
0 | 7
1 | -7     (-7 + 1 - 1)
2 | 8      ( 7 + 2 - 1)
3 | -6     (-8 + 3 - 1)
4 | 9      ( 6 + 4 - 1)
5 | -5     (-9 + 5 - 1)
6 | 10     ( 5 + 6 - 1)
7 | -4     (-10 + 7 - 1)
8 | 11     ( 4 + 8 - 1)
9 | -3     (-11 + 9 - 1)
你应该看到一种模式正在出现。每对解决方案
[(0,1),(2,3),(4,5),…]
的差值为14,从
(7,-7)
开始,每两个
n
点增加一个。我们可以概括如下:

f(0) = 7
f(n) = 7 + k - 14 * b
  where k is the increment value (each 1 k per 2 n)
        b is 1 when n is odd, else 0.
现在我们只需要用
n
来定义
k
b
k
不应该太难,让我们看看:

n | k
0 | 0
1 | 0
2 | 1
3 | 1
这让你想起什么了吗?那是一个有地板的二级演员

7 + (n // 2) - 14 * b
现在是
b

n | b
0 | 0
1 | 1
2 | 0
3 | 1
对我来说,这看起来像是第二版!模是除法问题的余数,是检查一个数是偶数还是奇数的好方法。我们也在寻找普通模,因为当
n
为奇数时,我们想要
b==1
,反之亦然

f(0) = 7
f(n) = 7 + (n // 2) - 14 * (n%2)
  where (//) is the floor division function
        (%) is the modulo function

现在我们可以把这些都放在一个函数中。在Go中,这是:

func f(n int) int {
    return 7 + n/2 - 14 * (n % 2)
}
在Python中,它是

def f(n):
    return 7 + n//2 - 14 * (n%2)
在哈斯克尔我们有

f :: Int -> Int
f n = 7 + n `div` 2 - 14 * (n `mod` 2)
或者,由于Haskell非常好地实现了递归,简单地说

f :: Int -> Int
f 0 = 7
f n = f (n-1) + n - 1

第一步应该是写出一个结果表

f(n)=x

n | x
-----
0 | 7
1 | -7     (-7 + 1 - 1)
2 | 8      ( 7 + 2 - 1)
3 | -6     (-8 + 3 - 1)
4 | 9      ( 6 + 4 - 1)
5 | -5     (-9 + 5 - 1)
6 | 10     ( 5 + 6 - 1)
7 | -4     (-10 + 7 - 1)
8 | 11     ( 4 + 8 - 1)
9 | -3     (-11 + 9 - 1)
你应该看到一种模式正在出现。每对解决方案
[(0,1),(2,3),(4,5),…]
的差值为14,从
(7,-7)
开始,每两个
n
点增加一个。我们可以概括如下:

f(0) = 7
f(n) = 7 + k - 14 * b
  where k is the increment value (each 1 k per 2 n)
        b is 1 when n is odd, else 0.
现在我们只需要用
n
来定义
k
b
k
不应该太难,让我们看看:

n | k
0 | 0
1 | 0
2 | 1
3 | 1
这让你想起什么了吗?那是一个有地板的二级演员

7 + (n // 2) - 14 * b
现在是
b

n | b
0 | 0
1 | 1
2 | 0
3 | 1
对我来说,这看起来像是第二版!模是除法问题的余数,是检查一个数是偶数还是奇数的好方法。我们也在寻找普通模,因为当
n
为奇数时,我们想要
b==1
,反之亦然

f(0) = 7
f(n) = 7 + (n // 2) - 14 * (n%2)
  where (//) is the floor division function
        (%) is the modulo function

现在我们可以把这些都放在一个函数中。在Go中,这是:

func f(n int) int {
    return 7 + n/2 - 14 * (n % 2)
}
在Python中,它是

def f(n):
    return 7 + n//2 - 14 * (n%2)
在哈斯克尔我们有

f :: Int -> Int
f n = 7 + n `div` 2 - 14 * (n `mod` 2)
或者,由于Haskell非常好地实现了递归,简单地说

f :: Int -> Int
f 0 = 7
f n = f (n-1) + n - 1

这是一个编程网站,不是数学网站,所以你的答案可能是代码。有没有一种语言可以用来实现这一点?我想把它最终实现成Java或python。你的基本情况是什么?现在没有什么是可以解决的。哎呀。a(0)=7。这是一个编程网站,不是数学网站,所以你的答案可能是代码。有没有一种语言可以用来实现这一点?我想把它最终实现成Java或python。你的基本情况是什么?现在没有什么是可以解决的。哎呀。a(0)=7。