fermat数推素数无穷_如何在3分钟内对Fermat测试进行素数测试

fermat数推素数无穷

The Fermat test is based on a result from number theory known as Fermat’s little theorem.

Fermat检验基于称为费马小定理的数论结果。

According to Fermat’s little theorem, if n is a prime number and d is any positive integer less than n, then d raised to the nth power is congruent to d modulo n.

根据费马小定理,如果n是素数,并且d是小于n的任何正整数,则d升至n的幂等于d的n

If two numbers have the same remainder when divided by n then they are said to be congruent modulo n. d modulo n is simply the remainder of a number d when divided by n.

如果两个数字除以n的 余数相同,则称它们为n模ð 模n是简单地将一个数d的剩余部分时除以n。

For example, 34 is congruent to 16 (modulo 3) as 34 modulo 3 = 1 and 16 modulo 3 = 1.

例如,当34模3 = 1和16模3 = 1时,34等于16(模3)。

费马检验素数 (Fermat test for primality)
  1. For a given number n, pick a random positive number d such that d <; n.

    对于给定的数字n ,选择一个随机正数d ,使d < ; 。

  2. Calculate (d^n) modulo n.

    计算(d ^ n)模n

  3. d modulo n is always going to be d as we always pick d that satisfies the condition d <; n.

    ð模n为总是将d作为我们总是选择d满足条件d <;

  4. If the result of (d^n) modulo n is not equal to d, then d is certainly not prime.

    如果(d ^ n)模n的结果不等于d ,则d当然不是素数。

  5. If the result of (d^n) modulo n is equal to d, then the chances are good that n is prime.

    如果(d ^ n)模n的结果等于d ,那么n是素数的机会就很大。

  6. Pick another random d that satisfies the condition d < n and repeat the above steps.

    选择另一个满足条件d < n的随机数d ,然后重复上述步骤。

Note: The examples in this post use Swift 4.1

注意 :本文中的示例使用Swift 4.1

We need a function to calculate the exponential of a number modulo another number.

我们需要一个函数来计算另一个模的个数的指数。

We use modular exponentiation to compute values when the exponent is greater than 1 as this lets us perform computation while only dealing with numbers less than n (modulo in the above function).

当指数大于1时,我们使用模幂运算来计算值,因为这使我们能够执行计算,而只处理小于n的数字(上述函数为 )。

The Fermat test chooses at random a number d between 1 and n-1 (number — 1 in the above function) inclusive. The aim is to check whether the remainder modulo n of the nth power of d is equal to d.

Fermat测试随机选择一个介于1和n-1之间的数字d (在上述函数中, 数字-1 ),包括两端。 目的是检查d的n次幂的余数n是否等于d。

The Fermat test is run for the specified count. If a number fails the Fermat test, we are assured that it is not prime. If a number passes the Fermat test, it is not guaranteed to be prime. We try to reduce the probability of error in our primality test by running the test enough times.

针对指定计数运行Fermat测试。 如果某个数字未通过Fermat测试,则可以确定它不是素数。 如果数字通过了Fermat测试,则不能保证它是质数。 我们尝试通过运行足够多次来减少素数测试中的错误概率。

By trying more and more values of d (a random positive number between 1 and n-1), we can increase our confidence in the result.

通过尝试越来越多的d值(介于1和n-1之间的随机正数),我们可以提高对结果的信心。

翻译自: https://www.freecodecamp.org/news/how-to-run-the-fermat-test-for-primality-in-under-3-minutes-89498c08f68c/

fermat数推素数无穷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值