Question about loops in python

I want to deflate Pythagorean triplets (code below), and I want to calculate endlessly, how can I do this without using three loops for a loop? Can I use a for loop in some way? thanks.

import math

def main():
    for x in range (10000, 1000):
        for y in range (10000, 1000):
            for z in range(10000, 1000):
                if x*x == y*y + z*z:
                    print y, z, x
                    print '-'*50

if __name__ == '__main__':
    main()  
+3
source share
12 answers

Generally, you cannot. Three variables, three loops.

But this is a special case, as no one noted . You can solve this problem with two cycles.

In addition, there is no point in checking y, z and z, y.

Oh and range(10000, 1000) = [].

import math

for x in range(1, 1000):
  for y in range(x, 1000):
      z = math.sqrt(x**2 + y**2)
      if int(z) == z:
        print x, y, int(z)
        print '-'*50
+8
source

- , math.sqrt(x*x+y*y) . , .

Python, , range(10000, 1000) - ? , , y x , - , .

edit: - , , , , Python.

+4

:

MIN = 10000
MAX = 10010
a = [MIN, MIN, MIN]
while True:
    print a
    for i in range(len(a)):
        a[i] = a[i] + 1
        if a[i] < MAX:
            break
        a[i] = MIN
        i += 1
    else:
        break

print a, Pythagorean. .

, , .

+4

xrange range , .

+2

, , , . (x, y) N, N.

import math
import itertools

def all_int_pairs():
    "generate all pairs of positive integers"
    for n in itertools.count(1):
        for x in xrange(1,n/2+1):
            yield x,n-x

for x,y in all_int_pairs():
    z = math.sqrt(x**2 + y**2)
    if int(z) == z:
        print x, y, int(z)
        print '-'*50
+2

(Python ), :

for x, y, z in [(x, y, z) for x in range(10000, 11000) for y in range(10000, 11000) for z in range(10000, 11000)]:
    if x*x == y*y + z*z:
        print y, z, x
        print '-'*50

, Christian Witts,

for x, y, z in ((x, y, z) for x in xrange(10000, 11000) for y in xrange(10000, 11000) for z in xrange(10000, 11000)):
    if x*x == y*y + z*z:
        print y, z, x
        print '-'*50

(, Python >= 2.4) .

, ... .

+1

, , , . , . ; , .

import math

def triplets(limit):
    for x in range(1, limit):
        for y in range(x, limit):
            z = math.sqrt(x**2 + y**2)
            if int(z) == z:
                yield x, y, int(z)

for x, y, z in triplets(10):
    print x, y, z
    print "-" * 50
+1

(. ), itertools.count, , .

import itertools

for x in itertools.count(1):
    for y in xrange(1, x):
         for z in xrange(1, y):
              if x*x == y*y + z*z:
                  print x, y, z
+1

, , . .

0

..

, , for

def inf():
   i = 0
   while True:
     yield i
     i = i + 1

for i in inf():
    print i  # or do whatever you want!

,

0

For infinity, at least three loops are required. To do something flexible, it takes a ton of cycles. This example is a solution to Project Euler Problem 9 or more.

#!/usr/bin/env python

def fcount(start=1):
    n = float(start)
    while True:
        yield n
        n += 1

def find_triples():
    for c in fcount():
        for b in fcount():
            if b > c:
                break
            for a in fcount():
                if a > b:
                    break
                if a ** 2 + b ** 2 == c ** 2:
                    yield (a, b, c)

def triples_by_sum(targetsum):
    for a, b, c in find_triples():
        if a + b + c == targetsum:
            yield a, b, c
        if c > targetsum:
            break

if __name__ == '__main__':
    # Finds multiple triples
    for a, b, c in triples_by_sum(252):
        print a, b, c
    # Finds single triples
    a, b, c = triples_by_sum(1000).next()
    print a, b, c
    # Goes forever
    for a, b, c in find_triples():
        print a, b, c
0
source

How to use itertools.product instead?

# range is [10000, 1000)
for x, y, z in itertools.product(range(10000, 1000, -1), repeat=3): 
    if x * x == y * y + z * z:
        print(y, z, x)

with litter bit optimization:

for x, y, z in itertools.product(range(10000, 1000, -1), repeat=3):
    if y >= x or z >= x or x >= (y + z) or z < y:
        continue
    if x * x == y * y + z * z:
        print(y, z, x)

EDIT: Here I just give the opportunity to use productinstead of a few for the loop. And you can find a more efficient method in the above posts.

0
source

Source: https://habr.com/ru/post/1703845/


All Articles