| Bytes | Lang | Time | Link |
|---|---|---|---|
| 061 | Arturo | 230305T142604Z | chunes |
| 123 | Java 8 | 230305T160741Z | Kevin Cr |
| 145 | PARI/GP | 230409T052854Z | 138 Aspe |
| 107 | Retina 0.8.2 | 230305T144840Z | Neil |
| 050 | Raku | 230306T162033Z | Sean |
| 075 | R | 230306T154331Z | Dominic |
| 2726 | J | 230305T110115Z | m90 |
| 025 | Charcoal | 230305T175200Z | Neil |
| 078 | Factor + combinators.extras math.primes | 230305T174504Z | chunes |
| 061 | Wolfram Language Mathematica | 230305T140942Z | ZaMoC |
| 111 | Python | 230305T113049Z | The Thon |
| 013 | 05AB1E | 230305T064414Z | Command |
| 079 | JavaScript ES6 | 230305T104609Z | Arnauld |
| nan | 230305T072025Z | The Thon | |
| 012 | Vyxal | 230305T035127Z | lyxal |
| 013 | Jelly | 230305T035028Z | caird co |
Arturo, 67 65 64 61 bytes
$=>[select&'x[map@++repeat[1+2*<=]3x=>prime?=@[<=x>0x=0x>0]]]
$=>[ ; a function
select& 'x [ ; select numbers from 1 to <input> and assign current elt to x
map [...] => prime? ; map over a block for primality
@ ; evaluate a block
++ [...] x ; append x to the end of a block
repeat [1+2*<=] 3 ; create the block [1+2*<=1+2*<=1+2*<=]
= ; is this equal to...
@[<=x>0x=0x>0] ; shortest way I could think of to make [true true false true]
] ; end select
] ; end function
Java 8, 133 126 123 bytes
i->{for(;i-->3;)if(p(i)+p(i-~i)+p(8*i+7)<4&p(4*i+3)>1)System.out.println(i);};int p(int i){for(int k=i;k%--i>0;);return i;}
Outputs each Imtiaz Germain prime on a separated newline in reversed order.
Explanation:
i->{ // Method with integer parameter and no return-type
for(;i-->3;) // Loop `i` in the range (input,3]:
if(p(i) // If `i` is a prime number
+p(i-~i) // and `2i+1` is a prime number
+p(8*i+7) // and `8i+7` is a prime number
<4 // (by checking if all three are 0 or 1)
&p(4*i+3) // and `4i+3` is NOT a prime number
>1) // (by checking whether it's NOT 0 or 1)
System.out.println(i);} // Print `i` with trailing newline
// Separated method with integer as both parameter and return-type,
// to check whether the given number (≥2) is a prime number (0 or 1)
int p(int i){
for(int k=i; // Set `k` to the given integer `i`
k%--i>0;); // Decrease `i` before every iteration with `--i`
// Keep looping as long as `k` is NOT divisible by `i`
return i;} // After the loop, return `i`
// (if it's 1 or 0, it means it's a prime number)
PARI/GP, 145 bytes
can find all desirable numbers up to 1e7 under the time limit in TIO.
Golfed version, try it online!
f(n)={seq=vector(4,i,0);for(i=1,4,seq[i]=n;n=2*n+1;);[isprime(s)|s<-seq]==[1,1,0,1]}
g(N)={res=[];forprime(p=2,N,if(f(p),res=concat(res,p)));res}
Ungolfed version
is_satisfying_condition(n) = {
seq = vector(4, i, 0);
for(i=1, 4, seq[i] = n; n = 2*n + 1;);
return([isprime(s) | s <- seq] == [1, 1, 0, 1]);
}
select_primes(N) = {
result = [];
forprime(p = 2, N, if(is_satisfying_condition(p)==1, result = concat(result, p)));
return(result);
}
g = select_primes(10000);
print(g);
Retina 0.8.2, 107 bytes
.+
$*
1
8$*
1{8}
$`7$*1¶
A`^(11+)\1+$
1(1?)
$1
G`^(11+)\1+$
1(1?)
$1
A`^(11+)\1+$
1(1?)
$1
A`^(11+)\1+$
%`1
Try it online! Explanation:
.+
$*
Convert to unary.
1
8$*
Multiply by 8.
1{8}
$`7$*1¶
Generate all numbers of the form 8k+7 less than that.
A`^(11+)\1+$
Delete all composite numbers.
1(1?)
$1
Integer divide by 2.
G`^(11+)\1+$
Delete all prime numbers.
1(1?)
$1
Integer divide by 2.
A`^(11+)\1+$
Delete all composite numbers.
1(1?)
$1
Integer divide by 2.
A`^(11+)\1+$
Delete all composite numbers.
%`1
Convert the results to decimal.
Raku, 50 bytes
{grep {is-prime $_&2*$_+1&8*$_+7&none 4*$_+3},^$_}
grep { ... }, ^$_returns all nonnegative integers less than the input argument$_that satisfies the bracketed predicate.$_ & 2*$_+1 & 8*$_+7 & none 4*$_+3is a conjunction of$_, the number being tested, as well as twice that number plus one, eight times that number plus seven, and anonejunction of four times that number plus three.is-primetests the primality of that junction, returning a truthy value if$_,2*$_+1, and8*$_+7are all prime, and4*$_+3is NOT prime.
R, 78 75 bytes
Edit: -1 byte thanks to pajonk
f=\(x)if(x)c(x[all(sapply(x*2^(0:3)-1,\(y)sum(!y%%2:y)<2)-!-2:1)]-1,f(x-1))
n, 2*n+1, 2*(2*n+1)+1 and 2*(2*(2*n+1)+1)+1 can be reformulated as x*2^(0:3)-1 using x=n+1.
So, we just check these for primes, and test that the desired result (TRUE TRUE FALSE TRUE) is always different to !-2:1 (FALSE FALSE TRUE FALSE), returning x-1 if so.
J, 27 26 bytes
[:I.0</@:p:0 1 7 3*&.>:/i.
i.: Make a list of the integers from 0 (inclusive) to the given number (exclusive).0 1 7 3*&.>:/:- First, apply
>:(increase by 1) to both0 1 7 3and the list from above. - Then, multiply (
*) them;/modifies the rank to produce a table of values. - Finally, apply the inverse of
>:, decreasing the values by 1.
- First, apply
0</@:p::0p:produces 0 for primes and 1 for non-primes.- Then (
@:, composition),</inserts (/)<('less than') between items, turninga b c dintoa < (b < (c < d)).
For Boolean (0/1) arguments,a < bis 1 if and only ifais 0 andbis 1. Therefore, the result of this is 1 precisely when its input is0 0 0 1.
[:I.: Make a list of the indices of 1s. A cap ([:) is used to make this monadic.
Charcoal, 25 bytes
IΦN⁼1101⭆⊖E⁴×⊕ιX²λ⬤…²λ﹪λν
Try it online! Link is to verbose version of code. Explanation:
N Input limit
Φ Filter on implicit range
⁴ Literal integer 4
E Map over implicit range
ι Outer value
⊕ Incremented
× Multiplied by
² Literal integer `2`
X Raised to power
λ Inner value
⊖ Vectorised decrement
⭆ Map over values and join
… Range from
² Literal integer `2`
λ To inner value
⬤ All values satisfy
λ Inner value
﹪ Modulo i.e. is not a multiple of
ν Innermost value
⁼ Equals
1101 Literal string `1101`
I Cast to string
Implicitly print
Factor + combinators.extras math.primes, 78 bytes
[ iota [ [ dup 2 * 1 + ] thrice 4array [ prime? ] map { t t f t } = ] filter ]
iota [ ... ] filterselect numbers up to input[ dup 2 * 1 + ] thricepushn, n*2+1, tos*2+1, tos*2+1to the stack4arraygather them into a sequence[ prime? ] mapprimality test each element{ t t f t } =is it equal to{ t t f t }?
Wolfram Language (Mathematica), 61 bytes
Select[Range@#,Boole@PrimeQ@NestList[2#+1&,#,3]=={1,1,0,1}&]&
Python, 118 114 112 111 bytes
lambda n:[i for i in range(n+1)if[*map(lambda x:all(x%k for k in range(2,x)),[i,i-~i,4*i+3,8*i+7])]==[1,1,0,1]]
Python, 123 119 118 bytes
f=lambda n,i=2:i<n and([*map(lambda x:all(x%k for k in range(2,x)),[i,i-~i,4*i+3,8*i+7])]==[1,1,0,1])*[i]+f(n,i+1)or[]
Probably can be improved.
-1 from both by using a trick from @KevinCruijssen's Java answer
Commented
lambda n: [i for i in range(n + 1) if # Anonymous function, taking an integer n
# Filter [0..n] by the following:
[*map( # Apply a function to each item of a list:
lambda x:all(x%k for k in range(2, x)) # Prime check function
, [i, i-~i, 4*i+3, 8*i+7]) # Applied to the list [i, 2*i+1, 4*i+3, 8*i+7]
] == [1, 1, 0, 1]] # i, 4*i+3, and 8*i+7 are prime, and 2*i+1 is not
f=lambda n, i=2: i<n and ( # Define a recursive function, f, taking an integer n,
# and using an integer, i, as the recursive variable
# If i is less than n:
[...] == [1, 1, 0, 1] # (same as above)
) * [i] + f(n, i+1) or [] # If true, add i. Make a recursive call with i+1
# Stop if i >= n
05AB1E, 14 13 bytes
ÅPʒ>3Lo*<pāÉQ
-1 thanks to @Kevin Cruijssen
I found a bunch of different ways for 14 bytes, but I can't find any 13 bytes solution ):
Lʒ>3Ýo*<pā3ÊQ
ÅPʒ>3Lo*<pJC5Q
ÅPʒ>3Lo*<pJƵ0Q
ÅPʒ>3Lo*<p2β5Q
ÅPʒ>3Lo*<p3LÉQ
ÅPʒ>3Lo*<pā+ÈP
Lʒ>3Ýo*<pJC13Q
Lʒ>3Ýo*<p2β13Q
Lʒ>3Ýo*<pJŽ4ιQ
Lʒ>3Ýo*<p4L3ÊQ
Explanation
ÅP generate all primes up to (and including) the input number
ʒ only keep those such that
> p+1
3L the list [1, 2, 3]
o 2^[1, 2, 3] = [2, 4, 8]
* times p+1 = [2p+2, 4p+4, 8p+8]
< -1 = [2p+1, 4p+3, 8p+7]
p is prime
ā length range, [1, 2, 3]
É is odd? [1, 0, 1]
Q equal to the results of is prime
JavaScript (ES6), 79 bytes
Returns a list.
f=n=>--n?(g=d=>q%--d?g(d):(q-=~q,d<2))(q=n)&g(q)&!g(q)&g(q)?[...f(n),n]:f(n):[]
Commented
f = n => // n = upper bound
--n ? // decrement n; if it's not 0:
( g = d => // g is a helper function taking d = q
q % --d ? // decrement d; if d is not a divisor of q:
g(d) // do recursive calls until it is
: // else:
( q -= ~q, // update q to 2q + 1
d < 2 // return true if d = 1
) // i.e. the original q was prime
)(q = n) & // test n (should be prime)
g(q) & // test 2n+1 (should be prime)
!g(q) & // test 4n+3 (should be composite)
g(q) // test 8n+7 (should be prime)
? // if all tests pass:
[ ...f(n), // append the result of a recursive call
n // followed by n
] //
: // else:
f(n) // just do a recursive call
: // else:
[] // stop
JavaScript (V8), 75 bytes
Prints the terms in reverse order.
n=>{for(;--n;)(g=d=>q%--d?g(d):(q-=~q,d<2))(q=n)&g(q)&!g(q)&g(q)&&print(n)}
Thunno +, \$ 17 \log_{256}(96) \approx \$ 13.99 bytes
g1+4R2@*1-NiJB13=
Explanation
g1+4R2@*1-NiJB13= # Implicit input: + flag adds one
g # Filter the range by the following: p
1+ # Add one p+1
4R # Push range(4) p+1, [0, 1, 2, 3]
2@ # Push 2 ** each p+1, [1, 2, 4, 8]
* # Multiply each [p+1, 2p+2, 4p+4, 8p+8]
1- # Subtract one [p, 2p+1, 4p+3, 8p+7]
Ni # Are they prime?
J # Join into a single string
B # Convert from binary
13= # Equals 13 (0b1101)?
Vyxal, 13 12 bytes
'‡d›↔4ẎæB13=
If I've understood the challenge correctly, for a number to be Imtiaz Germain, it has to first be prime, and applying 2p+1 3 times must produce the required pattern. Hence, the list [p, 2p + 1, 2(2p + 1) + 1, 2((2(2p + 1) + 1) + 1)] must equal [1, 1, 0, 1], which is 13 when converted from binary.
Accidentally the same algorithm as Jelly, which was posted while I was writing the explanation :p
Explained
'‡d›↔4ẎæB13=
' # From the range [1, input], keep numbers N where:
4Ẏ # the first 4 items of
‡d›↔ # applying `lambda x: 2 * x + 1` until fixed-point (yes it's infinite, but lazy evaluation means it doesn't get stuck here)
æ # tested for primality
B # converted from binary
13= # equals 13. This works for the reason explained in the introduction.
Jelly, 13 bytes
Ḥ‘$3СẒḄ=ʋƇ13
How it works
Ḥ‘$3СẒḄ=ʋƇ13 - Main link. Takes an integer n on the left
ʋ 13 - Last 4 links as a dyad f(i, 13):
$ - Last 2 links as a monad g(i):
Ḥ - 2i
‘ - 2i+1
3С - Collect [i, g(i), g(g(i)), g(g(g(i)))]
Ẓ - Is prime?
Ḅ - Convert from binary
= - Does that equal 13? I.e. is the pattern [prime, prime, composite, prime]?
Ƈ - Filter 1 ≤ i ≤ n by f(i, 13)