# n-digit primes given the first m digits

I just discovered this site and this is my first question, I hope I'm doing it correctly.

The challenge is the following, you must write a code that prints all the prime numbers of $$\n\$$ digits. Since this problem is computationally complicated, I think that one way to simplify it is to give a number $$\m\$$ that will be the first digits of the primes to be printed and let's take $$\n\in[1,2,...,10]\$$ (there are 45086079 10-digit primes OEIS A006879). To reduce problems that the primes of large digits (10,9,8) can generate, we can say that if $$\n=10,9,8,7\$$ then $$\\#m\geq 3\$$, where $$\\#m\$$ denotes the number of digits in $$\m\$$.

EXAMPLE

input:
n=4
m=22

output:
2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297


RULES

• You can use built-in functions to generate prime numbers.
• The user must provide $$\m\$$ and $$\n\$$.
• Lowest number of bytes in each language wins.
• Welcome to Code Golf and nice first question! Id' suggest using the standard [sequence] rules (first m, mth term or all terms) instead. For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main Jan 6 at 23:41
• Thank you very much, I will take it into account. If there is any modification to my question, I would like to know. Jan 6 at 23:47
• I guess that $m\ge3$ actually means that $m$ would have at least 3 digits? Jan 7 at 0:12
• @Arnauld yes!!! Jan 7 at 1:17
• It's a bit confusing that $m$ is both used for the input as for the length of the input.
– Ivo
Jan 9 at 7:32

# Bash + bsdgames package, 39

primes 1 $[10**$2]|grep ^$1|egrep .{$2}


Try it online!

# R, 65 bytes

\(n,m,k=10^(n-nchar(m)))for(i in 0:k+m*k)sum(!i%%2:i)<2&&print(i)


Attempt This Online!

• Doesn't seem to output anything for inputs where $n$ is equal to the length of $m$ and $m$ is a prime (e.g. $n=2,m=13$ or $n=1,m=7$). Jan 7 at 22:33
• @KevinCruijssen thanks for spotting. Correction implemented at no byte cost. Jan 8 at 8:20

# Vyxal, 12 11 bytes

↵:₀/ṡ~æ'⁰øp


Try it Online!

Takes n then m. Prints the primes in order from largest to smallest.

## Explained

↵:₀/ṡ~æ'⁰øp
↵:₀/         # 10 ** n, 10 ** (n - 1)
ṡ        # range(^, ^)
~æ      # filtered to only include primes
'⁰øp  # and filtered to only keep numbers which start with m

• 11 Jan 6 at 23:45
• @emanresuA I originally had that, but I don't know if it's allowed because it technically never terminates Jan 6 at 23:48

# Vyxal, 10 bytes

Lε↵~*~+ṡ~æ


Try it Online!


↵        # 10 **
ε         # Digit count minus
L          # Length of starting digits
~*      # Multiply by starting digits (without popping)
~+    # Add to starting digits (without popping)
ṡ   # Range from ^^ to ^
~æ # Filter by isprime


# Charcoal, 28 bytes

≔Ｘχ⁻ＮＬηθＩΦ⁺…⁰θ×Ｎθ∧›ι¹⬤…²ι﹪ιλ


Try it online! Link is to verbose version of code. Takes n as the first input and m as the second. Explanation:

≔Ｘχ⁻ＮＬηθ


Subtract the number of digits in m from n and then take 10 to that power.

ＩΦ⁺…⁰θ×Ｎθ∧›ι¹⬤…²ι﹪ιλ


Take the range from 0 to that number, add on m multiplied by that number, then output only the prime numbers in that range.

# Brachylog (v2), 10 bytes

⟨{~l}a₀⟩ṗ≜


Try it online!

A generator solution that generates all possible outputs (the TIO header converts the generator to a list for printing, but consensus is that the generator on its own is sufficient to output a list). Input is a list [n,m].

This should be 8 bytes, but I had to add grouping characters {} to work around a bug in Brachylog's parser.

## Explanation

⟨{~l}a₀⟩ṗ≜
~          Checking values whose
l           length
⟨              is the first input, and
a₀        which have a prefix that is
⟩       the second input,
ṗ    using only the values that are prime numbers,
≜   generate all specific values that meet these criteria.


The { and } don't do anything but are required to prevent the parser crashing.

One interesting quirk of Brachylog: the l doesn't imply anything about us looking for numbers here, and will also try 4-element lists, but a few of the later restrictions restrict the program to looking for numbers specifically and thus it'll discount 4-element lists as a possibility.

Brachylog's primality testing algorithm is slow but general – it'll work in theory for any number of digits and any size of prefix, but may take a long time on larger numbers.

# Python, 8380 75 bytes

lambda n,m:primerange(x:=m*(y:=10**(n-len(str(m)))),x+y)
from sympy import*


Attempt This Online!

-3 bytes by changing list(x) to [*x]

-5 bytes thanks to @emanresu A

• You're allowed to return iterators, which includes sympy's prime range. Jan 9 at 4:26
• Also, 75 bytes Jan 9 at 4:27
• Welcome to Code Golf! Jan 9 at 6:04
• @RydwolfPrograms I'd expect Neil do the welcome :)
– l4m2
Jan 11 at 6:28

# Retina 0.8.2, 117 bytes

.+*#
((.)+)¶(?<-2>#)+
$1 +%1# 0$'¶$1$'¶$2$'¶$3$'¶$4$'¶$5$'¶$6$'¶$7$'¶$8$'¶$9 .+$*
A^1?$|^(11+)\1+$
%1


Try it online! Takes m as the first input and n as the second. Explanation:

.+*#


Convert n to unary.

((.)+)¶(?<-2>#)+
$1  Decrement n for each digit of m. +%1# 0$'¶$1$'¶$2$'¶$3$'¶$4$'¶$5$'¶$6$'¶$7$'¶$8$'¶$9  Extend m to be n digits long, a digit at a time. .+$*


Convert all the values to unary.

A^1?$|^(11+)\1+$


Discard 0, 1 and all composite numbers. (If n was always at least 2, the ^1?\$| would not be needed for a saving of 5 bytes.)

%1


Convert the remaining primes back to decimal.

# 05AB1E, 12 bytes

°DT÷Ÿʒp}ʒ²Å?


Try it online!

Port of lyxal's Vyxal answer. It can probably be a bit shorter, but I can't get it to work with just 1 filter.

Explanation:

°DT÷Ÿʒp}ʒ²Å?  # Implicit input, with n first then m
°             # 10 ** n
DT÷          # 10 ** (n - 1)
Ÿ         # Range between them
ʒp}      # Primes only
ʒ     # Keep only those
²    # the second input, m
# Implicit output


# Thunno, $$\ 17 \log_{256}(96) \approx \$$ 13.99 bytes

10@D10,s:gNkgz1ZV


(No ATO link since that's on an older version)

#### Explanation

10@D10,s:gNkgz1ZV  # Implicit input, with n first then m
10@                # 10 ** n
D10,            # 10 ** (n - 1)
s:          # Range between them
gNk       # Primes only
g      # Keep only those
z1    # the second input, m
# Implicit output


#### Screenshot # C (gcc) with -lgmp, 159 147 bytes

• -2 thanks to ceilingcat
• -10 by removing the #import

Takes a number length and prefix (as a string).

Luckily GMP has a "next prime number" function, so I just had to turn the prime numbers into strings and compare the prefix to the stringified values.

*t,p;f(m,w,u,v)int*w;{u=strlen(w);__gmpz_init(p);for(v=0;v<=m;(v=__gmp_asprintf(&t,"%Zd",p))==m&!strncmp(w,t,u)&&puts(t))__gmpz_nextprime(p,p);}


Try it online!

# JavaScript (V8), 78 bytes

Expects (n)(m), with m passed as a string.

n=>m=>{for(q=1;!m[n-1];q*=10)m+=0;for(;q--;m++)for(x=m;x-2||print(m),m%--x;);}


Try it online!

### Commented

n =>              // outer function taking n
m => {            // inner function taking m
for(            // initialization loop:
q = 1;        //   q = size of range, initialized to 1
!m[n - 1];    //   stop when m is large enough
q *= 10       //   at each iteration, multiply q by 10
) m += 0;       //   and append a trailing 0 to m
for(            // main outer loop:
;             //
q--;          //   stop when q = 0 / decrement it afterwards
m++           //   increment m after each iteration
)               //
for(          //   main inner loop:
x = m;      //     stat with x = m
x - 2 ||    //     if we've reached x = 2 without breaking:
print(m), //       m is prime --> print it
m % --x;    //     decrement x and stop if it's a divisor of m
);            //
}                 //


# 05AB1E, 9 bytes

°ÅP¹ùʒIÅ?


Try it online.

Explanation:

°          # Push 10 to the power of the first (implicit) input-integer n
ÅP        # Pop and push a list of all primes lower than (or equal to) that 10**n
¹ù      # Only keep the primes with a length equal to the first input n
ʒ     # Filter it further by keeping those:
IÅ?  #  That start with the second input-integer m
# (after which the filtered list of primes is output implicitly as result)


If we could assume that $$\n>L_m\$$, where $$\L_m\$$ is the length of $$\m\$$, it could be 7 bytes instead:

°Ý«¹ùʒp


Try it online.

Without that assumption, it would still be 9 bytes with this approach:

°Ý«Iš¹ùʒp


Try it online.

Explanation:

°          # Push 10 to the power of the first (implicit) input-integer n
Ý         # Pop and push a list in the range [0,10**n]
«        # Append each to the second (implicit) input-integer m
¹ù      # Only keep the integers with a length equal to the first input n
ʒ     # Filter it further by keeping those:
p    #  That are primes
# (after which the filtered list is output implicitly as result)

Iš      # (prepend the second input-integer m to the list; if the length of m is
# equal to n, the final result will be [m] iff m is a prime, or an
# empty list otherwise)


# Python, 105 bytes

lambda n,m:[p for p in primerange(10**n+1)if n==len(d:=str(p))and(s:=str(m))<=d<s+'~']
from sympy import*


Attempt This Online!