简体   繁体   中英

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 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.

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.

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?

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 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

ie first loop calculates 3**2 + 4 **2 ==5 **2 ..soo on

increase the pow range to 10001 for all 10000 powers

mean I would like to check for upto i=10,000, ie: 3i+4i+5i+... does it equal (3+i)i etc...

Accounting for the way range works, and printing only when 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:

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM