| Bytes | Lang | Time | Link |
|---|---|---|---|
| 020 | Vyxal 3 | 250722T131821Z | Themooni |
| 014 | Husk | 201112T154240Z | Dominic |
| 212 | Java JDK | 201112T180157Z | user |
| 123 | Raku | 201115T231228Z | Sean |
| 111 | Retina | 201113T105400Z | Neil |
| 208 | Python 3.8 | 201113T144033Z | Noodle9 |
| 175 | Factor | 201113T134027Z | Galen Iv |
| 016 | Stax | 201112T165652Z | Razetime |
| 108 | JavaScript ES6 | 201112T172620Z | Arnauld |
| 123 | Wolfram Language Mathematica | 201112T155639Z | ZaMoC |
| 011 | 05AB1E | 201112T154327Z | Kevin Cr |
| 013 | 05AB1E | 201112T181323Z | ovs |
| 009 | Brachylog | 201112T170534Z | xash |
| 019 | Japt | 201112T152904Z | Shaggy |
Vyxal 3, 20 bytes
kNʎ∆qk±≡nK~℗¨ffS/ÞƵ∧
Online is too slow to output anything but first 2 but I can get a few more in under a minute on my machine:
kNʎ∆qk±≡nK~℗¨ffS/ÞƵ∧
kN # natural numbers
ʎ # filtered by...
∆qk±≡ # are the prime exponents exactly equal to [1, 1]?
∧ # and
nK~℗ # are the number's prime factors...
¨ffS # as a sorted, flattened list of digits...
/ÞƵ # invariant under "range-to-length-1"?
💎
Created with the help of Luminespire.
Give the snippet 30 seconds to process first 5 :D
<script type="vyxal3">
kNʎ∆qk±≡nK~℗¨ffS/ÞƵ∧ }5⊖
</script>
<script>
args=[]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Husk, 21 19 18 16 14 bytes
(or 15 bytes for versions that just output the n-th, or first n all-inclusive semi-primes, and so will always terminate instead of running indefinitely)
Edit: -2 bytes thanks to Razetime's suggestion to just use p twice instead of my complicated approach of spending 3 bytes just to save one character...
...then another -1 byte, and again -3 bytes, thanks to Razetime
...then -2 more bytes thanks to Leo
fȯεtpfȯS¦ŀṁdpN
Returns infinite list of all-inclusive semi-primes.
TIO header selects first 20 to avoid timing-out.
!fo§&o=2L(§=OoḣLṁd)pN
! # get the input-th element of
f N # natural numbers that satisfy
o§& p # both conditions of prime factors:
o=2L # there are 2 of them, and
ṁd) # their digits
O # sorted
(§= # are equal to
oḣL # the sequence from 1..number of digits
Java (JDK), 228...214 212 bytes
-7 bytes thanks to @Kevin Cruijssen!
b->{int x=9,l,C[],y,i=b;for(;i>0;i-=b){y=++x;var s="";for(b=0;y>1&&b++<2;y/=l,s+=l)for(l=1;l++<y&y%l>0;);C=new int[l=s.length()];b-=y;for(var c:s.getBytes())b&=c-49<l&c>48?C[c-49]=1:0;for(int k:C)b&=k;}return x;}
Returns the nth element of the sequence (1-indexed).
Ungolfed:
b -> {
int x = 9, l, C[], y, b, i = b;
for (; i > 0; i -= b) {
y = ++x;
var s = "";
for (b = 0; y > 1 && b++ < 2; y /= l, s += l)
for (l = 1; l++ < y & y % l > 0;);
C = new int[l = s.length()];
b -= y;
for (var c : s.getBytes())
b &= c - 49 < l & c > 48 ? C[c - 49] = 1 : 0;
for (int k : C) b &= k;
}
return x;
}
Raku, 123 bytes
{grep({$!=$_/($/=first $_%%*,2..$_);$!.is-prime&&{.[0]===.first(:k,0)}(+«[\∖]
"$!$/".comb.Bag,|('1'..'9'))},9..∞)[$_]}
$_is the argument to the function.grep(..., 9..∞)filters the integers from nine to infinity.[$_]indexes into that sequence with the function argument and returns that value. Within the filtering function$_is the number being tested.$! = $_ / ($/ = first $_ %% *, 2..$_)sets$/to the smallest (necessarily prime) divisor of$_, and$!to$_divided by that number.- If
$!is not prime, then$_is not a semiprime, so the filter function returns false. "$!$/"is the concatenation of the semiprime's two factors..combreturns a list of those digits..Bagreturns a bag (that is, a set with multiplicity) of those digits.[\∖]is a triangular reduction using the set-difference operator∖on the list consisting of the aforementioned Bag and each of the digits from 1 to 9. The elements of this reduction are the initial Bag, then the same bag with a single1removed, then that second Bag with a single2removed, and so on.
Now the idea is to check that this sequence of bags gets exactly one element smaller as one of each successive digit is removed until it reaches a size of zero.
+«turns that list of Bags into a list of their sizes (which counts multiplicity).- That list of sizes is fed into the bracket-delimited anonymous function, which is immediately called. Within that function,
$_refers to the list of sizes. .first(:k, 0)returns the index of the first zero element of the list. If no element is zero,Nilis returned instead..[0] === .first(:k, 0)is true if the index of the first zero is equal to the first element of the list. This will be true only for lists of the formN, N-1, N-2, ..., 1, 0, 0, 0. From the way the list is constructed, each successive element can only ever be one or zero less than the preceding element.
Retina, 112 111 bytes
123456789
L$`.
:::$>`
+%/\d$/&Lv$`(.)(.*:)(.)
$`$1$3$2$'
\d+
*
A`:_?:|(:_+)\1|:(__+)\2+:
:(_+):(_+):
$.($.1*$2
Try it online! Above program outputs the entire sequence, but TIO times out if the total number of semiprime digits is not less than 5, so the link only outputs six elements. Explanation:
123456789
Start with the potential semiprime digits.
L$`.
:::$>`
Get all their prefixes and add some markers to delimit the potential prime factors.
+%/\d$/&Lv$`(.)(.*:)(.)
$`$1$3$2$'
Generate all permutations of the digits among the potential primes.
\d+
*
Convert all the numbers to unary.
A`:_?:|(:_+)\1|:(__+)\2+:
Delete all results unless both numbers are prime and the first is greater than the second. This is one of the slowest parts of the program; Try it online! can calculate 23 results if these are split into separate filters. However, despite my best efforts, larger results are simply too slow.
:(_+):(_+):
$.($.1*$2
Calculate the resulting semiprimes.
Python 3.8, 208 bytes
def f(x,r={26},n=9,R=range):
while len(r)<x:
for d in R(2,n:=n+1):n%d<1and all(k%i for k in(d,n//d)for i in R(2,k))*(l:=[*map(int,sorted(str(d)+str(n//d)))])==[*R(1,len(l)+1)]and r.add(n)
return sorted(r)
Inputs an integer \$n\$ and output the first \$n\$ all-inclusive semi-primes (\$1\$-indexed).
Factor, 190 175 bytes
: f ( n -- n ) 1 [ over 0 = ] [ dup factors [ 10 >base ] map
[ pair? ] [ concat [ length [1,b] [ 48 + ] map ] keep
diff ""= ] bi and [ [ 1 - ] dip ] when 1 + ] until nip 1 - ;
Stax, 22 21 17 16 bytes
âH«q☻╧Ñ♦├x╓║Nm"°
-1 byte using a generating block.
-4 bytes using a filter.
-1 byte from wastl.
Outputs infinite list, separated by newlines.
Explanation (Uncompressed)
VIf|fY%2=y$eEc%R|}*
VIf filter natural numbers, printing truthy values
take current iteration value
|fY store prime factorization in Y(without pop)
% Length
2= Equals 2?
y Push prime factors again
$eE Convert to string, then to digits
c% copy and take length
R Range [1..length]
|} are they setwise equal?
* multiply the two values
JavaScript (ES6), 108 bytes
Returns the n-th term, 1-indexed.
f=(n,k=2)=>(g=n=>a=q>n?'':n%q?g(n,q++):q+g(n/q,i--))(k,q=i=2)*i|[...a].sort().some(v=>v^++i)||--n?f(n,k+1):k
Commented
f = ( // f is a recursive function taking:
n, // n = input
k = 2 // k = next integer to test
) => ( //
g = n => // g is a recursive function taking an integer and building
// a concatenation of its divisors
a = // assign the final result to a:
q > n ? // if q is greater than n:
'' // stop and coerce the answer to a string
: // else:
n % q ? // if q is not a divisor of n:
g(n, q++) // do a recursive call to g with q + 1
: // else:
q + // append q
g(n / q, i--) // and do a recursive call to g with n / q and i - 1
)(k, q = i = 2) // initial call to g with n = k, q = 2 and i = 2
* i | // if i is not equal to 0 (i.e. k has not exactly 2 divisors)
[...a].sort() // or the sorted digits of a ...
.some(v => v ^ ++i) // ... do not match the sequence 1, 2, ..., x
|| --n ? // or the previous conditions are met but decrementing n does
// not result in 0:
f(n, k + 1) // do a recursive call to f with k + 1
: // else:
k // success: return k
Wolfram Language (Mathematica), 123 bytes
outputs the nth term
(n=w=0;While[n<#,If[Last/@(f=FactorInteger)@w=={1,1}&&(s=Sort[Join@@(IntegerDigits@#&@@@f@w)])==Range@Max@s,n++];w++];w-1)&
the following prints all 36918 terms
Wolfram Language (Mathematica), 139 bytes
Union@(f=Flatten)[Times@@d/@#&/@Select[f[(q=#;TakeDrop[q,#]&/@o)&/@Permutations[o=Range@#],1],And@@PrimeQ[(d=FromDigits)/@#]&]&/@3~Range~9]
05AB1E, 17 16 14 12 11 bytes
∞ʒÒDS{āQиg<
Outputs the infinite sequence.
-2 bytes thanks to @CommandMaster, which also opened up the opportunity to golf 2 more bytes
-1 byte thanks to @ovs.
Explanation:
∞ # Push an infinite list of positive integers: [1,2,3,...]
ʒ # Filter this list by:
Ò # Get a list of its prime factors (including duplicates)
D # Duplicate this list
S # Convert the list of integers to a flattened list of digits
{ # Sort these digits
ā # Push a list in the range [1,length] (without popping the list itself)
Q # Check that the two lists are equal (1 if truthy; 0 if falsey)
и # Repeat the list if prime factors that many times as list
# (so it'll become empty for falsey results; and stays the same for truthy)
g # Pop and push the length of this list
< # And decrease it by 1
# (NOTE: only 1 is truthy in 05AB1E, so if the amount of prime factors was
# anything other than 2, this will be falsey)
# (after which the filtered infinite list is output implicitly as result)
05AB1E, 13 bytes
Outputs all all-inclusive semi-primes, not quite in ascending order.
7°ÅpâʒS{āQ}PÙ
Commented:
The S{āQ is borrowed from Kevin's answer.
7° # push 10^7
Åp # push the first 10^7 primes
# the first 5694505 would be enough but costs more bytes
â # take the cartesian product of that list with itself
# to create all pairs of primes
ʒ } # filter the pairs by
S # split into a list of digits
{ # sort the digits
ā # push the range [1 .. length]
Q # are those lists equal?
P # take the product of each pair
Ù # take the unique elements
The different order is a result of the order of the cartesian product, pairs that start with 2 are first, then pairs with 3, 7... The order could be fixed by replacing Ù with ê (unique sort), but this would prevent output until all calculations are finished.
98765431, the 5694505th prime number, is the largest prime that can be used to create an all-inclusive semi-prime.
Brachylog, 9 bytes
-3 thanks to Unrelated String!
This defines a predicate that unifies with all-inclusive-semi-primes. In Prolog this is equivalent to a generator, that will return all all-inclusive-semi-primes one by one.
ḋĊcẹo~⟦₁&
ḋĊcẹo~⟦₁&
ḋ prime factors
Ċ are a pair (list of length 2)
cẹ and the elements of the concatenation (all the digits)
o ordered
~⟦₁ are the result of a range operation, f.e.
4 ⟦₁ [1,2,3,4]
[1,2,3,4] ~⟦₁ 4
& return the input
Japt, 22 21 19 bytes
Returns the first n terms; replace j with i to return the 0-indexed nth term.
È=k)cì ÍeX¬ÊõXÊÉ}jU
Try the first 10 terms or the first 100 terms
È=k)cì ÍeX¬ÊõXÊÉ}jU :Implicit input of integer U
È :Function taking an integer X as argument
= : Reassign to X
k : Prime factors
) : End reassignment
c : Flat map
ì : Digit arrays
Í : Sort
e : Test equality with
X¬ : Join X
Ê : Length (a)
XÊÉ : Length of X minus 1 (b)
õ : Range [b,a]
} :End function
jU :Get the first U integers that return true (or the Uth, if using i)
