应该返回列表时返回none? [英] returning none when it should be returning a list?

查看:69
本文介绍了应该返回列表时返回none?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有另一个问题,我觉得我必须要错过

的东西..基本上,我写了一个递归函数来找到所有

主要是一个数字(lim)..这里是功能:


该功能基本上包含所有找到的素数列表,

it接下来要测试的下一个数字(下一个)以及它将为b $ b上限的限制。如果接下来

不大于lim,它会在下一个素数列表之间划分,如果不是这样的话,那么它的所有时间都是不均衡的。结果等于素数的长度,然后我们

有另一个素数,泡沫冲洗并重复:)


def findPrime(seed,next,lim ):

print(" seed:" + str(seed)+" next:" + str(next)+" lim:" +

str(lim))

打印(种子)

if(next> = lim):

print(seed)

返回种子

其他:

count = 0;

种子数:

modu = math.modf(float(next)/ float(num));

if(modu [0]> 0):

count + = 1

if(count == len(seed)):

seed.append(next)

findPrime(seed,next + 2,lim)

其他:

findPrime(种子,下一个+ 2,lim)


正如你可能看到的那样,我打印过种子的价值直到

指向我返回种子的地方,但是,功能仍然返回

无..

解决方案

2006年5月1日下午2:52, ra ******** @ gmail。 com 写道:

你好,我有另一个问题我觉得我必须要失踪
一些东西..基本上,我写了一个递归函数来找到所有<主要是一个数字(lim)..这里是功能:

该功能基本上包含所有找到的素数列表,
它需要下一个数字为(下一个)测试和它将达到的极限。如果接下来
不大于lim,则接下来除以前的素数列表,如果结果等于素数的长度,则计算所有不均匀分配的时间,然后我们有另一个素数,泡沫冲洗并重复:)

def findPrime(seed,next,lim):
print(" seed:" + str (种子)+" next:" + str(next)+" lim:" +
str(lim))
print(seed)
if(next> = lim):
打印(种子)
返回种子
否则:
count = 0;
用于种子中的num:
modu = math.modf( float(next)/ float(num));
if(modu [0]> 0):
count + = 1
if(count == len(seed)):
seed.append(下)
findPrime(种子,下一个+ 2,林)
否则:
findPrime(种子,下一个+ 2,林)
正如你可能看到的那样,我已经打印了这个价值种子直到我返回种子的地方,然而,功能仍然返回
无..




那个'是因为它落到了尽头该函数导致它返回无
。然而,这不是你唯一的问题。主要的其他

问题正在更新种子原地。


请考虑以下事项:


=== file:pseed.py ===

def findPrime(seed,next,lim):

print" seed:%r,next:%d,lim:%d" %(seed,next,lim)

如果下一个> = lim:

返回种子

为种子中的num:

#modu = math.modf(float(next)/ float(num));

#尝试整数算术:

if num> 2而不是(下一个%num):

#" next"不是素数

返回findPrime(种子,下一个+ 2,lim)

返回findPrime(种子+ [下一个],下一个+ 2,lim)


#results:

"""

pseed.findPrime([1,2],3,30)
seed:[1,2],next:3,lim:30

seed:[1,2,3] ,下一个:5,林:30

种子:[1,2,3,5],下一个:7,林:30

种子:[1,2, 3,5,7],下一个:9,林:30

种子:[1,2,3,5,7],下一个:11,林:30

种子:[1,2,3,5,7,11],下一个:13,林:30

种子:[1,2,3,5,7,11,13],下一个:15,林:30

种子:[1,2,3,5,7,11,13],下一个:17,林:30

种子: [1,2,3,5,7,11,13,17],下一个:19,林:30

种子:[1,2,3,5,7,11,13, 17,19],下一个:21,林:30

种子:[1,2,3,5,7,11,13,17,19],下一个:23,林:30

种子:[1,2,3,5,7,11,13,17,19,23],下一个:25,林:30

种子:[1,2,3,5,7,11,13,17,19,23],下一个:27,林:30

种子:[1,2,3,5 ,7,11,13,17,19,23],下一个:29,林:30

种子:[1,2,3,5,7,11,13,17,19, 23,29],下一个:31,林:30

[1,2,3,5,7,11,13,17,19,23,29]
"" ;"


#Doh!看起来像递归没有必要。谷歌'消除尾巴

递归'':-)

def findPrime2(lim):

seed = [1, 2]
$ x $ b for next in xrange(3,lim,2):

prime = True

for seed in seed:

if num> 2而不是(下一个%num):

prime = False

break

如果素数:

种子。追加(下一个)

返回种子


#results:

""" pseed.findPrime2(30)
[1,2,3,5,7,11,13,17,19,23,29]



"""


ra *** *****@gmail.com 写道:

该函数基本上包含了所有找到的素数的列表,
它需要下一个要测试的数字(下一个)和它将达到的极限。如果接下来
不大于lim,则接下来除以前的素数列表,如果结果等于素数的长度,则计算所有不均匀分配的时间,然后我们有另一个素数,泡沫冲洗并重复:)




您上面描述的基本算法听起来可行,但可以制作

更高效(我没有密切关注你的代码是否有错误)。我甚至不会接近复杂的数论算法。


1.整数mod将比浮点mod更快(除非你''得到的数字太大了,无法像int一样有效地存储,但是在你接近这个代码的那一点之前,你可能会遇到其他限制。)


2.你可以在找到零余数时立即停止,不需要保持

测试列表中其余的素数。


3.您可以在中途停止测试数字。没有素数

小于2,所以没有数字x可以有一个大于x / 2的因子。


4.实际上你甚至可以做到更好。一个简单的矛盾证据表明,如果素数1..y不能除x和y ^ 2> x然后x必须是素数。换句话说另一种方式,一旦你测试到x的平方根,就没有意义了继续。如果一个因子大于sqrt(x),则另一个因子将小于sqrt(x)。
。但是你已经测试了比bt(x)更小的因素
,所以不可能有任何因素。


5.你可以比检查每个奇数(下一个+ 2)更好找到

下一个素数,但我现在太累了,无法调查它。它可能需要

更复杂的机器。


定位质数是一个有趣的挑战,因为看似无穷无尽的
等级的改进简单的蛮力搜索。就像我说的那样,

如果速度和尺寸都不是问题,你的算法很好。


Edward Elliott写道:

[响应一些素数码]

5.你可以做的比检查每个奇数(下一个+ 2)更好找到
下一个素数,但我现在太累了,无法调查它。它可能需要更复杂的机器。




你只需要检查最高为sqrt(n)的素数。如果你按顺序计算素数,这很容易。否则,你可以通过考虑6k±1形式的奇数来取消三分之一的奇数除数(因为6k±3可以被3整除)。 br $>

def potential_primes():

收益率2

收益率3

i = 6

而True:

收益率i - 1

收益率i + 1

i + = 6


hello, i have another problem i feel that i have to be missing
something.. Basically, i''ve written a recursive function to find all
the prime up to a number (lim).. here is the function:

The function basically takes in a list of all the prime number found,
it takes the next number to be tested for (next) and the limit it will
go up to. It divide next by the list of previous prime numbers if next
is not bigger than lim, count up all the times where it''s not evenly
divdided, if the result is equal the length of prime number, then we
have another prime number, lather rinse and repeat :)

def findPrime(seed, next, lim):
print("seed: " + str(seed) + " next: " + str(next) + " lim: " +
str(lim))
print(seed)
if(next >= lim):
print(seed)
return seed
else:
count = 0;
for num in seed:
modu = math.modf(float(next)/float(num));
if(modu[0] > 0):
count += 1
if(count == len(seed)):
seed.append(next)
findPrime(seed, next+2, lim)
else:
findPrime(seed, next+2, lim)

As you can probably see, i''ve printed the value of seed up until the
point where i''m returning the seed, however, the function still returns
None..

解决方案

On 1/05/2006 2:52 PM, ra********@gmail.com wrote:

hello, i have another problem i feel that i have to be missing
something.. Basically, i''ve written a recursive function to find all
the prime up to a number (lim).. here is the function:

The function basically takes in a list of all the prime number found,
it takes the next number to be tested for (next) and the limit it will
go up to. It divide next by the list of previous prime numbers if next
is not bigger than lim, count up all the times where it''s not evenly
divdided, if the result is equal the length of prime number, then we
have another prime number, lather rinse and repeat :)

def findPrime(seed, next, lim):
print("seed: " + str(seed) + " next: " + str(next) + " lim: " +
str(lim))
print(seed)
if(next >= lim):
print(seed)
return seed
else:
count = 0;
for num in seed:
modu = math.modf(float(next)/float(num));
if(modu[0] > 0):
count += 1
if(count == len(seed)):
seed.append(next)
findPrime(seed, next+2, lim)
else:
findPrime(seed, next+2, lim)

As you can probably see, i''ve printed the value of seed up until the
point where i''m returning the seed, however, the function still returns
None..



That''s because it "falls off the end" of the function, which causes it
to return None. However that''s not your only problem. Major other
problem is updating "seed" in situ.

Consider the following:

=== file: pseed.py ===
def findPrime(seed, next, lim):
print "seed: %r, next: %d, lim: %d" % (seed, next, lim)
if next >= lim:
return seed
for num in seed:
# modu = math.modf(float(next)/float(num));
# Try integer arithmetic:
if num > 2 and not (next % num):
# "next" is not a prime number
return findPrime(seed, next+2, lim)
return findPrime(seed + [next], next+2, lim)

# results:
"""

pseed.findPrime([1, 2], 3, 30) seed: [1, 2], next: 3, lim: 30
seed: [1, 2, 3], next: 5, lim: 30
seed: [1, 2, 3, 5], next: 7, lim: 30
seed: [1, 2, 3, 5, 7], next: 9, lim: 30
seed: [1, 2, 3, 5, 7], next: 11, lim: 30
seed: [1, 2, 3, 5, 7, 11], next: 13, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13], next: 15, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13], next: 17, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17], next: 19, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19], next: 21, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19], next: 23, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23], next: 25, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23], next: 27, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23], next: 29, lim: 30
seed: [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29], next: 31, lim: 30
[1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29] """

# Doh! Looks like recursion not necessary. Google ''eliminate tail
recursion'' :-)

def findPrime2(lim):
seed = [1, 2]
for next in xrange(3, lim, 2):
prime = True
for num in seed:
if num > 2 and not (next % num):
prime = False
break
if prime:
seed.append(next)
return seed

# results:
""" pseed.findPrime2(30) [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]


"""


ra********@gmail.com wrote:

The function basically takes in a list of all the prime number found,
it takes the next number to be tested for (next) and the limit it will
go up to. It divide next by the list of previous prime numbers if next
is not bigger than lim, count up all the times where it''s not evenly
divdided, if the result is equal the length of prime number, then we
have another prime number, lather rinse and repeat :)



Your basic algorithm as described above sounds workable, but can be made
more efficient (I didn''t look closely at your code for bugs). I won''t even
go near the complicated number theory algorithms.

1. integer mod will be faster than floating point mod (unless you''re getting
into numbers too large to store efficiently as ints, but you''ll probably
run into other limitations before you get near that point in this code).

2. you can stop as soon as you find a zero remainder, no need to keep
testing the rest of the primes in the list.

3. you can stop testing numbers at the halfway point. there are no primes
smaller than 2, so no number x can have a factor larger than x/2.

4. in fact you can do even better. a simple proof by contradiction shows
that if primes 1..y don''t divide x and y^2 > x then x must be prime. put
another way, once you test up to the square root of x, there''s no point
continuing. if one factor were bigger than sqrt(x), the other would have
to be smaller than sqrt(x). but you''ve already tested the factors smaller
than sqrt(x), so there can''t be any factors.

5. you can do better than checking every odd number (next+2) to find the
next prime, but I''m too tired to look into it right now. it may require
more complex machinery.

Locating primes is an interesting challenge because of the seemingly endless
grades of refinements over simple brute-force search. Like I said though,
if speed and size aren''t concerns, your algorithm is fine.


Edward Elliott wrote:
[in reponse to some prime-number code]

5. you can do better than checking every odd number (next+2) to find the
next prime, but I''m too tired to look into it right now. it may require
more complex machinery.



You only need to check the prime numbers up to sqrt(n). If you''re
calculating primes in sequential order, this is easy. Otherwise, you
can remove a third of the odd divisors by considering only odd numbers
of the form 6k±1 (because 6k±3 is divisible by 3).

def potential_primes():
yield 2
yield 3
i = 6
while True:
yield i - 1
yield i + 1
i += 6


这篇关于应该返回列表时返回none?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆