Determine whether an integer is square-free
An integer is called square-free if it is not a multiple of a perfect square other than 1. For example, 42 is square-free, but 44 is not because it is a multiple of the perfect square 4 = 2².
Your task is to write a program or function that takes a positive integer, and returns a truthy value if the integer is square-free and a falsey value otherwise.
This is code golf, the shortest code wins.
The square-free numbers are OEIS sequence A005117 (thanks to Razetime for pointing this out).
Some test cases:
1 true
2 true
3 true
4 false
5 true
6 true
7 true
8 false
9 false
10 true
12 false
14 true
16 false
18 false
20 false
30 true
40 false
50 false
100 false
110 true
111 true
Scala, 41 bytes ```scala x=> …
4mo ago
Vyxal, 4 bytes ``` K∆²a ``` …
4mo ago
Ruby, 27 bytes ```ruby ->n …
4mo ago
Python3, 39 bytes ```python …
4mo ago
MATL, 6 bytes ``` YftuX= `` …
2mo ago
J, 17 bytes ```J {{./y|:2+ …
3mo ago
BQN, 13 bytesSBCS ```none ∧´ …
4mo ago
Japt, 7 bytes k ä!= e …
4mo ago
8 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
Scala, 41 bytes
x=>2 to x forall(d=>x%d+math.sqrt(d)%1>0)
Pretty straightforward
0 comment threads
Vyxal, 4 bytes
K∆²a
Outputs 0 for square-free, 1 for not.
K # Factors
∆² # Is perfect square
a # Any true?
0 comment threads
J, 17 bytes
{{*./y|~*:2+i.y}}
A direct definition closest to Razetime's infuriatingly good train solution. Outputs a non-zero number for true and 0 for false.
2 comment threads