22
\$\begingroup\$

Let \$n=42\$ (Input)

Then divisors are : 1, 2, 3, 6, 7, 14, 21, 42

Squaring each divisor : 1, 4, 9, 36, 49, 196, 441, 1764

Taking sum (adding) : 2500

Since \$50\times 50=2500\$ therefore we return a truthy value. If it is not a perfect square, return a falsy value.

Examples :

42  ---> true
1   ---> true
246 ---> true
10  ---> false
16  ---> false

This is so shortest code in bytes for each language wins

Thanks to @Arnauld for pointing out the sequence : A046655

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can the program output 0 if the result is true, and any other number if the result is false? \$\endgroup\$ Sep 11, 2018 at 0:25

45 Answers 45

1
2
1
\$\begingroup\$

Perl 6, 34 bytes

-1 byte thanks to nwellnhof

{grep($_%%*,1..$_)>>².sum**.5%%1}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ **.5 is one byte shorter than .sqrt. \$\endgroup\$
    – nwellnhof
    Sep 11, 2018 at 14:42
1
\$\begingroup\$

F#, 111 bytes

let d n=Seq.where(fun v->n%v=0){1..n}
let u n=
 let m=d n|>Seq.sumBy(fun x->x*x)
 d m|>Seq.exists(fun x->x*x=m)

Try it online!

So d gets the divisors for all numbers between 1 and n inclusive. In the main function u, the first line assigns the sum of all squared divisors to m. The second line gets the divisors for m and determines if any of them squared equals m.

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 47 bytes

$a+=$_*$_*!($n%$_)for 1..$n;$a=!($a**.5=~/\D/); 

Returns 1 for true and nothing for false.

Explanation:

$a+=              for 1..$n;                      sum over i=1 to n
    $_*$_                                         square each component of the sum
         *!($n%$_)                                multiply by 1 if i divides n.
                            $a=                   a equals
                                ($a**.5           whether the square root of a
                               !       =~/\D/);   does not contain a non-digit.
\$\endgroup\$
1
\$\begingroup\$

Groovy, 47 bytes

A lambda accepting a numeric argument.

n->s=(1..n).sum{i->n%i?0:i*i}
!(s%Math.sqrt(s))

Explanation

(1..n) creates an array of the values 1 to n

n%i is false (as 0 is falsy) if i divides n without remainder

n%i ? 0 : i*i is the sum of the square of the value i if it divides n without remainder, otherwise is 0

sum{ i-> n%i ? 0 : i*i } sums the previous result across all i in the array.

s%Math.sqrt(s) is false (as 0 is falsy) if the sqrt of s divides s without remainder

!(s%Math.sqrt(s)) returns from the lambda (return implicit on last statement) !false when the sqrt of s divides s without remainder

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java 8, 75 70 bytes

n->{int s=0,i=0;for(;++i<=n;)s+=n%i<1?i*i:0;return Math.sqrt(s)%1==0;}

-5 bytes thanks to @archangel.mjj.

Try it online.

Explanation:

n->{             // Method with integer parameter and boolean return-type
  int s=0,       //  Sum-integer, starting at 0
      i=0;       //  Divisor integer, starting at 0
  for(;++i<=n;)  //  Loop `i` in the range [1, n]
    s+=n%i<1?    //   If `n` is divisible by `i`:
        i*i      //    Increase the sum by the square of `i`
       :         //   Else:
        0;       //    Leave the sum the same by adding 0
  return Math.sqrt(s)%1==0;}
                 //  Return whether the sum `s` is a perfect square
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Hi, you can cut 5 bytes by removing the t variable (do the eval and assignment within the body of the for loop), like so: n->{int s=0,i=0;for(;++i<=n;)s+=n%i<1?i*i:0;return Math.sqrt(s)%1==0;} \$\endgroup\$ Sep 13, 2018 at 8:26
  • \$\begingroup\$ @archangel.mjj Ah, of course. Not sure how I missed that. Thanks! :) \$\endgroup\$ Sep 13, 2018 at 8:36
1
\$\begingroup\$

Wolfram Language (Mathematica), 24 20 bytes

1∣Norm@Divisors@#&

Try it online!

Divisors finds all the divisors of a number, Norm takes the square root of the sum of squares, and then we test if this is divisible by 1 (i.e., is an integer).

\$\endgroup\$
1
\$\begingroup\$

Ohm v2, 5 bytes

V²ΣƲ

Try it online! Explanation:

V²ΣƲ    
V      Pushes input's divisors
 ²     Squares
   Σ   Sum
    Ʋ Returns whether value is a proper square.
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 115 74 bytes

Thanks to user Misha Lavrov for teaching me how to properly do code golf! :)

a=>{int s=0;for(int i=0;i++<=a;)s+=a%i==0?i*i:0;return Math.Sqrt(s)%1==0;}

Try it online!

Ungolfed:

a => {                              // reads input
    int s = 0;                      // holds sum of squares
    for(int i = 0; i++ <= a;)       // index from 1 to input, inclusive
    {
        s +=                        // add to sum
            a % i == 0 ?            // check if remainder of input divided by current index is zero
                i * i : 0;          // if true, add square of current index, else add zero
    }
    return Math.Sqrt(s) % 1 == 0;   // prints "True" if square root is whole number, "False" otherwise
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ I think it's standard to do input and output via a pure function, something like this. So you don't have to count the length of Console.ReadLine() and such, that way. \$\endgroup\$ Oct 5, 2018 at 20:26
  • \$\begingroup\$ Good to know, thank you. I'll keep this in mind for the future! \$\endgroup\$
    – Meerkat
    Oct 5, 2018 at 20:33
  • \$\begingroup\$ using the interpreter saves a bit, although i've already posted this link on the other C# answer, so not sure what to do \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:58
  • \$\begingroup\$ Also, invalid, you need to use System.Math.Sqrt - the System import isn't free, so this is the cheapest alternative \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 6:00
  • \$\begingroup\$ also, no need to convert input since this is a function \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 6:00
1
\$\begingroup\$

APL(NARS), 23 chars, 46 bytes

{0=1∣√+/×⍨(0=a∣⍵)/a←⍳⍵}

test:

   g←{0=1∣√+/×⍨(0=a∣⍵)/a←⍳⍵}     
   g¨ 42 1 246 10 16
1 1 1 0 0 
\$\endgroup\$
1
\$\begingroup\$

Pyth, 26 16 bytes

!%@s^R2*M{yPQ2 1

Try it online!

I'm new to Pyth (and golfing in general) so this is pretty bad. Pyth doesn't have a built-in to get divisors, so I had to do that. Up until the perfect-square check, it was pretty alright, but after that it became a bit cluttered because of the if statement. Criticism is welcome.

-10 bytes thanks to Sok

Explanation:

            Q               #get user input
       *M{yP                #get the divisors
    ^R2                     #square each divisor
   s                        #sum the list
  @          2              #get square root
 %             1            #check if sqrt is a whole number (mod 1)
!                           #invert (0 -> True, any other num -> False)
\$\endgroup\$
1
  • \$\begingroup\$ A few savings for you - m^d2 can be ^R2, and @ functions as the root operator, so ^...c1h1 can be @...2. The return value is if the root mod 1 is non-zero - that is, should return True if the result of the mod is 0, and False otherwise - this means we can just use ! to invert the truthiness of the mod. Full program is then !%@s^R2*M{yPQ2 1 - demonstration \$\endgroup\$
    – Sok
    Oct 9, 2018 at 8:26
1
\$\begingroup\$

Stax, 10 9 bytes

▀ùj╦┬44eR

Run and debug it at staxlang.xyz!

Unpacked (11 bytes) and explanation:

:d{J+kc|qJ=
:d             Implicit input. Make list of divisors.
  {  k         Reduce using block:
   J+            Square and add to the running total.
      c        Copy this sum of squares on the stack.
       |qJ     Take square root, rounding down. Square.
          =    Check for equality.
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 141 137 135 131 61 bytes

Thanks ASCII-only.

a=>Math.Sqrt(Enumerable.Range(1,a).Sum(b=>a%b==0?b*b:0))%1==0

Explanation:

a => Math.Sqrt(                                                    ) % 1 == 0 //Square root, test for whole number
               Enumerable.Range(1, a).Sum(                        ) //Sum 1 through a with..
                                          b => a % b==0 ? b * b: 0 //b^2 if a modulo b = 0, else 0

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ Divide by zero exception because division by zero is undefined... \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:38
  • \$\begingroup\$ Fixed? \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:38
  • \$\begingroup\$ Wait... this isn't valid \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:51
  • \$\begingroup\$ here \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:54
  • \$\begingroup\$ using the implicit imports of the REPL (mostly for free LINQ) \$\endgroup\$
    – ASCII-only
    Jan 13, 2019 at 5:56
1
\$\begingroup\$

Arturo, 39 bytes

$=>[=floor<=sqrt∑map factors&'x->x*x]

Try it!

$=>[              ; a function where input is assigned to &
    factors&      ; divisors of the input
    map _ 'x->x*x ; square each
    ∑             ; sum
    sqrt          ; square root
    <=            ; duplicate
    floor         ; floor
    =             ; are the equal?
]                 ; end function
\$\endgroup\$
1
\$\begingroup\$

Ruby, 45 bytes

->n{(1..n).map{n%_1<1?_1*_1:0}.sum**0.5%1==0}

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

ARBLE, 33 bytes

nt(sqrt(1+sum(factors(n)|x^2))%1)

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.