-6
\$\begingroup\$

Your Task

You will write a program or function to return a truthy value if the integer inputted to it is a square repdigit, and a falsy value if it is not. A repdigit is an integer that contains only one digit (e.g. 2, 44, 9999). For the purpose of this challenge, a square repdigit is a repdigit that is the square of a different repdigit.

Input

An integer, in base 10.

Output

A truthy/falsy value that reflects whether the input is a square repdigit

Examples

0 --> falsy
1 --> falsy
2 --> falsy
4 --> truthy
9 --> truthy
11 --> falsy

Scoring

This is , lowest bytes wins.

\$\endgroup\$

closed as unclear what you're asking by steenbergh, Sriotchilism O'Zaic, Business Cat, Zacharý, Rɪᴋᴇʀ Jun 12 '17 at 16:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ So, the integer provided and it's square root must be repdigits? \$\endgroup\$ – Shaggy Jun 12 '17 at 10:57
  • 1
    \$\begingroup\$ Is this really much different from Is this number a repdigit? \$\endgroup\$ – TheLethalCoder Jun 12 '17 at 11:02
  • 13
    \$\begingroup\$ So we are asking if input is 4 or 9? \$\endgroup\$ – J42161217 Jun 12 '17 at 11:04
  • 1
    \$\begingroup\$ @Adám No they must be squares of different repdigits. \$\endgroup\$ – Erik the Outgolfer Jun 12 '17 at 11:26
  • 4
    \$\begingroup\$ 4 and 9 are indeed the only repdigits that are squares of other repdigits. The last two digits of any repdigit>10 squared contains two different values: 1..1 -> ..21, 2..2 -> ..84, 3..3 -> ..89, 4..4 -> ..36, 5..5 -> ..25, 6..6 -> ..36, 7..7 -> ..29, 9..9 -> ..01. If you square 8..8, the last three digits are 544. \$\endgroup\$ – JungHwan Min Jun 12 '17 at 12:41

13 Answers 13

8
\$\begingroup\$

Neim, 6 3 2 bytes

+𝕚

Abuses the way that Neim deals with lists.

+ pushes the numeric literal 49 to the stack.

Then, 𝕚 attempts to pop a list from the stack, and Neim implicitly converts 49 to [4 9]. It then pops another element, which is the input, provided implicitly. Finally, it checks that the input is in the list.

Try it!

\$\endgroup\$
  • \$\begingroup\$ Umm, can't you use one of the constants for 49? \$\endgroup\$ – Erik the Outgolfer Jun 12 '17 at 11:20
  • \$\begingroup\$ @EriktheOutgolfer Forgot all about them :P \$\endgroup\$ – Okx Jun 12 '17 at 11:23
  • \$\begingroup\$ And wait Neim is competing? o_O it even beats Jelly \$\endgroup\$ – Erik the Outgolfer Jun 12 '17 at 11:23
  • 1
    \$\begingroup\$ Why the heck does Neim have a built-in for 49? :P \$\endgroup\$ – ETHproductions Jun 12 '17 at 16:05
  • 1
    \$\begingroup\$ @ETHproductions It has a built-in for all numbers 1-156. As it's an incomplete language, characters not assigned are automatically assigned as a variable. It used to have numbers up to 180 (also it was Erik's idea to add these :P) \$\endgroup\$ – Okx Jun 12 '17 at 16:07
6
\$\begingroup\$

Befunge, 20 17 10 bytes

&4-:5-*!.@

Try it online!

Thanks @ovs for -7 bytes!

How?

&4-:5-*!.@    Initial pointer direction >  (Input: 9   )
&             Get input as integer         (Stack: 9   )
 4-           Subtract 4                   (Stack: 5   )         
   :          Duplicate                    (Stack: 5, 5)
    5-        Subtract 5                   (Stack: 5, 0)
      *       Multiply                     (Stack: 0   )
       !      NOT                          (Stack: 1   )
        .     Print as integer             (Stack:     )
         @    End of program

Proof that 4 and 9 are the only valid square repdigits

The last two digits of any repdigit>10 squared are two different values, except 8..8:

 n    n^2 % 100
--------------
1..1     21
2..2     84
3..3     89
4..4     36
5..5     25
6..6     36
7..7     29
8..8     44
9..9     01

88^2 = 7744, and the last three digits of 8..8^2 is 544.

\$\endgroup\$
  • 1
    \$\begingroup\$ +1 just for the proof. I really wish I had checked to make sure there were more repdigits before asking this. \$\endgroup\$ – Gryphon Jun 12 '17 at 13:05
  • \$\begingroup\$ @Gryphon You could use the Sandbox to prevent issues like this. \$\endgroup\$ – JungHwan Min Jun 12 '17 at 13:16
  • \$\begingroup\$ Yes, I should have posted this in the sandbox first. \$\endgroup\$ – Gryphon Jun 12 '17 at 13:17
  • \$\begingroup\$ &4-:5-*!.@ for 10 bytes \$\endgroup\$ – ovs Jun 12 '17 at 14:29
  • \$\begingroup\$ @ovs Wow, that's awesome. I couldn't think of using a NOT... \$\endgroup\$ – JungHwan Min Jun 12 '17 at 14:42
4
\$\begingroup\$

Mathematica, 11 bytes

#==4||#==9&
\$\endgroup\$
1
\$\begingroup\$

C#, JavaScript, Java, 12 bytes

n=>n==4|n==9

For Java replace => with ->.

Or a version more in the spirit of the challenge, C# only, for 61 bytes:

g=n=>n.Replace(n[0]+"","")==""
n=>g(n)&g(System.Math.Sqrt(n))
\$\endgroup\$
  • 1
    \$\begingroup\$ The 12 byte solution works for JS too. \$\endgroup\$ – Shaggy Jun 12 '17 at 11:19
  • \$\begingroup\$ @Shaggy I suppose it will work for Java too \$\endgroup\$ – TheLethalCoder Jun 12 '17 at 11:21
  • \$\begingroup\$ Doesn't Java use - instead of = for arrow functions? \$\endgroup\$ – Shaggy Jun 12 '17 at 11:22
  • \$\begingroup\$ @Shaggy Oh yeah... added a comment about it because it is really the same code. \$\endgroup\$ – TheLethalCoder Jun 12 '17 at 11:23
  • \$\begingroup\$ Was about to post a Java answer when I saw this. +1 \$\endgroup\$ – Kevin Cruijssen Jun 12 '17 at 11:32
1
\$\begingroup\$

Jelly, 4 bytes

e4,9

Try it online!

Note: if you disprove me, I'll edit. This returns a truthy result only if the input is 4 or 9.

Junghuan Min ninja'd a proof...

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

05AB1E, 5 bytes

49S¹å

Try it online!

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

APL (Dyalog), 5 bytes

Just tests for membership of {4,9}

∊∘4 9

Try it online!

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

Braingolf, 19 bytes

.pl3e1:0|>e1:0|<+1-

Try it online!

This is as far as I can tell actually the shortest way to check "is n 4 or 9?"

Will output 1 if input is 4 or 9, otherwise will output 0

Explanation

.pl3e1:0|>e1:0|<+1-  Implicit input from commandline args
.p                   Duplicate, pop duplicate, push prime factors
  l3e                If there are exactly 3 items in stack..
                     (2 prime factors plus original input)
     1               ..Push 1
      :              Else
       0             ..Push 0
        |            Endif
         >           Move last item to start
          e          If 2 prime factors are equal..
           1         ..Push 1
            :        Else
             0       ..Push 0
              |      Endif
               <     Move first item to end
                +    Sum last 2 items
                 1-  Subtract 1
                     Implicit output.
\$\endgroup\$
  • \$\begingroup\$ That's not very golfy, considering the language name is Braingolf and it is beaten by Java. \$\endgroup\$ – Okx Jun 12 '17 at 12:39
  • \$\begingroup\$ @Okx it's bad at complex conditionals \$\endgroup\$ – Skidsdev Jun 12 '17 at 13:07
0
\$\begingroup\$

PHP, 22 bytes

<?=$argn==4||$argn==9;

Try it online!

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

Python, 18 bytes

lambda n:n in[4,9]

Try it online!

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

Python 3, 39 25 bytes

Solution can be shorter for sure.

old:

n=input();print(n in ['0','1','4','9'])

Try it online!

new:

print(input()in['4','9'])

Try it online!


From this: "a square repdigit is a repdigit that is the square of a different repdigit."

Let z='ζζζζ...' be the square repdigit.

Case 1 z<10:

0² = 0

1² = 1

2² = 4

3² = 9

4² to 9² are not square repdigits.

Case z>10:

Lets assume there is an y∈ℕ with y²=z and y='xxxxx..'

We have just tested all single digits, so if there is an y with y² = z, then it has to be y>10.

So we can write y = x[10°+10¹] + x[10²+10³+...]

Then it is: y² = x²[10°+10¹]² + r = x² [10° + 2*10¹] + r, with r only containing summands of power >1.

Comparing z='ζζζ..' with y² we get: x²=ζ and 2x²=ζ, hence x=0.

⇒ The only square repdigits are 0,1, 4 and 9.

\$\endgroup\$
  • \$\begingroup\$ I don't understand the + r part. \$\endgroup\$ – JungHwan Min Jun 12 '17 at 12:42
  • 2
    \$\begingroup\$ Emphasis: a square repdigit is a repdigit that is the square of a different repdigit. \$\endgroup\$ – Okx Jun 12 '17 at 12:42
  • \$\begingroup\$ @JungHwanMin: You are right "r>100" is not clear. Now, as r=r₂*10²+r₃*10³+..., it should be clear, that r just contains all dezimals >=10². \$\endgroup\$ – P. Siehr Jun 12 '17 at 12:51
  • \$\begingroup\$ I think you can write [10⁰+10¹] as simply 11. Also, I can't understand what the r is for. \$\endgroup\$ – Erik the Outgolfer Jun 12 '17 at 12:52
  • 1
    \$\begingroup\$ Welcome on the site! Nice first answer! (as Okx mentioned, 0 and 1 are not square repdigits though) \$\endgroup\$ – Dada Jun 12 '17 at 12:52
0
\$\begingroup\$

Javascript, 17 bytes

a=>!((a-4)*(a-9))

f=
a=>!((a-4)*(a-9))

for (i=0;i<12;i++) console.log(''+i+' -> '+f(i))

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

Batch, 65 58 53 46 bytes

I have code-copying issues.

@if %1 neq 4 if %1 neq 9 echo 0&exit/b
echo 1

Return 1 for truthy value, 0 for falsely value.

\$\endgroup\$
  • 1
    \$\begingroup\$ Unrolling the loop to @if %1 neq 4 if %1 neq 9 echo 0&exit/b is a few bytes shorter. (FYI my solution is 26 bytes.) \$\endgroup\$ – Neil Jun 12 '17 at 12:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.