Smallest Prime with a Twist (A068103)
Java (OpenJDK 8), 164 110 bytes
a->{int i=0;for(;!(i+"").matches("2{"+a+"}.*")|new String(new char[i]).matches(".?|(..+)\\1+");i++);return i;}
Thanks to @FryAmTheEggman for a bunch of bytes!
Try it online!
Brachylog, 12 11 bytes
:2rj:Acb#p=
Try it online!
This translates into Brachylog surprisingly directly. This is a function, not a full program (although giving the interpreter Z
as a command-line argument causes it to add the appropriate wrapper to make the function into a program; that's what I did to make the TIO link work). It's also fairly unfortunate that j
appears to be -1-indexed and needs a correction to allow for that.
You can make a reasonable argument that the =
isn't necessary, but I think that given the way the problem's worded, it is; without, the function's describing the set of all prime numbers that start with the given number of 2
s, and without some explicit statement that the program should do something with this description (in this case, generating the first value), it probably doesn't comply with the spec.
Explanation
:2rjbAcb#p=
:2rj 2 repeated a number of times equal to the input plus one
:Ac with something appended to it
b minus the first element
#p is prime;
= figure out what the resulting values are and return them
When used as a function returning an integer, nothing ever requests values past the first, so the first is all we have to worry about.
One subtlety (pointed out in the comments): :Acb
and b:Ac
are mathematically equivalent (as one removes from the start and the other adds to the end, with the region in between never overlapping); I previously had b:Ac
, which is more natural, but it breaks on input 0 (which I'm guessing is because c
refuses to concatenate an empty list to anything; many Brachylog builtins tend to break on empty lists for some reason). :Acb
ensures that c
never has to see an empty list, meaning that the case of input 0 can now work too.
Pyth, 12 bytes
f&!x`T*Q\2P_
In pseudocode:
f key_of_first_truthy_value( lambda T:
! not (
x`T*Q\2 repr(T).index(input()*'2')
)
& and
P_T is_prime(T)
)
Loops the lambda
starting from T=1
, incrementing by 1 until the condition is satisfied. The string of 2
s must be a substring from the beginning of the string, i.e. the index method needs to return 0
. If the substring is not found it returns -1
which conveniently is also truthy, so no exceptional case exists.
You can try it online here, but the server only allows up to an input of 4
.