Finding Large Primes in Mathematica

I am new to Mathematica, and I am trying to find large prime numbers that can be written using only the digits 0, 1, 2 and 3, and more than half of these digits should be 0. For example, 100003 is right.

I was thinking of using the Prime [n] function with a bunch if “If”, but I was wondering if there was a more efficient way around this. Thanks in advance.

+4
source share
2 answers

For a good answer, ask this question at http://mathematica.stackexchange.com

For answers, read ...

Function

myTestQ[num_Integer] := 
   And[DigitCount[num][[10]] > Plus @@ DigitCount[num][[1 ;; 3]], PrimeQ[num]]

True, , (i) 0 , 1 s, 2 s, 3 (ii) . And , , .

; , , .

, 0,1,2,3. , , , 4. :

IntegerString[Range[lo, hi, 2], 4]

, base-4 lo -th hi -th 2 ( ).

IntegerString[Range[1, 13, 2], 4]

{"1", "3", "11", "13", "21", "23", "31"}

1-, 3-, 5-,..., 13- -4 . , , ToExpression.

, -

Select[ToExpression[IntegerString[Range[lo, hi, 2], 4]], myTestQ]

lo hi , , lo - .

+4

. , . Mathematica, Python:

import random

def rand_candidate(n):
    #assumes n > 4
    while True:
        s = [random.choice(['1','2','3'])]
        for i in range(n-2):
            s.append(random.choice(['0','0','0','1','2','3']))
        s.append(random.choice(['1','3']))
        if s.count('0') > n/2: return int(''.join(s))

, , 0, , , , .

primality - Fermat. (, , ) PGP RSA. . Mathematica , , :

def prob_prime(n):
    tests = [2,3,5,7]
    return all(n % p != 0 and pow(p,n-1,n) == 1 for p in tests)

:

def go_fishing(n):
    while True:
        p = rand_candidate(n)
        if prob_prime(p): return p

:

>>> go_fishing(20)
23002001032200000101
>>> go_fishing(100)
1100200103020002000000000020232222230321210000103030031022000000200001001300020010012122020101020113

Derive ( ) .

+3

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


All Articles