Ask Your Question
1

search for values in discrete range

asked 2020-09-23 02:29:57 +0100

brennan gravatar image

updated 2020-09-23 21:18:24 +0100

FrédéricC gravatar image

I am trying to search for solutions to a pair of equations but only using a few values. I know which values give the possible answers and I want to skip the computation time associated with checking every single value up within an interval. I am using the simple code:

for a in srange(0,200):
     for b in srange(1,a+1):
          if (a+b).is_square() and (a^2+b^2).is_square():
               print(a,b)

which gives me the solutions I want but it computes every integer solution from 0 to 200. If I know the answers, can I search for the range like

for a in range(9,21,28,36,40,84,112,133,156,160)

and then the same as above, and it will print:

28, 21

40, 9

112, 84

156,133

160,36

Any idea how to make that work? This is not homework or anything, just doing a bit of research and I'm a new. Thanks!!

edit retag flag offensive close merge delete

Comments

Then syntax is

for a in (9,21,28,36,40,84,112,133,156,160):
FrédéricC gravatar imageFrédéricC ( 2020-09-23 08:44:54 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2020-09-23 08:44:39 +0100

Emmanuel Charpentier gravatar image

Just create the list of values to be searched, and loop on that:

A=(9,21,28,36,40,84,112,133,156,160)
for a in A:
    for b in srange(1,a+1):
        if (a+b).is_square() and (a^2+b^2).is_square():
            print(a,b)

gives

28 21
40 9
112 84
156 133
160 36
edit flag offensive delete link more

Comments

Thank you so much!

brennan gravatar imagebrennan ( 2020-09-23 09:28:45 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-09-23 02:29:57 +0100

Seen: 169 times

Last updated: Sep 23 '20