在 Python 中求解困难的(多项式?)方程

发布于 2025-01-04 17:09:57 字数 894 浏览 0 评论 0原文

我是编程新手(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 的整数解的条件。

这看起来可能吗?

I am new to programming (Python is my first language) but I love to design algorithms. I am currently working on a system of equations (integers) and I cannot find any references to solving my particular problem.

Let me explain.

I have an equation (a test, if you will):

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

where a is some constant.

My problem is, the variable z counts in a manner very similar to a Fibonacci sequence, and the variable x is the step of z. So what I mean by this (for a Fibonacci sequence) is that at the first term of the z sequence, x = 0, and at the second term of the z sequence, x = 1. I need to solve for y.

The exact process for determining z is as follows

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)

I need to scan through (skip) the values of z < x to test for the condition of a whole-number solution for y.

Does this seem possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2025-01-11 17:09:57

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

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

It seems your best approach would be to recast the given equation as a recurrence relation and then either define a recursive function to determine the values you desire to compute or find the closed form solution to the relation. For more information on recurrence relations see:

Finally, in my experience, such problems are best tackled with mathematical numerical analysis software such as MatLab, Octave,or Mathematica. At the very least, with these you have a platform which enables rapid deployment and testing.

泛滥成性 2025-01-11 17:09:57

我所做的就是将你的伪代码翻译成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

All I've done is translate your psuedo-code into Python. Maybe it can be of some help. Perhaps you should have a look at the Python tutorial if you haven't already.

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文