Post Reply 
Numbers of Armstong WP34s
12-01-2015, 10:11 AM
Post: #5
RE: Numbers of Armstong WP34s
(12-01-2015 06:35 AM)ggauny@live.fr Wrote:  also the program of Thomas for 42s, but I am a bit with difficulty to apprehend Python approach.

You can just replace the exponent 3 by 4: Click run
Code:
for a in range(10):
    p = a
    t = a**4
    for b in range(10):
        q = 10*p + b
        u = t + b**4
        for c in range(10):
            r = 10*q + c
            v = u + c**4
            for d in range(10):
                s = 10*r + d
                w = v + d**4
                if s == w:
                    print s

Result:
0
1
1634
8208
9474
=> None


Cheers
Thomas

PS: If you want to go further with n you might extract that loop into a function that can be called recursively: Click run
Code:
def armstrong(n, i, a, b):
    if i == 0:
        if a == b:
            print a
    else:
        for d in range(10):
            armstrong(n, i-1, 10*a + d, b + d**n)
            
for n in range (1, 7):
    print "n = %d" % n
    armstrong(n, n, 0, 0)
    print

Result:
n = 1
0
1
2
3
4
5
6
7
8
9

n = 2
0
1

n = 3
0
1
153
370
371
407

n = 4
0
1
1634
8208
9474

n = 5
0
1
4150
4151
54748
92727
93084

n = 6
0
1
548834

=> None
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Numbers of Armstong WP34s - ggauny@live.fr - 11-30-2015, 07:02 PM
RE: Numbers of Armstong WP34s - Thomas Klemm - 12-01-2015 10:11 AM



User(s) browsing this thread: 1 Guest(s)