Multi-Base Primes

-3

For the purpose of this challenge, a multi-base prime is a prime which, when written in base 10, is prime in one or more bases smaller than 10 and larger than 1 as well. All single-digit primes are trivially multi-base primes. 11 is also a multi-base prime, as 11 in binary is 3, which is prime (it is also prime in base 4 and base 6). The first few terms are: 2,3,5,7,11,13, 17, 23,31,37,41,43,47,53,61...

Your Task:

Write a program or function that, when given an integer as input, returns/outputs a truthy value if the input is a multi-base prime, and a falsy value if it is not.

Input:

An integer between 1 and 10^12.

Output:

A truthy/falsy valule, depending on whether the input is a multi-base prime.

Test Cases:

3    -> truthy
4    -> falsy
13   -> truthy
2003 -> truthy (Also prime in base 4)
1037 -> falsy (2017 in base 5 but not a prime in base 10)

Scoring:

This is , lowest score in bytes wins!

Gryphon

Posted 2017-10-10T10:40:09.310

Reputation: 6 697

Question was closed 2017-10-10T21:37:30.823

Could you add a test case that's prime in base-2 and base-10 only? I haven't been able to find any such numbers yet but if any do exist, I'll need to update my solution. – Shaggy – 2017-10-10T11:15:32.580

3@Shaggy 173,1259,1277,2069,2099,2237,2797,2801,3331,3539,3541,3851,3929,3989,4261,4349,4373,5077,5087,5279,5399,6047,6269,6389... – None – 2017-10-10T11:37:52.327

1Thanks, @StraklSeth. I'd since found a couple but my solution needed to be updated anyway. – Shaggy – 2017-10-10T11:59:02.933

Isn't 17 a multi-base prime as well (101 in base 4)? – ovs – 2017-10-10T14:44:56.193

I concur that 17 seems to be a prime in bases 4, 6, and 10. – Peter Taylor – 2017-10-10T15:10:15.093

3The binary evaluation of 19 would be eleven (2^1×1+2^0×9=11), so it seems we are to explicitly ignore evaluating those possibilities that include "excessive digits" - is this correct? – Jonathan Allan – 2017-10-10T21:24:56.147

1I nominated this question for reopening but I now realize it is unclear. I would like to see the "excessive digit" problem solved. – Post Rock Garf Hunter – 2017-10-10T23:06:22.910

Answers

1

Python 2, 159 137 bytes

f=lambda n:p(n)*any(p(b(n,i))for i in range(2,10))
p=lambda n:all(n%x for x in range(2,int(n**0.5)+1))
b=lambda n,i:n and n%i+10*b(n/i,i)

Try it online!

Saved 22 bytes thanks to ovs.

TFeld

Posted 2017-10-10T10:40:09.310

Reputation: 19 246

137 bytes – ovs – 2017-10-10T16:58:58.650

0

Mathematica, 60 bytes

Or@@(p=PrimeQ)[FromDigits/@IntegerDigits[#,2~Range~9]]&&p@#&

Try it online!

J42161217

Posted 2017-10-10T10:40:09.310

Reputation: 15 931

As it stands 19 should be falsey even though 1,9 in "binary" is 1*2+9=11 which is prime. Edit - I've posted a comment under the OP to ask about this. – Jonathan Allan – 2017-10-10T21:22:48.447

@JonathanAllan I don't really get you...19 in binary is 10011... – J42161217 – 2017-10-10T21:52:04.473

1The question poses it the other way around: "11 is also a multi-base prime, as 11 in binary is 3, which is prime" – Jonathan Allan – 2017-10-10T22:11:30.697

0

Japt, 15 14 bytes

j ©Ao2@sX nÃdj

Try it


Explanation

Implicit input of integer U.

j

Test if input is prime (Boo-urns to input validation!).

©

Logical AND (&&).

Ao2@    Ã

Generate an array of integers from 2 to 9 and pass each through a function where X is the current element.

sX n

Convert U to a base-X string (s) and back to an integer (n).

dj

Check if any of the numbers in the array are prime.

Shaggy

Posted 2017-10-10T10:40:09.310

Reputation: 24 623