14
\$\begingroup\$

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.
\$\endgroup\$
6
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ Jan 6 at 23:41
  • \$\begingroup\$ Thank you very much, I will take it into account. If there is any modification to my question, I would like to know. \$\endgroup\$ Jan 6 at 23:47
  • 2
    \$\begingroup\$ I guess that \$m\ge3\$ actually means that \$m\$ would have at least 3 digits? \$\endgroup\$
    – Arnauld
    Jan 7 at 0:12
  • \$\begingroup\$ @Arnauld yes!!! \$\endgroup\$ Jan 7 at 1:17
  • \$\begingroup\$ It's a bit confusing that \$m\$ is both used for the input as for the length of the input. \$\endgroup\$
    – Ivo
    Jan 9 at 7:32

14 Answers 14

10
\$\begingroup\$

Bash + bsdgames package, 39

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

Try it online!

\$\endgroup\$
7
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ 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\$). \$\endgroup\$ Jan 7 at 22:33
  • 1
    \$\begingroup\$ @KevinCruijssen thanks for spotting. Correction implemented at no byte cost. \$\endgroup\$
    – pajonk
    Jan 8 at 8:20
6
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 11 \$\endgroup\$
    – emanresu A
    Jan 6 at 23:45
  • 1
    \$\begingroup\$ @emanresuA I originally had that, but I don't know if it's allowed because it technically never terminates \$\endgroup\$
    – lyxal
    Jan 6 at 23:48
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

Charcoal, 28 bytes

≔Xχ⁻NLηθIΦ⁺…⁰θ×Nθ∧›ι¹⬤…²ι﹪ιλ

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

≔Xχ⁻NLηθ

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

IΦ⁺…⁰θ×Nθ∧›ι¹⬤…²ι﹪ιλ

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

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

Python, 83 80 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

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

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.

\$\endgroup\$
3
\$\begingroup\$

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
          Å?  # which start with
         ²    # the second input, m
              # Implicit output
\$\endgroup\$
3
\$\begingroup\$

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

10@D10,s:gNkgz1ZV

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

Port of lyxal's Vyxal answer.

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
               ZV  # which start with
             z1    # the second input, m
                   # Implicit output

Screenshot

Screenshot

\$\endgroup\$
3
\$\begingroup\$

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[9];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!

\$\endgroup\$
0
3
\$\begingroup\$

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
    );            //
}                 //
\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.