相关疑难解决方法(0)

加速Python中的位串/位操作?

我使用Sieve of Eratosthenes和Python 3.1 编写了一个素数生成器.代码在ideone.com上以0.32秒正确且优雅地运行,以生成高达1,000,000的素数.

# from bitstring import BitString

def prime_numbers(limit=1000000):
    '''Prime number generator. Yields the series
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ...    
    using Sieve of Eratosthenes.
    '''
    yield 2
    sub_limit = int(limit**0.5) 
    flags = [False, False] + [True] * (limit - 2)   
#     flags = BitString(limit)
    # Step through all the odd numbers
    for i in range(3, limit, 2):       
        if flags[i] is False:
#        if flags[i] is True:
            continue …
Run Code Online (Sandbox Code Playgroud)

optimization primes bit-manipulation sieve-of-eratosthenes python-3.x

39
推荐指数
4
解决办法
1万
查看次数

第n个素数问题,需要加快一点

有简单的密码将数字翻译成系列 . ( )

为了加密这个表示的数字(0 ... 2147483647),我(我想)我需要:

  • 素数分解
  • 对于给定的p(p是Prime),p的顺序(即PrimeOrd(2) == 0,PrimeOrd(227) == 49)

一些例子

    0   .                  6    (()())
    1   ()                 7    (...())
    2   (())               8    ((.()))
    3   (.())              9    (.(()))
    4   ((()))            10    (().())
    5   (..())            11    (....())
    227 (................................................())
    2147483648    ((..........()))

我的问题源代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

static class P
{
    static List<int> _list = new List<int>();

    public static int Nth(int n)
    { …
Run Code Online (Sandbox Code Playgroud)

c# optimization performance primes prime-factoring

3
推荐指数
2
解决办法
2041
查看次数