3

我是编程新手(Python 是我的第一语言),但我喜欢设计算法。我目前正在研究一个方程组(整数),我找不到任何参考资料来解决我的特定问题。

让我解释。

我有一个方程式(如果你愿意,可以做一个测试):

raw_input == [(90*x + a) * y] + z

其中 a 是某个常数。

我的问题是,变量 z 的计数方式与斐波那契数列非常相似,变量 x 是 z 的步长。所以我的意思是(对于斐波那契数列),在 z 序列的第一项,x = 0,在 z 序列的第二项,x = 1。我需要求解 y。

确定z的具体过程如下

where c and d are constants:
#at x = 0
temp = (c+(90*x)) * (d+(90*x))
temp/90 = z(0) 

#at x = 1
new_temp = (c+(90*x)) * (d + (90*x))

new_temp/90 = z(1)  

#for all the rest of the values of z (and x), use:

j = z(@ x=1) - z(@ x=0)
k = j + 180
l = z(@ x=1) + k
print "z(@ x=1) - z(@ x=0) = j"
print "j + 180 = k"
print "k + z(1) = l"
repeat until z > raw_input

this creates the spread of z values by the relation:
j = z(@ x=n) - z(@ x=n-1)
k = j + 180
l = k + z(@ x = n)

我需要扫描(跳过)z < x 的值以测试 y 的整数解的条件。

这看起来可能吗?

4

2 回答 2

2

似乎您最好的方法是将给定的方程重新转换为递归关系,然后定义一个递归函数来确定您想要计算的值或找到该关系的封闭形式解决方案。有关重复关系的更多信息,请参见:

最后,根据我的经验,此类问题最好使用 MatLab、Octave 或 Mathematica 等数学数值分析软件来解决。至少,有了这些,您就有了一个可以快速部署和测试的平台。

于 2012-02-12T05:23:11.560 回答
1

我所做的只是将你的伪代码翻译成 Python。也许它可以提供一些帮助。如果你还没有,也许你应该看看Python 教程。

# python 2.7

# raw_input returns string - convert to int
upper_bound = int(raw_input('Upper bound: '))

def z(x):
    'A function to calculate z from x.'
    # c and d are constants
    c = 5
    d = 2
    # integer division here
    return (c + 90*x)*(d + 90*x)/90

# the value of z_0
z0 = z_x = z(0)
# a list to hold the z values z_0, z_1, ...
# the list includes z_0 (when x = 0)
zs = [z0]

x = 1
while z_x < upper_bound:
    z_x = z(x)
    zs.append(z_x)

    j = zs[x] - zs[x - 1]
    k = j + 180
    l = zs[x] + k
    print j, k, l

    x += 1
于 2012-02-12T18:34:23.737 回答