Find the perfect square!

05AB1E, 9 6 5 bytes

LnIÖO

Try it online or verify all test cases.

Previous 9 6 bytes approach:

LR.ΔnÖ

-3 bytes thanks to @ovs.

Try it online or verify all test cases.

Explanation:

L       # Push a list in the range [1, (implicit) input]
 n      # Take the square of each value in the list
  IÖ    # Check which squares are divisible by the input (1 if truthy; 0 if falsey)
    O   # And sum those checks
        # (after which this sum is output implicitly as result)

L       # Push a list in the range [1, (implicit) input]
 R      # Reverse it to [input, 1]
  .Δ    # Find the first value in this list which is truthy for:
    n   #  Square the current value
     Ö  #  Check if the (implicit) input is evenly divisible by this square
        # (after which the found value is output implicitly as result)

Jelly, 5 bytes

Ḷ²%ċ0

Uses the formula from the OEIS: the number of solutions to $$x^2 \equiv 0 \ (\mathrm{mod} \ n)$$ Explanation:

  • implicit input
  • range 0..n-1,
  • square each
  • modulo input (I got this part to work via trial and error)
  • count zeroes
  • implicit print

Try it online!


Jelly, 6 bytes

ÆE:2ÆẸ

A monadic Link accepting a positive integer which yields a positive integer.

Try it online! Or see the first 100.

How?

ÆE:2ÆẸ - Link: integer, X          e.g. 9587193
ÆE     - factorisation vector (X)       [0,1,0,4,3] (since 2°×3¹×5°×7⁴×11³=9587193)
  :2   - integer divide by two          [0,0,0,2,1]
    ÆẸ - evaluate factorisation vector  539         (since 2°×3°×5°×7²×11¹=539)

Tags:

Math

Code Golf