Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Determine whether an integer is square-free

+7
−0

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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Testcases? (2 comments)
[relevant oeis](http://oeis.org/A005117) (2 comments)

13 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.

+1
−0

Japt, 5 bytes

k
eUâ

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Scala, 41 bytes

x=>2 to x forall(d=>x%d+math.sqrt(d)%1>0)

Try it online!

Pretty straightforward

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Vyxal, 4 bytes

K∆²a

Try it Online!

Outputs 0 for square-free, 1 for not.

K    # Factors
 ∆²  # Is perfect square
   a # Any true?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Python3, 39 bytes

lambda n:all(n%i**2for i in range(2,n))

Try it online!

Makes a list comprehension from the numbers 2 through n of the remainder of the square, and then checks whether the list contains 0

-8 bytes thanks to celtschk‭
-5 bytes thanks to Moshi

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

not 0 in X => all X (2 comments)
Possible improvements (5 comments)
+2
−0

Ruby, 27 bytes

->n{(2..n).all?{n%_1**2>0}}

Try it online

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

BQN, 13 bytesSBCS

∧´0≠⊢|˜·×˜2+↕

Run online!

A train submission. It's 2 bytes shorter than the lambda version {∧´0≠𝕩|˜×˜2+↕𝕩}, due to omitting curly braces.

The idea is similar to ruby:

  • range 2..n+1
  • square it, then modulo n
  • are all remainders ≠ 0?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 17 bytes

{{*./y|~*:2+i.y}}

Try it online!

A direct definition closest to Razetime's infuriatingly good train solution. Outputs a non-zero number for true and 0 for false.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

J, 7 char

*./~:q:

How it works:

'q:' produces prime factors of number on right

'~:' replaces the first instance of unique numbers with a 1, the rest 0

'*./' tests for all ones

Sample runs:

*./~:q: 30

1

*./~:q: 40

0

*./~:q: 50

0

*./~:q: 100

0

*./~:q: 110

1

*./~:q: 111

1

Alternative 10 char solution:

(#=#@~.)q:

How it works:

'q:' of the number on the right produces its prime factors

'#' counts how many there are

'~.' eliminates repeats.

if the counts of factors before and after removing repeats are equal, then the number is square-free.

Sample runs:

(#@q:=#@~.@q:) 30

1

(#@q:=#@~.@q:) 40

0

(#@q:=#@~.@q:) 50

0

(#@q:=#@~.@q:) 100

0

(#@q:=#@~.@q:) 110

1

(#@q:=#@~.@q:) 111

1

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Myby, 12 5 bytes

primf=primfd
primf        : prime factors
     =       : equals
      primfd : unique prime factors

Evaluated as a monadic fork in J (f y) g (h y).

The test cases (retested) can be viewed here and were generated using this ruby script.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

How is this 5 bytes? I count 12 letters, each of which requires one byte. (2 comments)
+1
−0

MATL, 6 bytes

YftuX=

Try it online!

Same method as Moshi.

YftuX=
Yf      - factor with implicit input
   t    - duplicate
    u   - unique
     X= - isequal
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Factor, 108 bytes

USING: assocs kernel math math.primes.factors ;
IN: s
: ? ( n -- ? ) group-factors [ nip 2 < ] assoc-all? ;
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

JavaScript, 31 bytes

Outputs 0 for falsey and a non-zero value for truthy. If the 2 values must be consistent then replace the last * with && to output true instead.

n=>(g=d=>d++>n||n%d**2*g(d))(1)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Japt, 6 bytes

-1 byte thanks to Shaggy!

k ä¦ e

Try it

First time doing Japt so this is probably pretty bad. Just factorizes and checks that there are no duplicate factors.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

You missed a shortcut (2 comments)

Sign up to answer this question »