TypeError:&:'sage.rings.rational.Rational'和'int'的不支持的操作数类型

时间:2018-04-24 11:45:48

标签: python encryption cryptography sage

因此,对于我的密码学课程,我们获得了一个作业,在问题1中,我们必须在Solovay-Strassen Primality Test的其余部分中编写,并且我写下了这些内容:

#group {
    stroke-width: 3;
    stroke: #808080;
    cursor: -webkit-grab;
    cursor: grab;
}

.active {
    stroke: #ff0000;
    cursor: -webkit-grabbing;
    cursor: grabbing;
}

现在请注意,这是在线sage中编写的,但是当我运行代码时会弹出这个错误消息

<div class="svgContainer">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 776.63 680.09">
    <g id="group" transform="translate(0, 0)">
        <rect width="100" height="50" style="fill:rgb(40, 40, 109);" />
        <rect width="100" height="50" y="50" style="fill:rgb(63, 149, 75);" />
    </g>
</svg>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>

看起来它指的是错误似乎发生的这一行

def SolovayStrassen(n,k):
    for i in [1..k]:
        a = randint(2,n-1) #picks a random number between 2 and n-1
        j = jacobi_symbol(a,n) #computes jacobi function
        p = power_mod(a,(n-1)/2,n) #uses the power mod function 
        #now we test if both are equal to find if both are equal in order to check if the number is "composite" or "probably prime"
        if (j != p):
            return False #"composite"
    return True #"probably prime"

我认为错误可能是它试图根据power_mod函数将有理数转换为整数?

1 个答案:

答案 0 :(得分:1)

您需要使用//运算符进行整数除法。如果分子不能被分母整除,/运算符将在Sagemath中生成一个有理数。我怀疑你的算法不应该在n的偶数值上尝试,这是这个问题可以表现出来的唯一方法。即使n通常被视为一种特殊情况,因为它们的素性很容易确定。