⍸2⌊/⊢∨⍳
Try it online!
Uses a different approach from Bubbler's answer, but only works for Dyalog 17.X. Errors with a domain error on non-prime powers, and doesn't error on truthy inputs.
The train is decomposed as the following
┌─┬─────────────────┐
│⍸│┌─┬─────┬───────┐│
│ ││ │┌─┬─┐│┌─┬─┬─┐││
│ ││2││⌊│/│││⊢│∨│⍳│││
│ ││ │└─┴─┘│└─┴─┴─┘││
│ │└─┴─────┴───────┘│
└─┴─────────────────┘
First a range from 1 to the right argument is created using ⍳
.
(⍳) 10
1 2 3 4 5 6 7 8 9 10
Then each value in this list is GCDed ∨
with the right argument ⊢
.
(⊢∨⍳) 10
1 2 1 2 5 2 1 2 1 10
Then the pairwise minimum is taken, with the pairs overlapping, so 2⌊/1 3 2
is equivalent to (1⌊3)(3⌊2)
.
(2⌊/⊢∨⍳) 10
1 1 1 2 2 1 1 1 1
Monadic ⍸
in Dyalog 17.0 is a function that finds the indices of truthy values given a binary array, but this function of it is irrelevant for this solution. If the input is not a prime power, it will have at least one number greater than 1 when the above is performed (proof below). Otherwise if it is a prime power, the above will result in a list with only 1s. So applying ⍸
on a non-prime power will cause the function to error because its argument is not a binary array, whereas on a prime power it won't error because the argument would binary consisting only of 1s.
In Dyalog 18.0 ⍸
has been extended to also accept non-binary arrays, so the above won't work. Instead, using 'unique' ∪
in place of ⍸
will return the 1-length vector consisting only of 1 for truthy values, and a longer vector for falsey values.
Bubbler gave the following proof as to why 2⌊/⊢∨⍳
does not give a vector of 1s for falsey values.
Consider the Diophantine equation \$p_1a-p_2b=1\$, where \$p_1\$ and \$p_2\$ are distinct prime factors of a non-prime tower \$N\$ and \$a,b>0\$. This equation has solutions, a proof of which can be found in the following Wikipedia article. What this equation means is that you will have a situation where a multiple of \$p_1\$ occurs as the number immediately following a multiple of \$p_2\$. Since each prime factor divides the input \$N\$, their multiple, when GCDed with the input, will result in a value greater than 1. The fact that these multiples are less than \$N\$ is left as an exercise to the reader. So the two multiples of prime numbers following one another and that these multiples are less than \$N\$ mean that you have a situation where 2⌊/⊢∨⍳
will have a number greater than 1 in it.
This situation does not occur for prime powers because given any two prime factors \$p^m\$ and \$p^n\$ where \$n>m\$, there will never be a situation where \$p^na-p^mb=1\$ because that would imply \$p(p^{n-1}a-p^{m-1}b)=1\$, i.e. that \$p\$ divides 1 which is false.