Abandon all squares, ye who divide me

37

3

Definitions

  • A perfect square is an integer which can be expressed as the square of another integer. For example, 36 is a perfect square because 6^2 = 36.
  • A squarefree number is an integer which is not divisible by any perfect square, except by 1. For example, 10 is a squarefree number. However, 12 is not a squarefree number, because 12 is divisible by 4 and 4 is a perfect square.

Task

Given a positive integer n, output the largest squarefree number which divides n.

Testcases

n   output
1   1
2   2
3   3
4   2
5   5
6   6
7   7
8   2
9   3
10  10
11  11
12  6
13  13
14  14
15  15
16  2
17  17
18  6
19  19
20  10
21  21
22  22
23  23
24  6
25  5
26  26
27  3
28  14
29  29
30  30
31  31
32  2
33  33
34  34
35  35
36  6
37  37
38  38
39  39
40  10
41  41
42  42
43  43
44  22
45  15
46  46
47  47
48  6
49  7
50  10

Scoring

This is . Shortest answer in bytes wins.

Standard loopholes apply.

Reference

Leaky Nun

Posted 2017-05-11T15:56:28.810

Reputation: 45 011

1

...and is called the radical - so 1980's!

– Jonathan Allan – 2017-05-11T16:14:41.853

Closely related, just multiply the two outputs. Edit: Never mind, it only matches on cubefree numbers. – xnor – 2017-05-11T20:13:00.503

Answers

45

05AB1E, 2 bytes

fP

Try it online!

How it works

f   Implicitly take input and compute the integer's unique prime factors.
 P  Take the product.

Dennis

Posted 2017-05-11T15:56:28.810

Reputation: 196 637

26ockquote>

_> really...??

– HyperNeutrino – 2017-05-11T16:02:55.583

@HyperNeutrino yep - if a number is not square-free it is because some of its prime factor(s) have multiplicity. – Jonathan Allan – 2017-05-11T16:07:52.943

@JonathanAllan I'm just interested in the built-in for unique prime factors. I wish Jelly had one of those... – HyperNeutrino – 2017-05-11T16:09:15.930

@HyperNeutrino It's 05AB1E, get used to it. 05AB1E has some really redundant builtins that apparently save bytes. – Erik the Outgolfer – 2017-05-11T16:33:07.863

6Correction, "save bytes", there's no probably about it. – Draco18s no longer trusts SE – 2017-05-11T21:43:07.437

14

Brachylog, 3 bytes

ḋd×

Try it online!

A very original answer...

Explanation

ḋ          Take the prime factors of the Input
 d         Remove duplicates
  ×        Multiply

Fatalize

Posted 2017-05-11T15:56:28.810

Reputation: 32 976

1Again, Brachylog beats Jelly because a two-byte atom is only one byte here. >:-P – HyperNeutrino – 2017-05-11T16:21:18.190

4Jelly having a lot of builtins is often seen as an advantage; but more builtins means that they need longer names on average. So there are tradeoffs involved in golfing language design. – None – 2017-05-11T17:44:56.527

2

I'm not trying to be "that guy", and maybe I just misunderstand byte counting, but isn't this 6 bytes? https://mothereff.in/byte-counter#%E1%B8%8Bd%C3%97

– Captain Man – 2017-05-11T18:15:32.457

5

@CaptainMan Brachylog uses a 256 chars custom code page which you can find here.

– Fatalize – 2017-05-11T18:16:47.473

14

JavaScript (ES6), 55 54 50 46 bytes

Quoting OEIS:
a(n) is the smallest divisor u of n such that n divides u^n

Updated implementation:
a(n) is the smallest divisor u of n positive integer u such that n divides u^n

let f =

n=>(g=(p,i=n)=>i--?g(p*p%n,i):p?g(++u):u)(u=1)

for(n = 1; n <= 50; n++) {
  console.log(n,f(n));
}

Arnauld

Posted 2017-05-11T15:56:28.810

Reputation: 111 334

Nice approach to the problem, esp. given lack of builtin factorization – Riking – 2017-05-13T16:42:04.977

12

MATL, 6 4 bytes

2 bytes saved with help from @LeakyNun

Yfup

Try it online!

Explanation

Consider input 48.

Yf   % Implicit input. Push prime factors with repetitions.  STACK: [2 2 2 2 3]
u    % Unique.                                               STACK: [2 3]
p    % Product of array. Implicit display.                   STACK: 6

Luis Mendo

Posted 2017-05-11T15:56:28.810

Reputation: 87 464

Out-golfed – Leaky Nun – 2017-05-11T16:01:10.597

@LeakyNun Heh, I was about to post that :-) Thanks – Luis Mendo – 2017-05-11T16:03:06.553

11

Jelly, 4 bytes

ÆfQP

Try it online!

ÆfQP  Main link, argument is z
Æf    Takes the prime factors of z
  Q   Returns the unique elements of z
   P  Takes the product

HyperNeutrino

Posted 2017-05-11T15:56:28.810

Reputation: 26 575

9

CJam, 8 bytes

rimf_&:*

Why does every operation in this program have to be 2 bytes -_-

Try it online!

ri       e# Read int from input
  mf     e# Get the prime factors
    _&   e# Deduplicate
      :* e# Take the product of the list

Business Cat

Posted 2017-05-11T15:56:28.810

Reputation: 8 927

I couldn't find a way to deduplicate. Nice! – Luis Mendo – 2017-05-11T16:17:16.043

@LuisMendo I just discovered that recently. I always thought it was multiset intersection but apparently it's just normal set intersection. – Business Cat – 2017-05-11T16:18:25.393

9

Retina, 36 30 28 bytes

+`((^|\3)(^(1+?)|\3\4))+$
$3

Input and output in unary.

Try it online! (Includes a header and footer for decimal <-> unary conversion and to run multiple test cases at once.)

Explanation

The idea is to match the input as a square times some factor. The basic regex for matching a square uses a forward-reference to match sums of consecutive odd integers:

(^1|11\1)+$

Since we don't want to match perfect squares, but numbers that are divisible by a square, we replace that 1 with a backreference itself:

(^(1+?)|\1\2\2)+$

So now the outer group 1 will be used n times where n2 is the largest square that divides the input and group 2 stores the remaining factor. What we want is to divide the integer by n to remove the square. The result can be expressed as the number of iterations of group 1 times group 2, but this is a bit tricky to do. Retina's $* will probably soon be improved to take a non-character token as its right hand argument in which case we could simply replace this with $#1$*$2, but that doesn't work yet.

Instead, we decompose the odd numbers differently. Let's go back to the simpler example of matching perfect squares with (^1|11\1)+$. Instead of having a counter \1 which is initialised to 1 and incremented by 2 on each iteration, we'll have two counters. One is initialised to 0 and one is initialised to 1, and they're both incremented by 1 on each iteration. So we've basically decomposed the odd numbers 2n+1 into (n) + (n+1). The benefit is that we'll end up with n in one of the groups. In its simplest form, that looks like this:

((^|1\2)(^1|1\3))+$

Where \2 is n and \3 is n+1. However, we can do this a bit more efficiently by noticing that the n+1 of one iteration is equal to the n of the next iteration, so we can save on a 1 here:

((^|\3)(^1|1\3))+$

Now we just need to go back to using an initial factor instead of 1 to match inputs that are divided by a perfect square:

((^|\3)(^(1+?)|\3\4))+$

Now all we need to do is replace this entire thing with $3 at the end, which stores the initial factor times the number of steps, which drops one factor of the square from the input.

This is done repeatedly with the + at the very beginning of the program, to account for inputs that contain higher powers than squares.

Martin Ender

Posted 2017-05-11T15:56:28.810

Reputation: 184 808

8

Octave, 27 bytes

@(x)prod(unique(factor(x)))

Similar approach as the other answers. The difference is: The functions have much longer names. I believe the code explains itself really: Takes the product of the unique prime factors of a number.

Stewie Griffin

Posted 2017-05-11T15:56:28.810

Reputation: 43 471

You ninja'd me by ~30 seconds :) – user41805 – 2017-05-11T16:19:25.343

1longer names you say – Pavel – 2017-05-11T16:24:42.143

7

Wolfram Language, 29 28 bytes

-1 Thanks to @Martin Ender ♦

Most[1##&@@FactorInteger@#]&

Explanation:

           FactorInteger@#    (*Get prime factorization as {{a,b},{c,d}}*)
     1##&@@                   (*Multiply list elements together, to get the product of the factors and the product of their exponents*)
Most[                     ]&  (*Take the first element*)

Scott Milner

Posted 2017-05-11T15:56:28.810

Reputation: 1 806

2Just realized this is basically @GregMartin's comment on the Mathics answer, only less golfy... – Scott Milner – 2017-05-11T18:43:10.463

Don't feel bad, I had the even less golfy answer of Times@@(#&@@@FactorInteger@#)& – Ian Miller – 2017-05-12T13:04:37.720

Most leaves it as a list. You need First to get the value. – Ian Miller – 2017-05-12T13:06:30.717

@IanMiller I realize that, but it is less bytes to just return a list with only one element. I assumed that that was ok, since it is still a reasonable output. – Scott Milner – 2017-05-12T13:36:31.017

7

Python, 37 bytes

f=lambda n,r=1:1>>r**n%n or-~f(n,r+1)

Try it online!

The largest squarefree divisor of n is that smallest number r with all of n's prime factors. We can check this as r**n%n==0, since r**n make n copies of each prime factor of r, and is divisible by n only if each of n's prime factors is represented.

The 1>>r**n%n is equivalent to int(r**n%n==0). If True can be used output 1, it's 2 bytes shorter to do.

f=lambda n,r=1:r**n%n<1or-~f(n,r+1)

xnor

Posted 2017-05-11T15:56:28.810

Reputation: 115 687

6

Mathics, 40 bytes

Times@@(Transpose@FactorInteger@#)[[1]]&

Try it online!

Pavel

Posted 2017-05-11T15:56:28.810

Reputation: 8 585

Times@@#&@@Transpose@FactorInteger@#& saves 3 bytes (#&@@ is standard trick for [[1]] and in cases like this it can often save some extra bytes on parentheses). – Martin Ender – 2017-05-11T18:25:46.833

You can also use Thread instead of Transpose. In Mathematica there's also a 3-byte operator for Transpose, but I don't know whether Mathics supports it. – Martin Ender – 2017-05-11T18:26:27.417

6#&@@(1##&@@FactorInteger@#)& avoids the need to Transpose altogether. (1##&@@ is just Times@@ in disguise, which works great on the ordered pairs yielded by FactorInteger; and '#&@@ is First@ in disguise.) – Greg Martin – 2017-05-11T18:35:20.310

@GregMartin that's basically your own solution, feel free to post it, if you want. – Pavel – 2017-05-13T08:02:35.687

Looks like Scott Milner got it anyway :)

– Greg Martin – 2017-05-13T08:13:01.113

5

Alice, 4 bytes

iDo@

Try it online!

Input and output are given as the code point of a character (works for all valid Unicode code points).

Explanation

Well, Alice has a built-in D whose definition is "Deduplicate prime factors". That is, as long as a value is divisible by some p2 for a prime p, divide that value by p. This happens to be exactly the function required in this challenge. The rest is just input, output, terminate the program.

The reason this was added to Alice actually has nothing to do with this integer sequence. I was trying to stick to a theme of associating divisors with substrings and prime factors with characters. And I needed a function that goes with "deduplicate characters" (which is much more useful in general, because it let's you treat strings as sets, especially when used together with the various multiset operators).

Martin Ender

Posted 2017-05-11T15:56:28.810

Reputation: 184 808

The sad part is, even with a builtin this isn't the shortest answer. – Rɪᴋᴇʀ – 2017-05-11T18:40:56.413

@Riker Well, that's because Alice isn't a golfing language so it needs explicit I/O and (since it's a 2D language) program termination. – Martin Ender – 2017-05-11T18:41:28.617

Yeah, still somewhat sad though. – Rɪᴋᴇʀ – 2017-05-11T19:07:01.650

@ConorO'Brien We just had this discussion elsewhere, and that's only valid if the stand-alone operator is an expression which evaluates to the function (which isn't the case here, since functions/operators aren't first-class values). https://codegolf.meta.stackexchange.com/a/7206/8478

– Martin Ender – 2017-05-14T19:52:44.130

@ConorO'Brien sorry that was an exclusive "we". – Martin Ender – 2017-05-14T20:51:06.547

@ConorO'Brien independently of that post all answers need to be entire programs, or need to define a function, or be an expression that evaluates to a function. – Martin Ender – 2017-05-14T20:55:30.383

@MartinEnder Ohhh ok, cool – Conor O'Brien – 2017-05-14T20:56:47.983

4

Haskell, 31 bytes

f n=until(\r->r^n`mod`n<1)(+1)1

Try it online!

xnor

Posted 2017-05-11T15:56:28.810

Reputation: 115 687

3

Pyke, 3 bytes

P}B

Try it online!

P   -   factors(input)
 }  -  uniquify(^)
  B - product(^)

Blue

Posted 2017-05-11T15:56:28.810

Reputation: 26 661

3

Prolog (SWI), 84 39 bytes

f(N,P):-between(1,N,P),P^N mod N=:=0,!.

Try it online!

Adapted the idea of @xnor's Haskell answer to Prolog.

eush77

Posted 2017-05-11T15:56:28.810

Reputation: 1 280

2

PHP, 70 Bytes

for($r=1,$i=2;1<$n=&$argn;)$n%$i?++$i:$n/=$i+!($r%$i?$r*=$i:1);echo$r;

Try it online!

Jörg Hülsermann

Posted 2017-05-11T15:56:28.810

Reputation: 13 026

1

Pyth, 8 6 bytes

*F+1{P

*-2 bytes thanks to @LeakyNun

Would be 3 if Pyth had a built-in for products of lists...

Try it!

*F+1{P
      Q     # Implicit input
     P      # Prime factors of the input
    {       # Deduplicate
  +1        # Prepend 1 to the list (for the case Q==1)
*F          # Fold * over the list

KarlKastor

Posted 2017-05-11T15:56:28.810

Reputation: 2 352

You can use *F+1{P instead. – Leaky Nun – 2017-05-11T17:40:30.113

1

C, 65 50 bytes

Thanks to @Ørjan Johansen for removing the need for r. Thanks to this and some other dirty tricks I was able to squeeze 15 bytes off!

d;f(n){for(d=1;d++<n;)n%(d*d)||(n/=d--);return n;}

while is gone and replaced with || and index twiddling. <= should have been < all along.

<= turned to < by moving the increment to get n%(++d*d) (should be well defined due to operator precedence).


Original code:

d;r;f(n){for(r=d=1;d++<=n;)while(n%d<1)r*=r%d?d:1,n/=d;return r;}

algmyr

Posted 2017-05-11T15:56:28.810

Reputation: 858

I think you can shorten it by removing r and instead use while(n%(d*d)<1)n/=d;. – Ørjan Johansen – 2017-05-16T00:55:22.183

@ØrjanJohansen That seems right. I was thinking construction rather than reduction. I have some additional improvements to add, will update soon. – algmyr – 2017-05-16T01:40:53.687

++d*d is absolutely not well defined by the C standards - it's a classic case of explicitly undefined behavior. But we're going by implementations here, anyway. – Ørjan Johansen – 2017-05-16T02:35:29.303

Actually, shouldn't d++<n, which is well defined, still work? I think the old version went all the way to n+1 (harmlessly). – Ørjan Johansen – 2017-05-16T02:45:50.187

You're probably right about the undefined behavior. For some reason I thought that operator precedence would solve that. Most examples I've seen of UB uses same priority operators, but of course there is a data race here as well. You're also right about d++<n being correct, for some reason I didn't see that when I rewrote the code. – algmyr – 2017-05-16T03:10:00.223

0

R, 52 bytes

`if`((n=scan())<2,1,prod(unique(c(1,gmp::factorize(n))))

reads n from stdin. Requires the gmp library to be installed (so TIO won't work). Uses the same approach as many of the above answers, but it crashes on an input of 1, because factorize(1) returns an empty vector of class bigz, which crashes unique, alas.

Giuseppe

Posted 2017-05-11T15:56:28.810

Reputation: 21 077

This outputs 12 when I input 12. – Flounderer – 2017-05-12T04:09:22.987

@Flounderer you are correct, I have updated the code. – Giuseppe – 2017-05-12T15:35:42.040

0

Axiom, 89 bytes

f(x:PI):PI==(x=1=>1;g:=factor x;reduce(*,[nthFactor(g,i) for i in 1..numberOfFactors g]))

test and results

(38) -> [[i, f(i)] for i in 1..30 ]
   (38)
   [[1,1], [2,2], [3,3], [4,2], [5,5], [6,6], [7,7], [8,2], [9,3], [10,10],
    [11,11], [12,6], [13,13], [14,14], [15,15], [16,2], [17,17], [18,6],
    [19,19], [20,10], [21,21], [22,22], [23,23], [24,6], [25,5], [26,26],
    [27,3], [28,14], [29,29], [30,30]]

this is the one not use factor() function

g(x:PI):PI==(w:=sqrt(x);r:=i:=1;repeat(i:=i+1;i>w or x<2=>break;x rem i=0=>(r:=r*i;repeat(x rem i=0=>(x:=x quo i);break)));r)

but it is only 125 bytes

RosLuP

Posted 2017-05-11T15:56:28.810

Reputation: 3 036

0

Pari/GP, 28 bytes

n->factorback(factor(n)[,1])

Try it online!

alephalpha

Posted 2017-05-11T15:56:28.810

Reputation: 23 988

0

Actually, 2 bytes

Try it online!

Explanation:

yπ
y   prime divisors
 π  product

Mego

Posted 2017-05-11T15:56:28.810

Reputation: 32 998

0

Pyt, 3 bytes

←ϼΠ

Explanation:

←                  Get input
 ϼ                 Get list of unique prime factors
  Π                Compute product of list
                   Implicit print

mudkip201

Posted 2017-05-11T15:56:28.810

Reputation: 833