求素数算法_素数算法

求素数算法

介绍 (Introduction)

The algorithm for calculating prime numbers is based on the idea of ​​a prime number as the movement of such numbers. The basic idea is that prime numbers starting with 5 are not static, but dynamic, and can only appear in strictly defined places (6n ± 1). Thus, each new prime number, appearing, begins to move and occupy these places, preventing new prime numbers from appearing, since they will be derivatives of another prime number. To calculate the free space for the new estimated primes, which are formed by adding 2 and 4 to the initial one (5) in turn, division with the remainder is used for all previously stored primes, and if at least once the remainder is 0, then the place is taken, otherwise it is a new prime. Thus, the algorithm shows that primes do not appear randomly, but can be calculated from the previous ones.

计算素数的算法基于素数作为此类数的运动的思想。 基本思想是,以5开头的素数不是静态的,而是动态的,并且只能出现在严格定义的位置(6n±1)。 因此,出现的每个新素数开始移动并占据这些位置,从而阻止出现新素数,因为它们将是另一个素数的派生词。 要计算新的估计质数的可用空间,该空间是通过将2和4依次加到初始一(5)上而形成的,除数除法用于所有先前存储的素数,如果至少一次余数为0 ,则该地点将被占用,否则为新的素数。 因此,该算法显示素数不是随机出现的,而是可以从先前的素数计算得出的。

背景 (Background)

More details about the nature of primes can be found in my research Prime Number Explanation. Sorry for the quality, no degree, did by hand.

有关素数性质的更多详细信息,请参见我的研究“素数解释” 。 对不起,质量,没有学位,是手工做的。

使用代码 (Using the Code)

The code can be copied and pasted into the main method, to get the desired number of primes, change the cycle limit and run it.

可以将代码复制并粘贴到main方法中,以获取所需的素数,更改循环极限并运行它。

/* The flag indicates whether free space is available for the new prime.
 */
bool free;

/* Every prime number after 5 has its own factor. For example, 5 represents
 * a number of the form 6*1-1, its factor is -1, the next, if not occupied,
 * will have a factor of +1, then again -1, and so on.
 */
int factor = -1;

/* A collection of prime numbers.
 */
var primes = new List<uint>();

/* Start with 5, as this is the first dynamic prime.
 */
primes.Add(5);

/* Estimated next prime.
 */
uint next = primes.First();

for (int i = 0; i < 5000000; i++)
{
   /* Beginning with the assumption that the place is free.
    */
   free = true;

   /* Add to the estimated next number 2 or 4, depending on the factor.
    * If the factor is -1, like 5, then the next possible number is in 2 
    * and has a factor of +1, and the next is already in 4 with a factor of -1, and so on.
    */
   next += factor < 0 ? 2U : 4U;

   /* Go through the found primes, starting with the first.
    */
   foreach(var p in primes)
   {
       /* Only get to the middle, since then the remainder will never be 0.
        */
       if (next - p < p) break;
       
       /* If such a number is found, dividing by which the remainder is 0,
        * then the current estimated prime number is derived from it, therefore, is not prime.
        */
       if (next % p == 0) { free = false; break; }
   }

   /* If there was not a single division with a remainder of 0, 
    * then the current estimated number is prime, and is added to those found.
    */
   if (free) primes.Add(next);

   /* Change the factor. If there was a minus, it will become a plus, and vice versa.
    */
   factor -= 2 * factor;
}

/* Output
 */
Console.WriteLine("The last prime: " + primes.Last());
Console.WriteLine("The count of primes: " + primes.Count());

Console.ReadKey();

The opposite way of finding primes, the more reflective the idea, but extremely inefficient. In contrast to the previous one, it moves forward on a numerical line, as if running into the future. The found prime (6n±1) is extended by calculating all the numbers that will be at 6n±1 positions. These numbers are placed in the set, so only 6n±1 numbers will be in it, other numbers do not matter, because they will always be divisible by 2 or 3. The next 6n±1 number after the largest found prime is checked for presence in the set, and if it is there, then it is composite, otherwise it is prime.

查找质数的方法相反,这种想法更具反思性,但效率极低。 与前一个相反,它在数字线上向前移动,好像在走向未来。 通过计算将在6n±1位置上的所有数字来扩展找到的素数(6n±1)。 这些数字被放置在集合中,因此其中只有6n±1个数字,其他数字无所谓,因为它们始终可以被2或3整除。将检查找到的最大质数之后的下一个6n±1数字集合中的存在,如果存在,则为复合,否则为质。

var composites = new HashSet<uint>();
var primes = new List<uint>();
int factor = -1;
uint next = 5;
int limit = 1000;

while(next < limit)
{
    /* If the next is not in the set, then it is prime,
     * and it is necessary to calculate its future positions on 6n±1.
     * Example. We have 5 (6*1-1), its next positions are 
     * 5+5*4=25 (6*4+1), 25+5*2=35 (6*6-1), 35+5*4=55 (6*9+1)and so on to the limit. 
     * The next number for 5 (6*1-1) goes 7 (6*1+1),it is not in the set, 
     * so it is added, and its future positions go to the set,
     * 7 (6*1+1), 7+7*4=35 (6*6-1), 35+7*2=49 (6*8+1) and so on.
     */
    if (! composites.Contains(next))
    {
        uint temp = next;

        /* Go to the limit.
         */
        while (temp < limit)
        {
            /* Get and add the next 6n±1 position of the prime.
             */
            temp += next * 4;
            composites.Add(temp);

            /* Get and add the next 6n±1 position of the prime.
             */
            temp += next * 2;
            composites.Add(temp);
        }

        primes.Add(next);
    }

    /* Get the next 6n±1 number (estimated prime).
     */
    next += factor < 0 ? 2U : 4U;
    factor -= 2 * factor;
}

兴趣点 (Points of Interest)

In general, it is very funny, it is like a road along which prime numbers move discretely. And the new number needs to cross this road, as it were, and do it only in a strictly designated place, and if possible immediately after the largest number, so that it does not collide with others. And as soon as this transition is found, the number occupies it, the road becomes wider, and the traffic is more intense than making the transition more difficult.

总的来说,这很有趣,就像质数沿其离散运动的道路一样。 而且,新号码必须照原样穿过这条路,并且只能在严格指定的位置进行,并且如果可能的话,应在最大号码之后立即执行,以免与其他号码发生冲突。 一旦找到此过渡,便会占用很多人,道路会变得更宽,交通量会比使过渡变得更加困难更加繁忙。

These ways are ineffective and have rather theoretical interest.

这些方法无效,并且具有相当的理论意义。

历史 (History)

  • 17th October, 2019: Initial version

    2019年10月17 :初始版本

  • 21st October, 2019: Second version (added the opposite way; omitted the efficiency question)

    2019年10月21 :第二版(以相反的方式添加;省略了效率问题)

翻译自: https://www.codeproject.com/Tips/5248604/Prime-Number-Algorithm

求素数算法

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值