| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Uiua SBCS | 240201T073445Z | chunes |
| 050 | Regex ECMAScript or better | 240122T213410Z | Deadcode |
| 2875 | Vyxal g | 240117T213747Z | pacman25 |
| 043 | Perl 5 ap | 240117T153821Z | Xcali |
| 027 | Mathematica | 240102T125229Z | pzq_alex |
| 008 | APLNARS | 240104T211317Z | Rosario |
| 003 | Nekomata + e | 230805T050829Z | alephalp |
| 043 | Python | 230728T210745Z | xnor |
| 063 | C | 230730T132705Z | Peter |
| 047 | Desmos | 230801T072946Z | Aiden Ch |
| 032 | Ruby rprime | 230731T213616Z | Value In |
| 027 | TIBASIC TI84 Plus/Plus CE | 230728T160627Z | joyofori |
| 008 | Brachylog | 230731T094256Z | Fatalize |
| 021 | Dyalog APL | 230729T161824Z | mitchell |
| 002 | Thunno 2 G | 230729T155326Z | The Thon |
| 032 | JavaScript | 230729T134346Z | Akif9748 |
| 004 | Vyxal | 230729T141538Z | noodle m |
| 007 | Japt | 230728T170429Z | Shaggy |
| 101 | Racket | 230728T113216Z | Ed The & |
| 024 | R | 230728T094349Z | pajonk |
| 022 | Octave | 230728T111409Z | Luis Men |
| 030 | Arturo | 230728T092754Z | chunes |
| 025 | Charcoal | 230728T090345Z | Neil |
| 064 | Retina 0.8.2 | 230728T085531Z | Neil |
| 018 | K ngn/k | 230728T071459Z | Bubbler |
| 029 | Factor + math.primes.factors math.unicode | 230728T070619Z | chunes |
| 003 | 05AB1E | 230728T070743Z | Kevin Cr |
| 043 | Python 3 | 230728T063503Z | dingledo |
| 008 | J | 230728T060449Z | Jonah |
| 042 | Excel | 230728T062130Z | Jos Wool |
| 004 | Jelly | 230728T060424Z | Unrelate |
Regex (ECMAScript or better), 50 bytes
^((?=(xx+?)\2+$)((?=\2+$)(?=(x+)(\4+$))\5){2,})*x$
Try it online! - ECMAScript
Try it online! - Perl
Try it online! - Java
Try it online! - Python
Try it online! - Ruby
Try it online! - PCRE
Try it online! - .NET
I adapted this on 2018-12-26, as a slight modification to the regex that matches numbers of the form \$n^k\$, where \$k\$ is a constant. In its implementation, that regex actually matches numbers that are either \$0\$ or of the form \$n^k=\prod_{i=0}^{q} p_i^{k_i}=p_0^{k_0}p_1^{k_1}p_2^{k_2}...p_q^{k_q}\$ where all \$p_m\$ are distinct primes, all \$k_m=k\$, and \$q\ge 0\$.
The only changes here, to match powerful numbers instead, are to make \$k_m\ge 2\$ (which takes 1 extra byte), and not to match \$0\$ (which saves 1 byte).
In other words, this regex works by dividing away each prime factor, from smallest to largest, asserting along the way that each distinct prime is divided away at least twice, with an end result of \$1\$.
^ # N = input number (initial value of tail)
( # Loop/iterate the following:
(?=(xx+?)\2+$) # \2 = smallest prime factor of tail
( # Loop/iterate the following:
(?=\2+$) # Assert tail is still divisible by \2
(?=(x+)(\4+$)) # \4 = largest proper divisor of tail
= tail / \2 (implicitly);
# \5 = tail - \4 (a tool to make tail = \4)
\5 # tail -= \5, i.e. tail = \4
){2,} # Iterate the above at least 2 times
)* # Iterate the above any number of times, minimum 0
x$ # Assert tail == 1
Retina 0.8.2, 56 bytes
.+
$*
^((?=(11+?)\2+$)((?=\2+$)(?=(1+)(\4+$))\5){2,})*1$
Vyxal g, 23 bitsv2, 2.875 bytes
∆Ǐċ
Bitstring:
00010101110001100111100
Prime exponents, check if not equal to one, minimum of that list (will always be greater than one for a powerful number)
Mathematica, 40 39 38 36 27 bytes
FactorInteger@#1∨#==1&
The first is \[VectorGreater], the second ∨ is \[Or\]. Assuming UTF-8 encoding, they both occupy 3 bytes.
APL(NARS), 8 chars
1∊≢¨⊂⍨π⎕
It return 0 if the number is as question ask, 1 otherwise.
It is read in reverse ⎕π⍨⊂¨≢∊1
⎕Call for input,π return the list of factors of input,
⍨⊂ doing partition as input⊂input,¨≢ for each element list, of partition
return the lenght,∊1 see if in the last list there is 1
test:
1∊≢¨⊂⍨π⎕
⎕:
36
0
~
a/⍨∼{1∊≢¨⊂⍨π⍵}¨a←⍳100
┌14─────────────────────────────────────┐
│ 1 4 8 9 16 25 27 32 36 49 64 72 81 100│
└~──────────────────────────────────────┘
Python, 43 bytes
f=lambda n,c=1:f(n,c+1)if c**n%n else n/c%c
Outputs Truthy/Falsey reversed. A different method than dingledooper's solution, without using square roots.
The idea is to find the square-free part c of n, that is, the product of the distinct prime factors of n. We do this by counting up to the smallest c where c**n%n is 0, which checks that the prime factors of c, if copied many times, cover those of n.
Having found c, we check whether n/c%c is zero, which is equivalent to n being divisible by c**2. This confirms that n includes each of its prime factors at least twice. This also works in Python 2 where / is integer division rather than float division, since n is divisible by c, for a solution with only exact integer arithmetic.
Rather than finding the smallest c with c**n%n, we could instead check all values of c from 1 to n for one where c**n%n and n/c%c are both 0. This could look like:
44 bytes
f=lambda n,c=1:c>n or(c**n%n+n/c%c)*f(n,c+1)
39 bytes with exit code
def f(n,c=1):c**n%n+n/c%c>0!=f(n,-~c%n)
From @loopy walt
Python 2, 42 bytes
d=n=input()
while n%d**2:d-=1
print d**n%n
Finds the largest value d where n is divisible by d**2, then checks that it includes each distinct prime factor of n via d**n%n being zero.
C, 69 63 bytes
-6 bytes thanks to @c-- suggesting to reverse the loops and pointing out that I'd managed to leave in an unused variable.
i,j;f(n){for(i=n;i;i--)for(j=n;j;j--)n*=i*i*j*j*j!=n;return!n;}
Desmos, 47 bytes
f(k)=0^{∏_{n=1}^{k^{.5}}mod((k/n^2)^{1/3},1)}
In case it bothers anyone, k/n^2 can't be shortened to k/nn because k/nn is parsed as k/n * n when we actually want k/(n*n).
Ruby -rprime, 32 bytes
Prime.prime_division returns a number's prime factors \$\prod_{k=1}^\infty {p_k}^{e_k}\$ in the form [ [p1, e1], [p2, e2], ... ] so this function checks whether every exponent is 2 or more.
->n{n.prime_division.all?{_2>1}}
TI-BASIC (TI-84 Plus/Plus CE), 35 27 Bytes
-8 bytes thanks to Bbrk24
Prompt A:For(B,1,A:If not(fPart(√(A/B:Stop:End:Disp 0
The thing right after A/B is a cube (3). The thing right before the A/B is a sqrt symbol.
Does nothing and terminates if your number is powerful and prints 0 and terminates if your number is not powerful.
Ungolfed
Prompt A
For(B,1,A
If not(fPart(√(A/B
Stop
End
Disp 0
Explained
Prompt A Asks user for input A
For(B,1,A For loop: increase B by 1 from 1 until it reaches A
If If...
√( square root of...
A/B A/B^3...
not(fPart( is a whole number...
Stop then stop execution.
End End of while loop, go back to While A>B
Disp 0 If, eventually, B does exceed A, then A is not a powerful number.
TI-BASIC programs are scored by tokens bytes, not characters.
TI-BASIC also completes parentheses for you.
Edit: Test cases
Brachylog, 8 bytes
Ċ^₂ʰ^₃ᵗ×
Takes input as the Output variable. Outputs true. or false. as you would expect.
Explanation
It’s just a description of the alternative definition.
Ċ Take a couple of 2 variables
^₂ʰ Square the first one
^₃ᵗ Cube the last one
× Multiply them: it must be equal to the given input
Brachylog will implicitly check if such a couple of variables Ċ exists
Dyalog APL, 21 bytes
⊢(⊣|*⍨)0⌈/∘⍸⍤=⊢|⍨2*⍨⍳
port of xnor's Python answer. Outputs inverse truthy value
⊢(⊣|*⍨)0⌈/∘⍸⍤=⊢|⍨2*⍨⍳ ⍝ implicit n
⍳ ⍝ list values 1..n
2*⍨ ⍝ square each value
⊢|⍨ ⍝ mod by n
0 = ⍝ find where index is 0
⌈/∘⍸⍤ ⍝ Get max index divisible (d)
⊢(⊣|*⍨) ⍝ then check d^n mod n
Thunno 2 G, 2 bytes
ḟḅ
Try it online! or verify the first few powerful numbers
Port of Unrelated String's Jelly answer. Output with inverted booleans. Add the ! flag to take the logical NOT.
Explanation
# Implicit input
ḟ # Get the list of prime factor exponents
ḅ # Check for each if it equals one
# Take the maximum of this list
# Implicit output
Vyxal, 4 bytes
ǏΠ²Ḋ
Uses Command Master's insight that these are numbers where the product of the distinct prime factors of the input is divisible by the input.
ǏΠ²Ḋ # implicit input of integer
Ǐ # distinct prime factors
Π # the product of this list
² # squared
Ḋ # is divisible by the (implicit) input
Racket, 101 bytes
(define(f n)((λ(D ?)(=(count(λ(p)(and(? p n)(?(sqr p)n)))D)(length D)))(prime-divisors n)divides?))
Explanation
We create a function f that takes one input n. We find the list of prime divisors of n and call it D. We then iterate through the list and check whether n is divisable by each element and n is divisable by each element squared. If both conditions are satisfied, we increment a counter. After iterating through list, we retrieve the total count and test whether the count is the same as the length of the list of divisors. If it is, then the number is a Powerful Number.
The ungolfed version is simpler to understand:
(define (f n)
(let ([D (prime-divisors n)])
(= (count (lambda (p)
(and (divides? p n)
(divides? (sqr p) n)))
D)
(length D))))
Have a wonderful weekend ahead!
R, 24 bytes
\(n)all((n/1:n)^.5%%1:n)
Port of @dingledooper's approach.
R, 26 bytes
\(n,i=1:n)n%in%(i^2%o%i^3)
Uses the alternative characterization from the question.
Octave, 22 bytes
@(n)n-(t=1:n).^2'*t.^3
- The output for powerful input is a matrix containing at least one entry equal to 0, which is falsy in Octave.
- The output for a non-powerful input is a non-empty matrix with all entries different than zero, which is truthy.
Try it online! The footer code contains truthiness/falsihood test.
Or verify all test cases. The footer code displays the input number if powerful (falsy output), or -- otherwise (truthy output).
Arturo, 33 30 bytes
$=>[in? 1tally factors.prime&]
Port of Unrelated String's Jelly answer.
$=>[ ; a function where input is assigned to &
in? 1 ; is 1 in
tally ; counts of
factors.prime& ; input's prime factors
] ; end function
Charcoal, 25 bytes
Nθ⊙…·²θ∧∧¬﹪θι﹪θ×ιι⬤…²ι﹪ιλ
Try it online! Link is to verbose version of code. Outputs an inverted Charcoal boolean, i.e. - if the input is not a powerful number, nothing if it is. Explanation:
Nθ Input as an integer
…· Inclusive range from
² Literal integer `2` to
θ Input integer
⊙ Any value satisfies
θ Input integer
¬﹪ Is divisible by
ι Current value
∧ Logical And
θ Input integer
﹪ Is not divisible by
×ιι Current value squared
∧ Logical And
… Range from
² Literal integer `2` to
ι Current value
⬤ All satisfy
ι Outer value
﹪ Is not divisible by
λ Inner value
Implicitly print
Retina 0.8.2, 64 bytes
.+
$*
^((.){2,})(?<!^\3+(..+))\1*$(?<!^\4*(?(2)$)(((?<-2>)\1)+))
Try it online! Link includes test cases. Outputs 0 for a powerful number, 1 if not. Explanation:
.+
$*
Convert to unary.
^((.){2,})
Search for a nontrival integer...
(?<!^\3+(..+))
... that is not composite...
\1*$
... and is a factor of the input...
(?<!^\4*(?(2)$)(((?<-2>)\1)+))
... and whose square is not a factor of the input.
K (ngn/k), 18 bytes
{|/~1!%x%/3#,1+!x}
Works more like dingledooper's Python solution.
{|/~1!%x%/3#,1+!x}
1+!x 1..x
x%/3#, starting from x, divide by above three times
% square root of each
|/~1! test if any of them has zero fractional part
K (ngn/k), 22 bytes
{|//x=*/:/1_*\3#,1+!x}
A bit of a convoluted way to check if the given number x is a^2 * b^3. Returns 1 if x is a powerful number, 0 otherwise.
{|//x=*/:/1_*\3#,1+!x}
1+!x 1..x inclusive -> A
3#, an array that contains three copies of A
*\ cumulative product (A; A^2; A^3)
1_ drop first
*/:/ reduce by cartesian product by multiplication
x= boolean matrix indicating each number is equal to input
|// max of all booleans
Factor + math.primes.factors math.unicode, 30 29 bytes
[ group-factors unzip 1 ∋ ]
Port of Unrelated String's Jelly answer.
-1 by using unzip instead of values to get at the exponents
group-factorsget an associative array of a number's prime factors and exponentsunzipget just the exponents1 ∋is 1 in this?
05AB1E, 3 bytes
Ó1å
Outputs an inverted 05AB1E boolean: \$1\$ if it's NOT a powerful numbers; \$0\$ if it is a powerful number (only 1 is truthy in 05AB1E).
Port of @UnrelatedString's Jelly answer, so make sure to upvote that answer as well!
Try it online or verify all test cases.
Explanation:
Ó # Get a list of exponents of the (implicit) input's prime factorization
1å # Does this list contain a 1?
# (after which the result is output implicitly)
Python 3, 43 bytes
Returns False for a powerful number, and True otherwise.
f=lambda n,i=1:i>n or(n/i)**.5%i>0<f(n,i+1)
Checks the condition that n is in the form a^2*b^3 by asserting that n/a^3 is a perfect square. In Python this would look something like (n/a**3)**.5%1==0. This condition can be rearranged so that it is slightly golfier:
(n/a**3)**.5%1==0
n**.5/a**1.5%1==0
n**.5/a**.5/a%1==0
(n/a)**.5/a%1==0
(n/a)**.5%a==0
J, 12 10 8 bytes
1 e._&q:
-2 by using Unrelated String's approach of checking for 1 instead of verifying that every exponents was 2 or more
-2 thanks to Bubbler for a shorter way to get prime exponents
_&q:shows the number's prime exponents1 e.checks if all exponents are greater than 1, by checking if 1 is an element and using 0 for truthy and 1 for falsy
Excel, 42 bytes
=LET(a,SEQUENCE(A1),OR(a^2*TOROW(a^3)=A1))
Input in cell A1.
Jelly, 4 bytes
ÆE1e
Inverted native truthy/falsy semantics: 0 for powerful numbers, 1 otherwise.
1e Is 1 an element of
ÆE the array of the input's prime exponents?





