g | x | w | all
Bytes Lang Time Link
039Raku Perl 6 rakudo241213T174159Zxrs
055AWK241107T163114Zxrs
012Japt v1.4.5 h170825T165247ZShaggy
016MATL170826T121224ZSuever
012Husk210405T090218ZRazetime
077Java 8201007T143237ZKevin Cr
00905AB1E201007T141656ZKevin Cr
057R200922T064344ZRazetime
010Stax180501T112632Zwastl
033Bash + bsdgames package170826T024917ZDigital
014Pyth170825T165209ZMr. Xcod
112C# .NET Core170827T020646ZAyb4btu
056Ruby170826T060503ZMichael
054JavaScript ES6170826T015422ZJustin M
035Perl 6170826T193224ZSean
009Jelly170825T172613Znmjcman1
053Mathematica170825T193058ZJungHwan
389C++170826T131331ZDút
064PowerShell170826T043754ZTessella
049Bash + Core Utils170825T182057Zmarkasof
148PHP170826T003929ZMic1780
047Perl 5170825T194209ZXcali
01105AB1E170825T185327ZErik the
076Python 2170825T171559ZHalvard
009Neim170825T181111ZOkx
080Python 3170825T171446ZMr. Xcod
082Mathematica170825T171325ZZaMoC
128PowerShell170825T170059ZAdmBorkB

Raku (Perl 6) (rakudo), 39 bytes

{(0..$_).grep({.is-prime&&/666/}).tail}

Attempt This Online!

AWK, 55 bytes

{for(i=$1;j!=i+1;i--)if(i~/666/)for(j=1;i%++j;);}$0=i+1

TIO doesn't like.

awk '{for(i=$1;j!=i+1;i--)if(i~/666/)for(j=1;i%++j;);}$0=i+1' <<< 46666

Try it online!

{for(i=$1;j!=i+1;i--)   # count backward, break on match
if(i~/666/)             # only Satan ints
for(j=1;i%++j;);}       # primality
$0=i+1                  # set output

Japt v1.4.5 -h, 14 12 bytes

õ fj fÈsø666

Try it

MATL, 16 bytes

ZqP"@V'666'Xf?@.

Try it at MATL Online

Explanation

         Implicitly grab input (n)
Zq       Compute the primes up to n (output is in increasing order)
P        Flip the array (so larger primes come first)
"        For each prime
  @V     Convert it to a string
  '666'  Push the string literal '666' to the stack
  Xf     Find the location of '666' in the prime
  ?      If it was present...
    @.   Push it to the stack and break
         Implicitly display the stack contents

Husk, 12 bytes

ḟo`€d666dfṗṫ

Try it online!

Java 8, 77 bytes

n->{for(int i=++n;i>1|!(n+"").contains("666");)for(i=--n;n%--i>0;);return n;}

Try it online.

Explanation:

n->{               // Method with integer as both parameter and return-type
  for(int i=++n;   //  Increase the input by 1
                   //  Set `i` to this input+1 (setting `i` to anything above 1 is fine)
      i>1          //  Continue looping as long as `i` is not 0 nor 1
      |!(n+"").contains("666");)
                   //  or if `n` as String does not contain "666" as substring:
    for(i=--n;     //   Decrease `n` by 1 first
                   //   And then set `i` to this new `n`
        n%--i      //   Decrease `i` by 1 before every iteration
             >0;); //   And continue looping as long as `n` does NOT evenly divide `i`
  return n;}       //  After both loops, return the modified `n` as result

If for(i=n;n%--i>0;); results in \$i<2\$, it means \$n\$ is a prime number (note: this prime checker only works for \$n\geq2\$, which is fine in this case).

05AB1E, 9 bytes

ÅPR.Δ666å

Try it online. (No test suite with all test cases, because it's a bit too slow.)

Explanation:

ÅP         # Push a list of primes below or equal to the (implicit) input-integer
  R        # Reverse this list
   .Δ      # Find the first value which is truthy for:
     666å  #  Check whether it contains "666" as substring
           # (after which the found result is output implicitly)

The 666 can alternatively be Ž2ć (compressed 666) or Ƶé· (compressed 333 and then doubled).

R, 57 bytes

(a=conf.design::primes(scan():1))[which(grepl(666,a))[1]]

-42 bytes from Dominic Van Essen's enormous golf.

uses grepl to coerce values to string(since 666 cannot be considered a regex) and check for truthy values.

Runs through the array in reverse to save 2 bytes.

R + conf.design, 81 78 99 bytes

library(conf.design);function(n){a=primes(1:n)
a[max(which(grepl("666",as.character(a),fixed=T)))]}

Try it on rdrr.io!

My first R solution.

+21 bytes after including library name.

A simple filter. grepl returns true at indices with 666, which returns the truthy indices, and max gets the required index of the prime.

Stax, 10 bytes

ü>:Ñb/VP6─

Run and debug it

Explanation (unpacked):

^w:pc$666$#! Full program, implicit input-parsing
^            Increment input
 w           do-while:
  :p           Previous prime
    c$         Copy and stringify
      666$     Push "666"
          #    Number of occurences
           !   Logical not
             Implicit output

Bash + bsd-games package, 33

primes 2 $[$1+1]|grep 666|tail -1

Try it online.

Pyth, 15 14 bytes

Saved 1 byte with help from Dave.

Memory errors for 969696 and anything higher on my machine, but it is fine if it is given enough memory.

ef&/`T*3\6P_TS

Try it here or check out the Test Suite.


How?

ef&/`T*3\6P_TSQ - Full program, with implicit input (Q) at the end

             SQ - Range [1,Q]
 f              - Filter.
          P_T   - Is prime?
  &             - And
   /`T*3\6      - It contains 666.
e               - Last element.
                - Implicitly output the result.

Pyth, 14 bytes

ef/`T*\63fP_TS

Try it here!

C# (.NET Core), 117 115 112 bytes

f=>{for(int i=f;i>1;i--){int p=1,j=2;while(j<i)if(i%j++<1)p=0;if(p>0&$"{i}".Contains("666"))return i;}return 0;}

Try it online!

I'm sure this could be made shorter; maybe by recursively calling func f and removing the outer for-loop.

Recursive Approach, 85 bytes

i=>{int p=1,j=2;while(j<i)if(i%j++<1)p=0;return p>0&$"{i}".Contains("666")?i:f(--i);}

Try it online!

I'm unsure how well this approach fits within the bounds of code-golf due to having to set the Func<int,int> f = null first, and that f is called again, but not counted towards the bytes. Any clarification would be appreciated.

Ruby, 67, 66, 58, 56 bytes

Includes +7 bytes for -rprime

->z{z.downto(1).find{|x|/666/=~x.to_s&&x.prime?}}

It's pretty fast, computing values up to ~2^52 in about a second and 2^64 in under 5 minutes (2011 MBP, Ruby 2.3.1).

JavaScript (ES6), 55 54 bytes

-1 byte thanks to @ThePirateBay.

f=n=>/666/.test(d=n)&eval("while(n%--d);d<2")?n:f(n-1)

Very slow with large inputs. Primality test adapted from this code golf answer.

Timings

Tests

Some of these will hang your browser for several minutes.

f=n=>/666/.test(d=n)&eval("while(n%--d);d<2")?n:f(n-1)

function test(n) {
  O.value="Working..."
  setTimeout(_=>{
    let t = Date.now()
    O.value=`f(${n}) = ${f(n)} in ${(Date.now()-t)/1000}s`
  }, 10)
}
Tests<br>
<button onclick="test(6662)">6662</button>
<button onclick="test(10000)">10000</button>
<button onclick="test(328765)">328765</button>
<button onclick="test(678987)">678987</button>
<button onclick="test(969696)">6662</button><br>
Result<br>
<input id=O type=text size=25 readonly>

Faster Version, 56 bytes

Completes each test case in under a second.

f=n=>/666/.test(n)&&eval("for(d=2;n%d++;);d>n")?n:f(n-1)

;[6662, 10000, 328765, 678987, 969696].forEach(n=>console.log(`f(${n}) -> ${f(n)}`))

Perl 6, 35 bytes

my&f={/666/&&.is-prime??$_!!f $_-1}

Try it online!

Straightforward recursive solution.

Jelly, 10 9 bytes

Saved 10% thanks to @Dennis!

ÆRwÐf666Ṫ

Try it online!

Explanation

ÆR          # All primes in range [2, input]
   Ðf      # Keep those which satisfy
  w        # truthy if y is in x
     666   #           ^ (this is y)
        Ṫ  # Tail (take the last element)

Mathematica, 64 62 61 53 bytes

#//.i_/;!PrimeQ@i||ToString@i~StringFreeQ~"666":>i-1&

-1 byte thanks to @KellyLowder

-8 bytes (wow) thanks to @Notatree

Explanation

Take an input. We decrement it under these conditions:

We repeat that until a Satan prime is reached.

C++ 389 bytes

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/miller_rabin.hpp>
using namespace boost::random;typedef boost::multiprecision::cpp_int Z;int main(int,char**v){mt19937 m(clock());independent_bits_engine<mt11213b,256,Z>g(m);Z n{v[1]},p;while(p++<=n)if(miller_rabin_test(p,25,g)&&p.convert_to<std::string>().find( "666" )!=-1)std::cout<<p<<" ";}

This is a full program!
You'll need Boost to compile it. (Or copy and paste into your favorite online C++ shell.)
Run it from the command-line giving n as argument.

Ungolfed:

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/miller_rabin.hpp>
using namespace boost::random;

typedef boost::multiprecision::cpp_int integer;

int main( int argc, char** argv )
{
  mt19937 mt( clock() );
  independent_bits_engine <mt11213b, 256, integer> rng( mt );

  integer input {argv[ 1 ]};
  integer possible;

  while (possible++ <= input)
    if (
      // is_prime( possible )
      miller_rabin_test( possible, 25, rng )
    && 
      // possible has "666" in it
      (possible.convert_to <std::string> ().find( "666" ) != std::string::npos))

    std::cout << possible << " ";
}

Shortcuts were made in terms of random number testing. The original code started testing possible primes at 6661 and incremented by two. You'll also get a compiler warning because of that (-1) there instead of npos.

Still, this runs pretty quickly. It only took about 40 seconds to find all 214 satan primes under 1,000,000 on my old AMD Sempron 130.

:^D

PowerShell, 71 69 64 bytes

param($s)for(;$s-notmatch666-or(2..($s/2)|?{!($s%$_)});$s--){}$s

Try it online!

Bash + Core Utils, 51 49 Bytes

seq $1|tac|factor|awk 'NF==2&&/666/&&!a--&&$0=$2'

Takes command line argument. Can be quite slow with larger numbers.

PHP, 148 bytes

<?php $p=[2];$s=[];for($i=3;$i<=$argv[1];$i++){foreach($p as $q)if($i%$q===0)continue 2;$p[]=$i;if(strpos($i,'666')!==false)$s[]=$i;}echo end($s);?>

Try it online!

Perl 5, 47 bytes

46 bytes of code + 1 for -p

{$f=0|sqrt;1while$_%$f--;/666/*!$f||$_--*redo}

Try it online!

05AB1E, 11 bytes

ƒNpN666å*iN

Try it online!

Python 2, 77 76 bytes

Edit: -1 byte thanks to @Mr.Xcoder

Slow running time, runs in O(n^2)

lambda x:max(q for q in range(x+1)if"666"in`q`*all(q%t for t in range(2,q)))

Try it online!

Another 76 bytes solution

lambda x:max(q*("666"in`q`*all(q%t for t in range(2,q)))for q in range(x+1))

Try it online!

With SymPy 73 bytes

lambda x:max(q for q in primerange(0,x+1)if"666"in`q`)
from sympy import*

Try it online!

Neim, 9 bytes

>ͻ:D+6S𝕚÷

Explanation:

>         Increment input
 ͻ        Start infinite loop
  :        Previous prime
   D       Duplicate
    +6     Push 666
      S    Swap
       𝕚   See if 666 is a substring of the top of the stack
        ÷  If true, break

Try it online!

Python 3, 85 83 80 bytes

Halvard's is 4 bytes shorter because it's done in Python 2.

lambda k:max(x for x in range(k+1)if"666"in str(x)*all(x%i for i in range(2,x)))

Try it online!

Give it some time, it's extremely slow because of its O(n^2) complexity.

Mathematica, 82 bytes

Last@Select[Prime@Range@PrimePi@#,!FreeQ[Subsequences[IntegerDigits@#],{6,6,6}]&]&

PowerShell, 128 bytes

param($n)function f($a){for($i=2;$a-gt1){if(!($a%$i)){$i;$a/=$i}else{$i++}}}for(){if($n-match666-and($n-eq(f $n))){$n;exit}$n--}

Try it online!

PowerShell doesn't have any prime factorization built-ins, so this borrows code from my answer on Prime Factors Buddies.

We take input $n, then declare a new function f that calculates out the factors of input $a. If the input $a is prime, then this will return just $a.

The main part of the program is the infinite for() loop. Inside the loop, we check if $n -matches against 666 and whether $n is prime (i.e., $n matches all of the factors of $n). If it is, we place $n on the pipeline and exit, with implicit output. Otherwise, we decrement $n-- and continue the loop.