[英]Programming a number theory programme
I have this thing that I want to programme which goes as follows. 我要编程的东西如下。
We all know the next two identities: 我们都知道接下来的两个身份:
3 2 +4 2 =5 2
3 2 +4 2 = 5 2
3 3 +4 3 +5 3 =6 33 3 +4 3 +5 3 = 6 3
Now, I want to write a computer code (in mathematica,C or Python) that will check for such relations. 现在,我想编写一个计算机代码(用mathematica,C或Python),以检查这种关系。
Eg, for 3 4 +4 4 +5 4 +6 4 compare it with 7 4 and check if it equals to it or not, I mean I want to check for more cases of such a sequence of numbers as above and compare them if indeed they make a sequence as above. 例如,对于3 4 +4 4 +5 +6 4 4 7 4对照,并检查它是否等于与否,我的意思是我要检查数字如上的这种序列的更多的情况下,如果他们比较实际上,它们按上述顺序进行。
Basically I know I need here a loop and conditionals, my problem is how do I keep the numbers 3,4,5,6,... to keep being generated in the sequences? 基本上我知道我需要一个循环和条件,我的问题是如何保持数字3,4,5,6,...保持在序列中生成?
This is where I am bugged down as to how to write this code. 这就是我如何编写这段代码的地方。
I mean I would like to check for upto i=10,000, ie: 3 i +4 i +5 i +... does it equal (3+i) i etc... 我的意思是我想检查最多i = 10,000,即:3 i +4 i +5 i + ...是否等于(3 + i) i等...
I hope you understood my question. 我希望你理解我的问题。
Thanks in advance. 提前致谢。
for pow in xrange(2,5):
sum=0
for index in xrange(3,3+pow):
sum+=index ** pow
if sum==(index+1) ** pow:
print True
#output is for power 2, 3 it works
i took it for range of 2,5 我接受了2,5的范围
ie first loop calculates 3**2 + 4 **2 ==5 **2
..soo on 即第一个循环在上计算
3**2 + 4 **2 ==5 **2
..soo
increase the pow range to 10001 for all 10000 powers 将所有10000力量的战力范围增加至10001
mean I would like to check for upto i=10,000, ie: 3i+4i+5i+... does it equal (3+i)i etc...
意思是我想检查最多i = 10,000,即:3i + 4i + 5i + ...是否等于(3 + i)i等...
Accounting for the way range
works, and printing only when True
: 考虑
range
工作方式,仅在True
时打印:
limit = 10000
for pow in range(2, limit + 1):
if ((3 + pow) ** pow == sum([exp ** pow for exp in range(3, 3 + pow)])):
print pow
This tries to avoid re-doing calculations as much as possible. 这试图尽可能避免重新进行计算。
>>> def f(n):
... c = 1
... L1 = [3]
... L2 = [1]
... while (c + 3 < n):
... L2 = [L1[i] * L2[i] for i in range(c)]
... c += 1
... x = (c + 2) ** (c - 1)
... print(c, x == sum(L2))
... L1.append(c + 2)
... L2.append(x)
...
>>> f(10)
(2, False)
(3, True)
(4, True)
(5, False)
(6, False)
(7, False)
>>>
Well, in Mathematica it's quite easy: 好吧,在Mathematica中,这非常简单:
For[i = 3, i < 101, i++ ]
If[Sum[(3 + j)^i, {j, 0, i - 1}] == (3 + i)^i, Print[i]]
I am pretty much sure that in other programming languages it may run faster, though. 我非常确定它在其他编程语言中可能运行得更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.