len()函数或其他什么错误?

时间:2014-11-09 18:23:38

标签: python

所以我一直试图让我的程序遍历这个pke方程式,只有e更改为15次列表。我以为我已经解决了,但是在我的打印声明中,而不是len只为每个指数打印一个数字,它为每个唯一的数字打印并打印一个数字。我很难看到自己出错的地方。我的计划是这样的:

def greatest_common_divisor(a_int, b_int):
    if a_int > b_int:
        dividend = a_int
        divisor = b_int
    else: # b >= a
        dividend = b_int
        divisor = a_int
    remainder = dividend % divisor
    quotient = dividend // divisor
    while remainder != 0:
        dividend = divisor
        divisor = remainder
        remainder = dividend % divisor
        quotient = dividend // divisor
    return divisor # which is the gcd

p =5
q = 7
n = p*q
header = ["p","q","n","e","diminished","gcd","unique remainders","max remainders"]
print(header)

for e in range(12,27):
unique_remainders_list = []
for x in range(1,(n+1)):
    y = x**e % n
    diminished = (p - 1)*(q - 1)
    gcd = greatest_common_divisor(e, diminished)
    max_uni_val = n-1
    if not (y in unique_remainders_list):
        unique_remainders_list.append(y)
        print("{:>2} {:>4} {:>6} {:>4} {:>10} {:>9} {:>10} {:>16}".\
    format(p,q,n,e,diminished,gcd,len(unique_remainders_list),max_uni_val, end = ' '))

打印示例:

 ['p', 'q', 'n', 'e', 'diminished', 'gcd', 'unique remainders', 'max remainders']
   5    7     35   12         24        12          1               34
   5    7     35   12         24        12          2               34
   5    7     35   12         24        12          3               34
   5    7     35   12         24        12          4               34
   5    7     35   13         24         1          1               34
   5    7     35   13         24         1          2               34
   5    7     35   13         24         1          3               34
   5    7     35   13         24         1          4               34
   5    7     35   13         24         1          5               34
   5    7     35   13         24         1          6               34
   5    7     35   13         24         1          7               34
   5    7     35   13         24         1          8               34

所以我认为我最终遇到了e的范围问题,我只是不确定在哪里。

1 个答案:

答案 0 :(得分:0)

想出来,需要移动我的打印声明。当我想念这么小的东西时,我讨厌,但感谢hagubear的帮助。

for e in range(12,27):
 unique_remainders_list = []
 for x in range(1,(n+1)):
 y = x**e % n
 diminished = (p - 1)*(q - 1)
 gcd = greatest_common_divisor(e, diminished)
 max_uni_val = n-1
 if not (y in unique_remainders_list):
    unique_remainders_list.append(y)
 print("{:>2} {:>4} {:>6} {:>4} {:>10} {:>9} {:>10} {:>16}".\
format(p,q,n,e,diminished,gcd,len(unique_remainders_list),max_uni_val, end = ' '))