Skip to main content
added 334 characters in body
Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 88 bytes

Note: as a special 'Language of the month' treat for anyone contemplating starting to golf in R (and I hope there are many of you...), I've written a short commentary of the various iterations that led to this answer in the R golfing chatroom.

function(n,k)for(x in 1:10^n)if(sum(x%/%10^(1:n-1)%%10%in%k)==n&(sum(!x%%1:x)<3))stop(x)

Try it online!

Outputs as an error message. No output when there is no solution.

Checks all numbers in the range 1:10^n (obviously 10^n is not a prime, so we don't need to worry about the end case):

  • x%/%10^(1:n-1)%%10 extracts the digits, and %in% k checks whether each digit is in the list. If the sum is ==n then we know that all the digits are Ok.
  • (sum(!x%%1:x)<3) checks for primality (a prime should have only 2 factors including 1 and itself).

When we find the first solution, we exit with stop(x) to output the answer as an error message. return(x) would be the friendlier solution to output to STDOUT, but it costs +2 bytes.

R, 88 bytes

function(n,k)for(x in 1:10^n)if(sum(x%/%10^(1:n-1)%%10%in%k)==n&(sum(!x%%1:x)<3))stop(x)

Try it online!

Outputs as an error message. No output when there is no solution.

Checks all numbers in the range 1:10^n (obviously 10^n is not a prime, so we don't need to worry about the end case):

  • x%/%10^(1:n-1)%%10 extracts the digits, and %in% k checks whether each digit is in the list. If the sum is ==n then we know that all the digits are Ok.
  • (sum(!x%%1:x)<3) checks for primality (a prime should have only 2 factors including 1 and itself).

When we find the first solution, we exit with stop(x) to output the answer as an error message. return(x) would be the friendlier solution to output to STDOUT, but it costs +2 bytes.

R, 88 bytes

Note: as a special 'Language of the month' treat for anyone contemplating starting to golf in R (and I hope there are many of you...), I've written a short commentary of the various iterations that led to this answer in the R golfing chatroom.

function(n,k)for(x in 1:10^n)if(sum(x%/%10^(1:n-1)%%10%in%k)==n&(sum(!x%%1:x)<3))stop(x)

Try it online!

Outputs as an error message. No output when there is no solution.

Checks all numbers in the range 1:10^n (obviously 10^n is not a prime, so we don't need to worry about the end case):

  • x%/%10^(1:n-1)%%10 extracts the digits, and %in% k checks whether each digit is in the list. If the sum is ==n then we know that all the digits are Ok.
  • (sum(!x%%1:x)<3) checks for primality (a prime should have only 2 factors including 1 and itself).

When we find the first solution, we exit with stop(x) to output the answer as an error message. return(x) would be the friendlier solution to output to STDOUT, but it costs +2 bytes.

Source Link
Dominic van Essen
  • 36.4k
  • 2
  • 22
  • 60

R, 88 bytes

function(n,k)for(x in 1:10^n)if(sum(x%/%10^(1:n-1)%%10%in%k)==n&(sum(!x%%1:x)<3))stop(x)

Try it online!

Outputs as an error message. No output when there is no solution.

Checks all numbers in the range 1:10^n (obviously 10^n is not a prime, so we don't need to worry about the end case):

  • x%/%10^(1:n-1)%%10 extracts the digits, and %in% k checks whether each digit is in the list. If the sum is ==n then we know that all the digits are Ok.
  • (sum(!x%%1:x)<3) checks for primality (a prime should have only 2 factors including 1 and itself).

When we find the first solution, we exit with stop(x) to output the answer as an error message. return(x) would be the friendlier solution to output to STDOUT, but it costs +2 bytes.