Найти - Пользователи
Полная версия: Завершение цикла
Начало » Python для новичков » Завершение цикла
1 2
Ed
chain итерирует сначала по первому итератору, потом по второму и т.д. Вам, как новичку, важнее понять разницу между списком и итератором и концепцию ленивых вычислений.
Ed
Вот вариант с генерацией простых чисел на лету и с проверкой правильности разложения:
#!/usr/bin/python
# vim: sw=4 ts=4 expandtab ai
# coding:utf8

import sys
from operator import mul

def eratosthenes(x):
'''Yields the sequence of prime numbers via the Sieve of Eratosthenes.'''
D = {} # map composite integers to primes witnessing their compositeness
q = 2 # first integer to test for primality
while q <= x:
if q not in D:
yield q # not marked composite, must be prime
D[q*q] = [q] # first multiple of q not already marked
else:
for p in D[q]: # move each witness to its next multiple
D.setdefault(p+q, []).append(p)
del D[q] # no longer need D[q], free memory
q += 1

def razl(x):
y=[]
for i in eratosthenes(x):
while not x % i and x != 1:
y.append(i)
x /= i
if x == 1:
break
return y

num = int(sys.argv[1])
res = razl(num)
print res, reduce(mul, res, 1) == num
py.user.next
Ed
#!/usr/bin/python
# vim: sw=4 ts=4 expandtab ai
# coding:utf8
это неправильно

http://www.python.org/dev/peps/pep-0263/

5. Encoding comments which don't work:

Missing “coding:” prefix:

#!/usr/local/bin/python
# latin-1
import os, sys


Encoding comment not on line 1 or 2:

#!/usr/local/bin/python
#
# -*- coding: latin-1 -*-
import os, sys
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB