The main number generating the puzzle (edge ​​cases)

I solve the following problem on the coding site. This fails for some edge cases on tests (hidden tests), but I'm not sure what it is. Anyone see any problems with this?

Problem: Let be the Astring of all primes sequentially grouped together (i.e. 235711131719...). Given the index n, return a 5-digit string where the first digit has an index nin A.

eg. foo(0) => 23571andfoo(10) => 19232

Here is my code:

def gen_primes():                                                                                                                                                                                                    
    A = {}                                                                                                                                                                                                           
    i = 2                                                                                                                                                                                                            
    while True:                                                                                                                                                                                                      
        if i not in A:                                                                                                                                                                                               
            yield i                                                                                                                                                                                                  
            A[i * i] = [i]                                                                                                                                                                                           
        else:                                                                                                                                                                                                        
            for p in A[i]:                                                                                                                                                                                           
                A.setdefault(p + i, []).append(p)                                                                                                                                                                    
            del A[i]                                                                                                                                                                                                 
        i += 1                                                                                                                                                                                                       

def answer(n):                                                                                                                                                                                                       

    counter = 0                                                                                                                                                                                                      
    prime_string = ""                                                                                                                                                                                                

    for p in gen_primes():                                                                                                                                                                                           
        if (counter >= n):
            prime_string += str(p)                                                                                                                                                                                   
        counter += len(str(p))                                                                                                                                                                                       
        if len(prime_string) >= 5:                                                                                                                                                                                   
            break                                                                                                                                                                                                    

    return prime_string[:5]    
+4
source share
3 answers

I think this might break for primes with more than one digit:

, , 103. Counter 10, n 11 ( , , )

"03" "103". Counter , n, . 107.

, Counter : , , n+5 .

EDIT:

: : answer(5) answer(6). "13171". "11" .

:

def answer(n):                                                                                                                                                                                                       

    counter = 0                                                                                                                                                                                                      
    prime_string = ""                                                                                                                                                                                                

    for p in gen_primes():
        prime_string += str(p)                                                                                                                                                                                     
        if len(prime_string) >= n+5:                                                                                                                                                                                   
            break                                                                                                                                                                                                    

    return prime_string[n:n+5] 

11317  # answer(5)
13171  # answer(6)
+2

; @lhk, :

phil@haydn ~ $ python
Python 2.7.5+ (default, Sep 17 2013, 15:31:50) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def primegen(): # http://stackoverflow.com/a/20660551
...     yield 2; yield 3          # prime (!) the pump
...     ps = primegen()           # sieving primes
...     p = next(ps) and next(ps) # first sieving prime
...     D, q, c = {}, p*p, p      # initialize
...     def add(x, s):            # insert multiple/stride
...         while x in D: x += s  #   find unused multiple
...         D[x] = s              #   save multiple/stride
...     while True:               # infinite list
...         c += 2                # next odd candidate
...         if c in D:            # c is composite
...             s = D.pop(c)      #   fetch stride
...             add(c+s, s)       #   add next multiple
...         elif c < q: yield c   # c is prime; yield it
...         else: # (c == q)      # add sqrt(c) to sieve
...             add(c+p+p, p+p)   #   insert in sieve
...             p = next(ps)      #   next sieving prime
...             q = p * p         #   ... and its square
... 
>>> def primeSubstr(n): # nth character in concatenated primes
...     ps = primegen()           # sequence of prime numbers
...     i, s = 0, ''              # current index, sliding window
...     while True:               # do ... until i == n
...         if len(s) < 5:        # sliding window too small
...             s+=str(next(ps))  #   add next prime to window
...         elif i < n:           # not yet to target index
...             i, s = i+1, s[1:] #   slide window to right
...         else: return s[:5]    # return desired substring
... 
>>> primeSubstr(0)
'23571'
>>> primeSubstr(5)
'11317'
>>> primeSubstr(10)
'19232'
>>> primeSubstr(15)
'93137'
>>> primeSubstr(20)
'41434'
>>> primeSubstr(1000)
'98719'
>>> CTRL-D

.

0

itertools,

>>> from sympy import sieve
>>> from itertools import islice, chain
>>> def primeSubStr(n):
        primes=iter(sieve)
        next(primes)
        return "".join( islice( chain.from_iterable(map(str,primes)), n, n+5))

>>> primeSubStr(0)
'23571'
>>> primeSubStr(10)
'19232'
>>> primeSubStr(5)
'11317'
>>> primeSubStr(15)
'93137'
>>> primeSubStr(20)
'41434'
>>> primeSubStr(1000)
'98719'
>>> primeSubStr(2000)
'98940'
>>> 

sympy.sieve , , , .

, , "2", "3", "5", "7", "11", "13", ... "101", "103", ..., chain.from_iterable, "2", "3", "5", "7", "1","1", "1","3", ... "1","0","1", "1","0","3", ... , , islice, , ,

0

Source: https://habr.com/ru/post/1665696/


All Articles