| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | Vyxal 3 | 250707T113822Z | Themooni |
| 152 | Java 8 | 171218T095342Z | Kevin Cr |
| 024 | Pyth | 171213T201528Z | Mr. Xcod |
| 023 | Pyth | 171218T102135Z | Steven H |
| 125 | Clean | 171218T101027Z | Οurous |
| nan | Perl 5 | 171215T200615Z | Xcali |
| nan | 171215T184214Z | Brad Gil | |
| 036 | CJam | 171214T144249Z | Ven |
| 087 | Ruby | 171214T105647Z | G B |
| 014 | Jelly | 171213T151930Z | Ven |
| 086 | Perl 6 | 171213T143425Z | Ven |
| 016 | Brachylog | 171214T095737Z | Fatalize |
| 085 | Mathematica 85 Bytes | 171213T205455Z | Kelly Lo |
| 014 | Jelly | 171213T171611Z | Mr. Xcod |
| 091 | Mathematica | 171213T171624Z | ZaMoC |
| 083 | JavaScript ES6 | 171213T142909Z | Arnauld |
| 123 | Python 2 | 171213T134928Z | ovs |
Vyxal 3, 15 bytes
↯ʎf~⍰J℗A}ɦ,t~¿,
-2 bytes with help from Weird Glyphs
↯ʎf~⍰J℗A}ɦ,t~¿,
ʎ } # filter...
↯ # inclusive range implicit inputs by...
℗A # are all primes:
f~⍰ # the number's digits that are truthy (not 0)?
J # and the number itself
ɦ, # print the first such number
t~¿, # if the last such number is non-zero, print it as well
💎
Created with the help of Luminespire.
<script type="vyxal3">
↯ʎf~⍰J℗A}ɦ,t~¿,
</script>
<script>
args=[["1","100"],["70","80"],["190","200"],["2000","2100"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Java 8, 165 164 163 152 bytes
(a,b)->{for(;a<=b&p(a);a++);for(;b>a&p(b);b--);return a>b?"0":a+" "+b;}boolean p(int n){int i=1;for(;n%++i%n>0;);return!(n+"").matches("[02357]+")|i<n;}
-1 byte thanks to @ceilingcat.
Explanation:
(a,b)->{ // Method with two integer parameters and String return-type
// (Input `a` is the lowest input, input `b` is the highest input)
for(;a<=b // Increase `a` as long as it's smaller than or equal to `b`,
&p(a);a++); // and it's not a prime, and not all of its digits are prime-digits
for(;b>a // Decrease `b` as long as it's larger than `a`,
&p(b);b--); // and it's not a prime, and not all of its digits are prime-digits
return a>b? // If `a` is now larger than `b`:
"0" // Return 0, because nothing is found
: // Else:
a+" "+b;} // Return the resulting `a` and `b`
// Separated method that'll result in true if the integer is NOT a prime number,
// or any of its digits are NOT prime numbers (excluding 0)
boolean p(int n){int i=1;for(;n%++i%n>0;);return!(n+"").matches("[02357]+")|i<n;}
Pyth, 24 bytes
Using my initial approach turns out to be shorter.
.x,eKfP#I_M-+TjT;0}EQhKZ
(I was just updating to 23 but Steven beat me to it)
?KfP#I_M-+TjT;0}FQhM_BK0
Natually, hM_BK can be replaced by ,hKeK.
25 bytes
.x,eKf.AmP_|d2+TjT;}EQhKZ
26 bytes
|>2.<f.AmP_|d2+TjT;*2}EQ1Z
|>2.<fP#I_M|R2+TjT;*2}EQ1Z
How they work
.x,eKfP#I_M-+TjT;0}EQhKZ ~ Full program. Q, E = first, second inputs
.x Z ~ Try-catch block. If the code errors, output 0.
f }EQ ~ Filter the range [E ... Q] for (uses a variable T):
+TjT; ~ Append T to the list of its digits.
- 0 ~ Remove the zeros.
_M ~ Multiply each by -1 (for primality testing).
I ~ Check if the result is invariant over...
P# ~ Removing non-prime items.
K ~ Assigned the filtered range to a variable K.
,e ~ Pair the last element of K with...
hK ~ Its first element.
|>2.<f.AmP_|d2+TjT;*2}EQ1Z ~ Full program.
*2}EQ ~ Inclusive range, repeated twice..
f ~ Filter, using T as the current number.
jT; ~ Base-10 digits of T.
+T ~ Concatenated with T.
mP_|d2 ~ Prime check after performing OR 2 (makes 0 be treated as prime)
.A ~ Do all satisfy this condition?
.< 1 ~ Rotate by one place cyclically to the left.
>2 ~ Last two elements (ignored if there aren't enough)
| Z ~ Logical or with 0.
Pyth, 28 25 23 bytes
.xhM_BfP#I_M #+TjT;}FQ0
Test suite. Returns [2003,2053] for the last test case, since 2053 is prime.
Clean, 142 131 125 bytes
import StdEnv
@a b#l=[n\\n<-[a..b]|and[gcd p n<2&&or[c==k\\k<-:"02357"]\\p<-[1..n-1],c<-:toString n]]
|l>[]=[hd l,last l]=[0]
Ungolfed:
import StdEnv
fn start finish
# primes
= [ n
\\
n <- [start..finish]
| and [ gcd p n == 1 && isMember c ['0','2','3','5','7']
\\
p <- [1..n-1],
c <-: toString n
]
]
| isEmpty primes
= [0]
= [hd primes, last primes]
Perl 5, 79 + 2 (-ap) = 81 bytes
map{$f=0|sqrt;1while$_%$f--;$s||=$_,$\=-$_ if!$f&&!/[14689]/}$F[0]..$F[1];$_=$s
Perl 6, 68 66 65 61 58 bytes
{($_=($^a..$^b).grep({.is-prime&&/^<[02357]>+$/})[0,*-1])[1]??$_!!0}
{($_=($^a..$^b).grep({.is-prime&&!/<[14689]>/})[0,*-1])[1]??$_!!0}
{($_=($^a..$^b).grep({.is-prime*!/<[14689]>/})[0,*-1])[1]??$_!!0}
{($_=($^a..$^b).grep({.is-prime*!/<[14689]>/}))??.[0,*-1]!!0}
{($_=grep {.is-prime*!/<[14689]>/},$^a..$^b)??.[0,*-1]!!0}
Expanded:
{ # bare block lambda with two placeholder parameters 「$a」 and 「$b」
(
$_ = # store the list in 「$_」 for later use
grep {
.is-prime
* # True * True == 1 (all others equal 0)
!/<[14689]>/ # doesn't contain a non-prime other than 0
},
$^a .. $^b # inclusive Range
) # is the list Truish (not empty)
?? .[0,*-1] # if so output the first and last values (from 「$_」)
!! 0 # otherwise return 0
}
Ruby, 87 bytes
->a,b{(z=(a..b).select{|x|(2...x).none?{|r|x%r<1}&&/[14689]/!~"%d"%x})[0]?(z.minmax):0}
Jelly, 14 bytes
Do2ÆPẠ
æRÇÐf.ị
æRÇÐf.ị Main link
æR Prime range
Ðf Filter the range with...
Ç The helper link
ị At index (decimal, returns [xs[floor], xs[ceil]], otherwise 0)
. 0.5
Do2ÆPẠ Helper link
D For each decimal
o2 Replace 0s with 2s, an actual prime prime (could be 3, 5, or 7).
ÆP Filter primes (1 if true, 0 if false)
Ạ Check if all are true
Thanks to Erik the Outgolfer for the help fixing a bug.
Thanks to Mr. Xcoder for the .ị trick.
Perl 6, 105 94 90 86 bytes
{my @b=grep {is-prime all($_,|.comb>>.Int Xor 2)},$^a..$^b;say @b??"@b[0] @b[*-1]"!!0}
Brachylog, 16 bytes
⟦₂{ṗṗᵐ}ˢ⟨⌋≡⌉⟩|∧0
The completely non-sensical "return 0 if there's no prime" makes us lose 3 bytes (|∧0) for no reason (it would return false. if we didnt add them)
Explanation
⟦₂ Range from the smallest element of the input to the biggest
{ }ˢ Select on that range:
ṗ Numbers that are primes
ṗᵐ And whose digits are primes
⟨ ⟩ Fork on this new list:
⌋ Minimum
⌉ maximum
≡ Do nothing and return [Minimum, Maximum]
|∧0 If all of this fails (i.e. the list after selection is empty), return 0
Mathematica 85 Bytes
I know there is already a similar answer, but the approach here is quite different.
MinMax@Cases[Range@##,x_/;PrimeQ@x&&DisjointQ@@IntegerDigits/@{x,14689}]/.{_,∞}->0&
This 83 character answer will paste and run in Mathematica. The TIO site doesn't know how to interpret ∞.
Jelly, 14 bytes
æRµDo2ÆPẠµÐf.ị
How it works
æRµDo2ÆPẠµÐf.ị ~ Full program.
æR ~ Inclusive prime range.
µ µÐf ~ Only keep those which satisfy a condition.
Do2ÆPẠ ~ The filter condition:
D ~ The decimal digits of the current number.
o2 ~ Logical or with 2 (maps 0 to 2 and any other digit to itself).
ÆP ~ Is prime (element-wise)?
Ạ ~ Check whether all the digits satisfy the condition.
.ị ~ Get the element at modular index 0.5. Some details:
~ Jelly is 1-indexed, so 1 gives us the first element, whilst 0
gives us the last element.
~ If the ceil and the floor of the given number N don't match,
then Jelly returns the items at indexes floor(N) and ceil(N).
~ If the list is empty, this yields 0, hence it is very convenient.
If taking the whole range would be allowed (although I think it should not be), then 12 bytes:
Do2,ÆPȦµÐf.ị
Mathematica, 91 bytes
If[(s=Select[Range@##,(p=PrimeQ)@#&&And@@((p@#||#<1)&/@IntegerDigits@#)&])!={},MinMax@s,0]&
JavaScript (ES6), 83 bytes
Takes the range [a-b] in currying syntax (a)(b). Returns either a 2-element array or 0.
n=>g=(m,a=0)=>n>m?a:g(m-1,(P=d=>m%--d?P(d):d>1|/[14689]/.test(m))(m)?a:[m,a[1]||m])
Test cases
let f =
n=>g=(m,a=0)=>n>m?a:g(m-1,(P=d=>m%--d?P(d):d>1|/[14689]/.test(m))(m)?a:[m,a[1]||m])
console.log(f(1)(100))
console.log(f(70)(80))
console.log(f(190)(200))
Python 2, 123 bytes
def f(a,b):r=[i for i in range(a,b+1)if(set(`i`)<=set('02357'))>any(i%k<1for k in range(3,i))];return len(r)and(r[0],r[-1])